Skip to content

Commit

Permalink
Merge pull request #136 from Cysharp/hadashiA/unity-ext
Browse files Browse the repository at this point in the history
Additional unity feature
  • Loading branch information
neuecc authored Dec 22, 2023
2 parents 6c791c8 + 10382ee commit 3c7adcb
Show file tree
Hide file tree
Showing 31 changed files with 722 additions and 140 deletions.
3 changes: 3 additions & 0 deletions sandbox/GeneratorSandbox/GeneratedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public Type GetParameterType(int index)
return default!;
}

public object? GetContext() => null;

public int Count => 2;

public KeyValuePair<string, object?> this[int index] => index switch
Expand Down Expand Up @@ -278,4 +280,5 @@ public Type GetParameterType(int index)
return default!;
}

public object? GetContext() => null;
}
2 changes: 2 additions & 0 deletions src/ZLogger.Generator/ZLoggerGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public IZLoggerEntry CreateEntry(LogInfo info)
return ZLoggerEntry<{{stateTypeName}}>.Create(info, this);
}
public object? GetContext() => null;

""");

EmitIZLoggerFormattableMethods(method);
Expand Down
17 changes: 13 additions & 4 deletions src/ZLogger.Unity/Assets/Sample/SampleBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Sample;
using UnityEngine;
using ZLogger;
using ZLogger.Unity;
Expand All @@ -12,6 +13,8 @@ public static partial class Log

public class SampleBehaviour : MonoBehaviour
{
ILogger logger;

void Start()
{
using var loggerFactory = LoggerFactory.Create(logging =>
Expand All @@ -30,18 +33,24 @@ void Start()
});

var logger = loggerFactory.CreateLogger(nameof(SampleBehaviour));
logger = loggerFactory.CreateLogger(nameof(SampleBehaviour));

var name = "Hoge";
var id = 12345;

logger.ZLogInformation($"!!!!!! Hello {name} your id is {id:@userId}");
logger.Log(LogLevel.Information, $"Log");
logger.LogInformation($"LogInformation");

logger.ZLog(LogLevel.Information, $"ZLog Hello {name} your id is {id:@userId}");
logger.ZLogInformation($"ZLogInformation Hello {name} your id is {id:@userId}");

using (logger.BeginScope("{Id}", id))
{
logger.ZLogInformation($"@@@@@@@@ Hello {name}");
logger.ZLogInformation($"Scoped log {name}");
}

logger.Hello("example.com", "111.111.111.111");
logger.ZLogInformation($"with context {name}", this);

new SampleClass1(logger).Method1();
}
}
41 changes: 41 additions & 0 deletions src/ZLogger.Unity/Assets/Sample/SampleClass.cs
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");
}
}
3 changes: 3 additions & 0 deletions src/ZLogger.Unity/Assets/Sample/SampleClass.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions src/ZLogger.Unity/Assets/Scenes.meta

This file was deleted.

164 changes: 164 additions & 0 deletions src/ZLogger.Unity/Assets/ZLogger.Unity/Runtime/DiagnosticsHelper.cs
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>";
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3c7adcb

Please sign in to comment.