ManualLogSource: Add string interpolation for Log* overloads; clean up logging

This commit is contained in:
ghorsington committed 2021-12-21 19:40:15 +02:00
1 parent 4bb48c502e
commit 3fd5d68e3b
9 files changed
+126 -17

No files matched your search

@@ -3,7 +3,7 @@ using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using BepInEx.Logging; using BepInEx.Logging;
namespace BepInEx.Core.Logging; namespace BepInEx.Core.Logging.Interpolation;
/// <summary> /// <summary>
/// Interpolated string handler for BepInEx <see cref="Logger" />. This allows to conditionally skip logging certain /// Interpolated string handler for BepInEx <see cref="Logger" />. This allows to conditionally skip logging certain
@@ -99,3 +99,69 @@ public class BepInExLogInterpolatedStringHandler
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => sb?.ToString() ?? string.Empty; public override string ToString() => sb?.ToString() ?? string.Empty;
} }
/// <inheritdoc />
[InterpolatedStringHandler]
public class BepInExFatalLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
{
/// <inheritdoc />
public BepInExFatalLogInterpolatedStringHandler(int literalLength,
int formattedCount,
out bool isEnabled) : base(literalLength, formattedCount,
LogLevel.Fatal, out isEnabled) { }
}
/// <inheritdoc />
[InterpolatedStringHandler]
public class BepInExErrorLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
{
/// <inheritdoc />
public BepInExErrorLogInterpolatedStringHandler(int literalLength,
int formattedCount,
out bool isEnabled) : base(literalLength, formattedCount,
LogLevel.Error, out isEnabled) { }
}
/// <inheritdoc />
[InterpolatedStringHandler]
public class BepInExWarningLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
{
/// <inheritdoc />
public BepInExWarningLogInterpolatedStringHandler(int literalLength,
int formattedCount,
out bool isEnabled) : base(literalLength, formattedCount,
LogLevel.Warning, out isEnabled) { }
}
/// <inheritdoc />
[InterpolatedStringHandler]
public class BepInExMessageLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
{
/// <inheritdoc />
public BepInExMessageLogInterpolatedStringHandler(int literalLength,
int formattedCount,
out bool isEnabled) : base(literalLength, formattedCount,
LogLevel.Message, out isEnabled) { }
}
/// <inheritdoc />
[InterpolatedStringHandler]
public class BepInExInfoLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
{
/// <inheritdoc />
public BepInExInfoLogInterpolatedStringHandler(int literalLength,
int formattedCount,
out bool isEnabled) : base(literalLength, formattedCount,
LogLevel.Info, out isEnabled) { }
}
/// <inheritdoc />
[InterpolatedStringHandler]
public class BepInExDebugLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
{
/// <inheritdoc />
public BepInExDebugLogInterpolatedStringHandler(int literalLength,
int formattedCount,
out bool isEnabled) : base(literalLength, formattedCount,
LogLevel.Debug, out isEnabled) { }
}
+1 -1
View File
@@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using BepInEx.Core.Logging; using BepInEx.Core.Logging.Interpolation;
namespace BepInEx.Logging; namespace BepInEx.Logging;
+44 -2
View File
@@ -1,6 +1,6 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using BepInEx.Core.Logging; using BepInEx.Core.Logging.Interpolation;
namespace BepInEx.Logging; namespace BepInEx.Logging;
@@ -34,8 +34,14 @@ public class ManualLogSource : ILogSource
/// <param name="data">Data to log.</param> /// <param name="data">Data to log.</param>
public void Log(LogLevel level, object data) => LogEvent?.Invoke(this, new LogEventArgs(data, level, this)); public void Log(LogLevel level, object data) => LogEvent?.Invoke(this, new LogEventArgs(data, level, this));
/// <summary>
/// Logs an interpolated string with the specified log level.
/// </summary>
/// <param name="level">Log levels to attach to the message. Multiple can be used with bitwise ORing.</param>
/// <param name="logHandler">Handler for the interpolated string.</param>
public void Log(LogLevel level, public void Log(LogLevel level,
[InterpolatedStringHandlerArgument("level")] BepInExLogInterpolatedStringHandler logHandler) [InterpolatedStringHandlerArgument("level")]
BepInExLogInterpolatedStringHandler logHandler)
{ {
if (logHandler.Enabled) if (logHandler.Enabled)
LogEvent?.Invoke(this, new LogEventArgs(logHandler.ToString(), level, this)); LogEvent?.Invoke(this, new LogEventArgs(logHandler.ToString(), level, this));
@@ -47,33 +53,69 @@ public class ManualLogSource : ILogSource
/// <param name="data">Data to log.</param> /// <param name="data">Data to log.</param>
public void LogFatal(object data) => Log(LogLevel.Fatal, data); public void LogFatal(object data) => Log(LogLevel.Fatal, data);
/// <summary>
/// Logs an interpolated string with <see cref="LogLevel.Fatal" /> level.
/// </summary>
/// <param name="logHandler">Handler for the interpolated string.</param>
public void LogFatal(BepInExFatalLogInterpolatedStringHandler logHandler) => Log(LogLevel.Fatal, logHandler);
/// <summary> /// <summary>
/// Logs a message with <see cref="LogLevel.Error" /> level. /// Logs a message with <see cref="LogLevel.Error" /> level.
/// </summary> /// </summary>
/// <param name="data">Data to log.</param> /// <param name="data">Data to log.</param>
public void LogError(object data) => Log(LogLevel.Error, data); public void LogError(object data) => Log(LogLevel.Error, data);
/// <summary>
/// Logs an interpolated string with <see cref="LogLevel.Error" /> level.
/// </summary>
/// <param name="logHandler">Handler for the interpolated string.</param>
public void LogError(BepInExErrorLogInterpolatedStringHandler logHandler) => Log(LogLevel.Error, logHandler);
/// <summary> /// <summary>
/// Logs a message with <see cref="LogLevel.Warning" /> level. /// Logs a message with <see cref="LogLevel.Warning" /> level.
/// </summary> /// </summary>
/// <param name="data">Data to log.</param> /// <param name="data">Data to log.</param>
public void LogWarning(object data) => Log(LogLevel.Warning, data); public void LogWarning(object data) => Log(LogLevel.Warning, data);
/// <summary>
/// Logs an interpolated string with <see cref="LogLevel.Warning" /> level.
/// </summary>
/// <param name="logHandler">Handler for the interpolated string.</param>
public void LogWarning(BepInExWarningLogInterpolatedStringHandler logHandler) => Log(LogLevel.Warning, logHandler);
/// <summary> /// <summary>
/// Logs a message with <see cref="LogLevel.Message" /> level. /// Logs a message with <see cref="LogLevel.Message" /> level.
/// </summary> /// </summary>
/// <param name="data">Data to log.</param> /// <param name="data">Data to log.</param>
public void LogMessage(object data) => Log(LogLevel.Message, data); public void LogMessage(object data) => Log(LogLevel.Message, data);
/// <summary>
/// Logs an interpolated string with <see cref="LogLevel.Message" /> level.
/// </summary>
/// <param name="logHandler">Handler for the interpolated string.</param>
public void LogMessage(BepInExMessageLogInterpolatedStringHandler logHandler) => Log(LogLevel.Message, logHandler);
/// <summary> /// <summary>
/// Logs a message with <see cref="LogLevel.Info" /> level. /// Logs a message with <see cref="LogLevel.Info" /> level.
/// </summary> /// </summary>
/// <param name="data">Data to log.</param> /// <param name="data">Data to log.</param>
public void LogInfo(object data) => Log(LogLevel.Info, data); public void LogInfo(object data) => Log(LogLevel.Info, data);
/// <summary>
/// Logs an interpolated string with <see cref="LogLevel.Info" /> level.
/// </summary>
/// <param name="logHandler">Handler for the interpolated string.</param>
public void LogInfo(BepInExInfoLogInterpolatedStringHandler logHandler) => Log(LogLevel.Info, logHandler);
/// <summary> /// <summary>
/// Logs a message with <see cref="LogLevel.Debug" /> level. /// Logs a message with <see cref="LogLevel.Debug" /> level.
/// </summary> /// </summary>
/// <param name="data">Data to log.</param> /// <param name="data">Data to log.</param>
public void LogDebug(object data) => Log(LogLevel.Debug, data); public void LogDebug(object data) => Log(LogLevel.Debug, data);
/// <summary>
/// Logs an interpolated string with <see cref="LogLevel.Debug" /> level.
/// </summary>
/// <param name="logHandler">Handler for the interpolated string.</param>
public void LogDebug(BepInExDebugLogInterpolatedStringHandler logHandler) => Log(LogLevel.Debug, logHandler);
} }
+2 -2
View File
@@ -48,13 +48,13 @@ public class TraceLogSource : TraceListener
/// Writes a message to the underlying <see cref="ManualLogSource" /> instance. /// Writes a message to the underlying <see cref="ManualLogSource" /> instance.
/// </summary> /// </summary>
/// <param name="message">The message to write.</param> /// <param name="message">The message to write.</param>
public override void Write(string message) => LogSource.LogInfo(message); public override void Write(string message) => LogSource.Log(LogLevel.Info, message);
/// <summary> /// <summary>
/// Writes a message and a newline to the underlying <see cref="ManualLogSource" /> instance. /// Writes a message and a newline to the underlying <see cref="ManualLogSource" /> instance.
/// </summary> /// </summary>
/// <param name="message">The message to write.</param> /// <param name="message">The message to write.</param>
public override void WriteLine(string message) => LogSource.LogInfo(message); public override void WriteLine(string message) => LogSource.Log(LogLevel.Info, message);
/// <inheritdoc /> /// <inheritdoc />
public override void TraceEvent(TraceEventCache eventCache, public override void TraceEvent(TraceEventCache eventCache,
+4 -4
View File
@@ -104,7 +104,7 @@ public class FastNativeDetour : IDetour
{ {
debuggerLogSource debuggerLogSource
.LogDebug($"Detouring 0x{OriginalFunctionPtr.ToString("X")} -> 0x{DetourFunctionPtr.ToString("X")}"); .LogDebug($"Detouring 0x{OriginalFunctionPtr.ToString("X")} -> 0x{DetourFunctionPtr.ToString("X")}");
debuggerLogSource.LogDebug("Original (32) asm"); debuggerLogSource.Log(LogLevel.Debug, "Original (32) asm");
DetourGenerator.Disassemble(debuggerLogSource, OriginalFunctionPtr, 32); DetourGenerator.Disassemble(debuggerLogSource, OriginalFunctionPtr, 32);
} }
@@ -116,10 +116,10 @@ public class FastNativeDetour : IDetour
if (debuggerLogSource != null) if (debuggerLogSource != null)
{ {
debuggerLogSource.LogDebug($"Trampoline allocation: 0x{TrampolinePtr.ToString("X")}"); debuggerLogSource.Log(LogLevel.Debug, $"Trampoline allocation: 0x{TrampolinePtr.ToString("X")}");
debuggerLogSource.LogDebug("Modified (32) asm"); debuggerLogSource.Log(LogLevel.Debug, "Modified (32) asm");
DetourGenerator.Disassemble(debuggerLogSource, OriginalFunctionPtr, 32); DetourGenerator.Disassemble(debuggerLogSource, OriginalFunctionPtr, 32);
debuggerLogSource.LogDebug($"Trampoline ({trampolineLength}) asm"); debuggerLogSource.Log(LogLevel.Debug, $"Trampoline ({trampolineLength}) asm");
DetourGenerator.Disassemble(debuggerLogSource, TrampolinePtr, trampolineLength); DetourGenerator.Disassemble(debuggerLogSource, TrampolinePtr, trampolineLength);
} }
@@ -275,7 +275,7 @@ public unsafe class IL2CPPDetourMethodPatcher : MethodPatcher
return dmd; return dmd;
} }
private static void ReportException(Exception ex) => DetourLogger.LogError(ex.ToString()); private static void ReportException(Exception ex) => DetourLogger.Log(LogLevel.Error, ex.ToString());
private static Type ConvertManagedTypeToIL2CPPType(Type managedType) private static Type ConvertManagedTypeToIL2CPPType(Type managedType)
{ {
+1 -1
View File
@@ -91,7 +91,7 @@ public class IL2CPPChainloader : BaseChainloader<BasePlugin>
Logger.Log(LogLevel.Debug, "Initializing TLS adapters"); Logger.Log(LogLevel.Debug, "Initializing TLS adapters");
Il2CppTlsAdapter.Initialize(); Il2CppTlsAdapter.Initialize();
PreloaderLogger.Log.LogDebug("Runtime invoke patched"); PreloaderLogger.Log.Log(LogLevel.Debug, "Runtime invoke patched");
} }
private void OnInstallUnityTlsInterface(IntPtr unityTlsInterfaceStruct) private void OnInstallUnityTlsInterface(IntPtr unityTlsInterfaceStruct)
+4 -4
View File
@@ -91,7 +91,7 @@ public static class Preloader
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.LogFatal(ex); Log.Log(LogLevel.Fatal, ex);
throw; throw;
} }
@@ -100,11 +100,11 @@ public static class Preloader
private static void InitializeUnityVersion() private static void InitializeUnityVersion()
{ {
if (TryInitializeUnityVersion(ConfigUnityVersion.Value)) if (TryInitializeUnityVersion(ConfigUnityVersion.Value))
Log.LogWarning("Unity version obtained from the config."); Log.Log(LogLevel.Warning, "Unity version obtained from the config.");
else if (TryInitializeUnityVersion(Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion)) else if (TryInitializeUnityVersion(Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion))
Log.LogDebug("Unity version obtained from main application module."); Log.Log(LogLevel.Debug, "Unity version obtained from main application module.");
else else
Log.LogError("Running under default Unity version. UnityVersionHandler is not initialized."); Log.Log(LogLevel.Error, "Running under default Unity version. UnityVersionHandler is not initialized.");
} }
private static bool TryInitializeUnityVersion(string version) private static bool TryInitializeUnityVersion(string version)
@@ -200,7 +200,8 @@ public class AssemblyPatcher : IDisposable
} }
catch (Exception e) catch (Exception e)
{ {
Logger.LogError($"Failed to load patchers from type [{patcherPlugin.TypeName}]: {(e is ReflectionTypeLoadException re ? TypeLoader.TypeLoadExceptionToString(re) : e)}"); Logger.Log(LogLevel.Error,
$"Failed to load patchers from type [{patcherPlugin.TypeName}]: {(e is ReflectionTypeLoadException re ? TypeLoader.TypeLoadExceptionToString(re) : e.ToString())}");
} }
var assName = ass.GetName(); var assName = ass.GetName();