From c8d5c65593d576d5aa9cddec5806de94ec0d3d7a Mon Sep 17 00:00:00 2001 From: uurha Date: Sat, 24 Feb 2024 10:25:34 +0000 Subject: [PATCH] Rework BetterLogger to Unity ILogHandler --- Runtime/BetterLogger.Runtime.asmdef | 3 +- Runtime/Logger/BetterLogger.cs | 209 ---------------------- Runtime/Logger/BetterLogger.cs.meta | 11 -- Runtime/Logger/LogBuilder.cs | 59 +++--- Runtime/Logger/LogHandler.cs | 28 +++ Runtime/Logger/LogHandler.cs.meta | 3 + Runtime/Logger/LogHandlerInjector.cs | 22 +++ Runtime/Logger/LogHandlerInjector.cs.meta | 3 + Runtime/Settings/LoggerSettings.cs | 2 - package.json | 8 +- 10 files changed, 90 insertions(+), 258 deletions(-) delete mode 100644 Runtime/Logger/BetterLogger.cs delete mode 100644 Runtime/Logger/BetterLogger.cs.meta create mode 100644 Runtime/Logger/LogHandler.cs create mode 100644 Runtime/Logger/LogHandler.cs.meta create mode 100644 Runtime/Logger/LogHandlerInjector.cs create mode 100644 Runtime/Logger/LogHandlerInjector.cs.meta diff --git a/Runtime/BetterLogger.Runtime.asmdef b/Runtime/BetterLogger.Runtime.asmdef index 2c5ef29..59cb182 100644 --- a/Runtime/BetterLogger.Runtime.asmdef +++ b/Runtime/BetterLogger.Runtime.asmdef @@ -2,7 +2,8 @@ "name": "BetterLogger.Runtime", "rootNamespace": "Better.Logger.Runtime", "references": [ - "GUID:a59e3daedde9ca94bba45364d4ead25f" + "GUID:a59e3daedde9ca94bba45364d4ead25f", + "GUID:28da8d3b12e3efa47928e0c9070f853d" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Runtime/Logger/BetterLogger.cs b/Runtime/Logger/BetterLogger.cs deleted file mode 100644 index bf0b677..0000000 --- a/Runtime/Logger/BetterLogger.cs +++ /dev/null @@ -1,209 +0,0 @@ -using System; -using System.Diagnostics; -using System.Reflection; -using System.Runtime.CompilerServices; -using Better.Logger.Runtime; -using UnityEngine; -using Debug = UnityEngine.Debug; -using Object = UnityEngine.Object; - -public static class BetterLogger -{ - - #region Log - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void Log(string message) - { - LogTypeInternal(LogType.Log, message, null); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void Log(object message) - { - LogTypeInternal(LogType.Log, message, null); - } - - [DebuggerNonUserCode] - public static void Log(string message, Object context) - { - LogTypeInternal(LogType.Log, message, context); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void Log(object message, Object context) - { - LogTypeInternal(LogType.Log, message, context); - } - - #endregion - - #region Warning - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogWarning(string message) - { - LogTypeInternal(LogType.Warning, message, null); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogWarning(object message) - { - LogTypeInternal(LogType.Warning, message, null); - } - - [DebuggerNonUserCode] - public static void LogWarning(string message, Object context) - { - LogTypeInternal(LogType.Warning, message, context); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogWarning(object message, Object context) - { - LogTypeInternal(LogType.Warning, message, context); - } - - #endregion - - #region Error - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogError(string message) - { - LogTypeInternal(LogType.Error, message, null); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogError(object message) - { - LogTypeInternal(LogType.Error, message, null); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogError(string message, Object context) - { - LogTypeInternal(LogType.Error, message, context); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogError(object message, Object context) - { - LogTypeInternal(LogType.Error, message, context); - } - - #endregion - - #region Exception - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(Exception exception) - { - LogExceptionInternal(exception, exception.Message,null); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(Exception exception, Object context) - { - LogExceptionInternal(exception, exception.Message, context); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(object message) where T : Exception, new() - { - var exception = new T(); - LogExceptionInternal(exception, message, null); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(object message, Object context) where T : Exception, new() - { - var exception = new T(); - LogExceptionInternal(exception, message, context); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(string message) where T : Exception, new() - { - var exception = new T(); - LogExceptionInternal(exception, message, null); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(string message, Object context) where T : Exception, new() - { - var exception = new T(); - LogExceptionInternal(exception, message, context); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(object message) - { - var exception = new Exception(); - LogExceptionInternal(exception, message, null); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(object message, Object context) - { - var exception = new Exception(); - LogExceptionInternal(exception, message, context); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(string message) - { - var exception = new Exception(); - LogExceptionInternal(exception, message, null); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - public static void LogException(string message, Object context) - { - var exception = new Exception(); - LogExceptionInternal(exception, message, context); - } - - #endregion - - #region Internal - - [DebuggerHidden] - [DebuggerNonUserCode] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void LogExceptionInternal(Exception exception, object message, Object context) - { - LogBuilder.BuildLogException(exception, message); - Debug.unityLogger.LogException(exception, context); - } - - [DebuggerHidden] - [DebuggerNonUserCode] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void LogTypeInternal(LogType logType, object message, Object context) - { - Debug.unityLogger.Log(logType, LogBuilder.BuildLogObject(message), context); - } - - #endregion -} \ No newline at end of file diff --git a/Runtime/Logger/BetterLogger.cs.meta b/Runtime/Logger/BetterLogger.cs.meta deleted file mode 100644 index 333700f..0000000 --- a/Runtime/Logger/BetterLogger.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: a1a806f91660a0b428decc00c9fd539e -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Runtime/Logger/LogBuilder.cs b/Runtime/Logger/LogBuilder.cs index dfee939..ece4c86 100644 --- a/Runtime/Logger/LogBuilder.cs +++ b/Runtime/Logger/LogBuilder.cs @@ -1,7 +1,8 @@ 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; @@ -9,33 +10,41 @@ 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(nameof(LoggerSettings)); - MessageField = GetExceptionMessage(); + _settings = Resources.Load(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; @@ -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); } } } \ No newline at end of file diff --git a/Runtime/Logger/LogHandler.cs b/Runtime/Logger/LogHandler.cs new file mode 100644 index 0000000..ecd9c1f --- /dev/null +++ b/Runtime/Logger/LogHandler.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/Runtime/Logger/LogHandler.cs.meta b/Runtime/Logger/LogHandler.cs.meta new file mode 100644 index 0000000..fdae9e6 --- /dev/null +++ b/Runtime/Logger/LogHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e551c3a58ff84da19671b6adcc14bf00 +timeCreated: 1708766701 \ No newline at end of file diff --git a/Runtime/Logger/LogHandlerInjector.cs b/Runtime/Logger/LogHandlerInjector.cs new file mode 100644 index 0000000..cb3bab4 --- /dev/null +++ b/Runtime/Logger/LogHandlerInjector.cs @@ -0,0 +1,22 @@ +using UnityEditor; +using UnityEngine; + +namespace Better.Logger.Runtime +{ + internal class LogHandlerInjector + { + private static readonly LogHandler _logHandler; + + static LogHandlerInjector() + { + _logHandler = new LogHandler(); + } + + [InitializeOnLoadMethod] + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)] + public static void OnInitialize() + { + Debug.unityLogger.logHandler = _logHandler; + } + } +} \ No newline at end of file diff --git a/Runtime/Logger/LogHandlerInjector.cs.meta b/Runtime/Logger/LogHandlerInjector.cs.meta new file mode 100644 index 0000000..b6fabee --- /dev/null +++ b/Runtime/Logger/LogHandlerInjector.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 97cdafa4d0944c22bc562da814022aee +timeCreated: 1708767115 \ No newline at end of file diff --git a/Runtime/Settings/LoggerSettings.cs b/Runtime/Settings/LoggerSettings.cs index ec492b1..29a35b1 100644 --- a/Runtime/Settings/LoggerSettings.cs +++ b/Runtime/Settings/LoggerSettings.cs @@ -13,10 +13,8 @@ public class LoggerSettings : ProjectSettings [SerializeField] private bool _useFormatting = true; - public string LogFormat => _logFormat; public string ExceptionFormat => _exceptionFormat; - public bool UseFormatting => _useFormatting; } } \ No newline at end of file diff --git a/package.json b/package.json index b646d89..0268ea9 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,12 @@ { "name": "com.tdw.better.logger", "displayName": "Better Logger", - "version": "0.0.2", + "version": "0.0.4", "unity": "2021.3", "description": " ", "dependencies": { - "com.uurha.bettereditortools": "1.0.69" + "com.uurha.bettereditortools": "1.0.70", + "com.uurha.betterextensions": "1.2.3" }, "author": { "name": "Better Plugins", @@ -20,6 +21,7 @@ "logging", "logs", "log", - "log management" + "log management", + "log handler" ] }