-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from Cysharp/hadashiA/unity-ext
Additional unity feature
- Loading branch information
Showing
31 changed files
with
722 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Microsoft.Extensions.Logging; | ||
using ZLogger; | ||
|
||
namespace Sample; | ||
|
||
public class SampleClass1 | ||
{ | ||
readonly ILogger logger; | ||
|
||
public SampleClass1(ILogger logger) | ||
{ | ||
this.logger = logger; | ||
} | ||
|
||
public void Method1() | ||
{ | ||
Method2(); | ||
} | ||
|
||
public void Method2() | ||
{ | ||
new SampleClass2<int>(logger).Method1(); | ||
} | ||
} | ||
|
||
public class SampleClass2<T> | ||
{ | ||
readonly ILogger logger; | ||
|
||
public SampleClass2(ILogger logger) | ||
{ | ||
this.logger = logger; | ||
} | ||
|
||
public void Method1() | ||
{ | ||
logger.ZLog(LogLevel.Information, $"ZLog Hello"); | ||
logger.ZLogInformation($"ZLogInformation Hello"); | ||
logger.Hello("example.com", "111.111.111.111"); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
164 changes: 164 additions & 0 deletions
164
src/ZLogger.Unity/Assets/ZLogger.Unity/Runtime/DiagnosticsHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Security; | ||
using System.Text.RegularExpressions; | ||
using Application = UnityEngine.Device.Application; | ||
|
||
namespace ZLogger.Unity.Runtime; | ||
|
||
public static class DiagnosticsHelper | ||
{ | ||
static readonly Regex typeBeautifyRegex = new("`.+$", RegexOptions.Compiled); | ||
|
||
static readonly Dictionary<Type, string> builtInTypeNames = new() | ||
{ | ||
{ typeof(void), "void" }, | ||
{ typeof(bool), "bool" }, | ||
{ typeof(byte), "byte" }, | ||
{ typeof(char), "char" }, | ||
{ typeof(decimal), "decimal" }, | ||
{ typeof(double), "double" }, | ||
{ typeof(float), "float" }, | ||
{ typeof(int), "int" }, | ||
{ typeof(long), "long" }, | ||
{ typeof(object), "object" }, | ||
{ typeof(sbyte), "sbyte" }, | ||
{ typeof(short), "short" }, | ||
{ typeof(string), "string" }, | ||
{ typeof(uint), "uint" }, | ||
{ typeof(ulong), "ulong" }, | ||
{ typeof(ushort), "ushort" } | ||
}; | ||
|
||
static string? applicationDataPath; | ||
static bool displayFilenames = true; | ||
|
||
public static string CleanupStackTrace(StackTrace stackTrace) | ||
{ | ||
var sb = new StringBuilder(); | ||
for (var i = 0; i < stackTrace.FrameCount; i++) | ||
{ | ||
var sf = stackTrace.GetFrame(i); | ||
var mb = sf.GetMethod(); | ||
|
||
if (IgnoreLine(mb)) continue; | ||
|
||
// return type | ||
if (mb is MethodInfo mi) | ||
{ | ||
sb.Append(BeautifyType(mi.ReturnType, false)); | ||
sb.Append(" "); | ||
} | ||
|
||
// method name | ||
sb.Append(BeautifyType(mb.DeclaringType!, false)); | ||
if (!mb.IsConstructor) | ||
{ | ||
sb.Append("."); | ||
} | ||
sb.Append(mb.Name); | ||
if (mb.IsGenericMethod) | ||
{ | ||
sb.Append("<"); | ||
foreach (var item in mb.GetGenericArguments()) | ||
{ | ||
sb.Append(BeautifyType(item, true)); | ||
} | ||
sb.Append(">"); | ||
} | ||
|
||
// parameter | ||
sb.Append("("); | ||
sb.Append(string.Join(", ", mb.GetParameters().Select(p => BeautifyType(p.ParameterType, true) + " " + p.Name))); | ||
sb.Append(")"); | ||
|
||
// file name | ||
if (displayFilenames && sf.GetILOffset() != -1) | ||
{ | ||
string? fileName = null; | ||
try | ||
{ | ||
fileName = sf.GetFileName(); | ||
} | ||
catch (NotSupportedException) | ||
{ | ||
displayFilenames = false; | ||
} | ||
catch (SecurityException) | ||
{ | ||
displayFilenames = false; | ||
} | ||
|
||
if (fileName != null) | ||
{ | ||
sb.Append(' '); | ||
sb.AppendFormat(CultureInfo.InvariantCulture, "(at {0})", AppendHyperLink(fileName, sf.GetFileLineNumber().ToString())); | ||
} | ||
} | ||
|
||
sb.AppendLine(); | ||
} | ||
return sb.ToString(); | ||
} | ||
|
||
static string BeautifyType(Type t, bool shortName) | ||
{ | ||
if (builtInTypeNames.TryGetValue(t, out var builtin)) | ||
{ | ||
return builtin; | ||
} | ||
if (t.IsGenericParameter) return t.Name; | ||
if (t.IsArray) return BeautifyType(t.GetElementType()!, shortName) + "[]"; | ||
if (t.FullName?.StartsWith("System.ValueTuple") ?? false) | ||
{ | ||
return "(" + string.Join(", ", t.GetGenericArguments().Select(x => BeautifyType(x, true))) + ")"; | ||
} | ||
|
||
if (!t.IsGenericType) | ||
{ | ||
return shortName ? t.Name : t.FullName ?? t.Name; | ||
} | ||
|
||
var innerFormat = string.Join(", ", t.GetGenericArguments().Select(x => BeautifyType(x, true))); | ||
|
||
var genericType = t.GetGenericTypeDefinition().FullName; | ||
if (genericType == "System.Threading.Tasks.Task`1") | ||
{ | ||
genericType = "Task"; | ||
} | ||
return typeBeautifyRegex.Replace(genericType, "").Replace("Cysharp.Threading.Tasks.Triggers.", "").Replace("Cysharp.Threading.Tasks.Internal.", "").Replace("Cysharp.Threading.Tasks.", "") + "<" + innerFormat + ">"; | ||
} | ||
|
||
static bool IgnoreLine(MethodBase methodInfo) | ||
{ | ||
var declareType = methodInfo.DeclaringType!.FullName!; | ||
if (declareType.StartsWith("ZLogger")) | ||
{ | ||
return true; | ||
} | ||
if (declareType.StartsWith("Microsoft.Extensions.Logging")) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
static string AppendHyperLink(string path, string line) | ||
{ | ||
var fi = new FileInfo(path); | ||
if (fi.Directory == null) | ||
{ | ||
return fi.Name; | ||
} | ||
|
||
var root = applicationDataPath ??= Application.dataPath; | ||
var href = fi.FullName.Replace(Path.DirectorySeparatorChar, '/').Replace(root, "Assets"); | ||
return "<a href=\"" + href + "\" line=\"" + line + "\">" + href + ":" + line + "</a>"; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/ZLogger.Unity/Assets/ZLogger.Unity/Runtime/DiagnosticsHelper.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
src/ZLogger.Unity/Assets/ZLogger.Unity/Runtime/InterpolatedStringHandlerArgumentAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma warning disable | ||
#nullable enable annotations | ||
|
||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
/// <summary> | ||
/// Indicates which arguments to a method involving an interpolated string handler should be passed to that handler. | ||
/// </summary> | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] | ||
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] | ||
internal sealed class InterpolatedStringHandlerArgumentAttribute : global::System.Attribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="global::System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute"/> class. | ||
/// </summary> | ||
/// <param name="argument">The name of the argument that should be passed to the handler.</param> | ||
/// <remarks><see langword="null"/> may be used as the name of the receiver in an instance method.</remarks> | ||
public InterpolatedStringHandlerArgumentAttribute(string argument) | ||
{ | ||
Arguments = new string[] { argument }; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="global::System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute"/> class. | ||
/// </summary> | ||
/// <param name="arguments">The names of the arguments that should be passed to the handler.</param> | ||
/// <remarks><see langword="null"/> may be used as the name of the receiver in an instance method.</remarks> | ||
public InterpolatedStringHandlerArgumentAttribute(params string[] arguments) | ||
{ | ||
Arguments = arguments; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the names of the arguments that should be passed to the handler. | ||
/// </summary> | ||
/// <remarks><see langword="null"/> may be used as the name of the receiver in an instance method.</remarks> | ||
public string[] Arguments { get; } | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ger.Unity/Assets/ZLogger.Unity/Runtime/InterpolatedStringHandlerArgumentAttribute.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.