Skip to content

Commit

Permalink
Rework BetterLogger to Unity ILogHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Feb 24, 2024
1 parent 0326ccb commit c8d5c65
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 258 deletions.
3 changes: 2 additions & 1 deletion Runtime/BetterLogger.Runtime.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "BetterLogger.Runtime",
"rootNamespace": "Better.Logger.Runtime",
"references": [
"GUID:a59e3daedde9ca94bba45364d4ead25f"
"GUID:a59e3daedde9ca94bba45364d4ead25f",
"GUID:28da8d3b12e3efa47928e0c9070f853d"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
209 changes: 0 additions & 209 deletions Runtime/Logger/BetterLogger.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Runtime/Logger/BetterLogger.cs.meta

This file was deleted.

59 changes: 27 additions & 32 deletions Runtime/Logger/LogBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using Better.Extensions.Runtime;
using Better.Logger.Runtime.Settings;
using UnityEngine;

namespace Better.Logger.Runtime
{
public static class LogBuilder
{
private static readonly LoggerSettings Settings;
private static readonly FieldInfo MessageField;

private const string ExceptionMessageFieldName = "_message";
private static readonly LoggerSettings _settings;
private const int JumpCount = 4;
private const string MethodNameFormat = "{0}{1}";
private const string Null = "null";
private const string Unknown = "Unknown";

static LogBuilder()
{
Settings = Resources.Load<LoggerSettings>(nameof(LoggerSettings));
MessageField = GetExceptionMessage();
_settings = Resources.Load<LoggerSettings>(nameof(LoggerSettings));
}

private static string GetDeclaringTypeName(MethodBase methodBase)
{
if (methodBase == null)
{
return Unknown;
}

return methodBase.DeclaringType != null ? methodBase.DeclaringType.Name : "Global";
}

private static string GetMethodName(MethodBase methodBase)
{
if (methodBase == null)
{
return Unknown;
}

var name = methodBase.Name;
switch (name)
{
case ".ctor":
case ".cctor":
var typeName = GetDeclaringTypeName(methodBase);
const string format = "{0}{1}";
return string.Format(format, typeName, name);
return string.Format(MethodNameFormat, typeName, name);
}

return name;
Expand All @@ -53,47 +62,33 @@ private static StringBuilder GetLogBuilder(string logFormat, string message, Met
.Replace(LoggerDefinitions.Message, message);
return builder;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static FieldInfo GetExceptionMessage()
{
var type = typeof(Exception);
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
return type.GetField(ExceptionMessageFieldName, flags);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ReplaceExceptionMessage(Exception exception, object message)
{
MessageField.SetValue(exception, message.ToString());
}

public static object BuildLogObject(object message)
public static string BuildLogObject(string message)
{
if (!Settings.UseFormatting)
if (!_settings.UseFormatting)
return message;

return BuildLogByFormat(Settings.LogFormat, message, 3);
return BuildLogByFormat(_settings.LogFormat, message, JumpCount);
}

private static object BuildLogByFormat(string logFormat, object message, int jumpCount)
private static string BuildLogByFormat(string logFormat, string message, int jumpCount)
{
using (var stackFrame = new JumpStackFrame())
{
stackFrame.Jump(jumpCount);
var methodBase = stackFrame.GetMethod();
message ??= Null;
return GetLogBuilder(logFormat, message.ToString(), methodBase);
return GetLogBuilder(logFormat, message, methodBase).ToString();
}
}

public static void BuildLogException(Exception exception, object message)
public static void BuildLogException(Exception exception, string message)
{
if (!Settings.UseFormatting)
if (!_settings.UseFormatting)
return;

var logObject = BuildLogByFormat(Settings.ExceptionFormat, message, 3);
ReplaceExceptionMessage(exception, logObject);
var logObject = BuildLogByFormat(_settings.ExceptionFormat, message, JumpCount);
exception.ReplaceExceptionMessageField(logObject);
}
}
}
28 changes: 28 additions & 0 deletions Runtime/Logger/LogHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Better.Logger.Runtime
{
internal class LogHandler : ILogHandler
{
private readonly ILogHandler _defaultLogHandler;

public LogHandler()
{
_defaultLogHandler = Debug.unityLogger.logHandler;
}

public void LogFormat(LogType logType, Object context, string format, params object[] args)
{
var newFormat = LogBuilder.BuildLogObject(format);
_defaultLogHandler.LogFormat(logType, context, newFormat, args);
}

public void LogException(Exception exception, Object context)
{
LogBuilder.BuildLogException(exception, exception.Message);
_defaultLogHandler.LogException(exception, context);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Logger/LogHandler.cs.meta

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

Loading

0 comments on commit c8d5c65

Please sign in to comment.