mirror of
https://github.com/BepInEx/BepInEx.git
synced 2026-10-19 15:51:45 +00:00
ManualLogSource: Add string interpolation for Log* overloads; clean up logging
This commit is contained in:
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 BepInEx.Logging;
|
||||
|
||||
namespace BepInEx.Core.Logging;
|
||||
namespace BepInEx.Core.Logging.Interpolation;
|
||||
|
||||
/// <summary>
|
||||
/// Interpolated string handler for BepInEx <see cref="Logger" />. This allows to conditionally skip logging certain
|
||||
@@ -99,3 +99,69 @@ public class BepInExLogInterpolatedStringHandler
|
||||
/// <inheritdoc />
|
||||
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,7 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using BepInEx.Core.Logging;
|
||||
using BepInEx.Core.Logging.Interpolation;
|
||||
|
||||
namespace BepInEx.Logging;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using BepInEx.Core.Logging;
|
||||
using BepInEx.Core.Logging.Interpolation;
|
||||
|
||||
namespace BepInEx.Logging;
|
||||
|
||||
@@ -34,8 +34,14 @@ public class ManualLogSource : ILogSource
|
||||
/// <param name="data">Data to log.</param>
|
||||
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,
|
||||
[InterpolatedStringHandlerArgument("level")] BepInExLogInterpolatedStringHandler logHandler)
|
||||
[InterpolatedStringHandlerArgument("level")]
|
||||
BepInExLogInterpolatedStringHandler logHandler)
|
||||
{
|
||||
if (logHandler.Enabled)
|
||||
LogEvent?.Invoke(this, new LogEventArgs(logHandler.ToString(), level, this));
|
||||
@@ -47,33 +53,69 @@ public class ManualLogSource : ILogSource
|
||||
/// <param name="data">Data to log.</param>
|
||||
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>
|
||||
/// Logs a message with <see cref="LogLevel.Error" /> level.
|
||||
/// </summary>
|
||||
/// <param name="data">Data to log.</param>
|
||||
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>
|
||||
/// Logs a message with <see cref="LogLevel.Warning" /> level.
|
||||
/// </summary>
|
||||
/// <param name="data">Data to log.</param>
|
||||
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>
|
||||
/// Logs a message with <see cref="LogLevel.Message" /> level.
|
||||
/// </summary>
|
||||
/// <param name="data">Data to log.</param>
|
||||
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>
|
||||
/// Logs a message with <see cref="LogLevel.Info" /> level.
|
||||
/// </summary>
|
||||
/// <param name="data">Data to log.</param>
|
||||
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>
|
||||
/// Logs a message with <see cref="LogLevel.Debug" /> level.
|
||||
/// </summary>
|
||||
/// <param name="data">Data to log.</param>
|
||||
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);
|
||||
}
|
||||
@@ -48,13 +48,13 @@ public class TraceLogSource : TraceListener
|
||||
/// Writes a message to the underlying <see cref="ManualLogSource" /> instance.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// Writes a message and a newline to the underlying <see cref="ManualLogSource" /> instance.
|
||||
/// </summary>
|
||||
/// <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 />
|
||||
public override void TraceEvent(TraceEventCache eventCache,
|
||||
|
||||
@@ -104,7 +104,7 @@ public class FastNativeDetour : IDetour
|
||||
{
|
||||
debuggerLogSource
|
||||
.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);
|
||||
}
|
||||
|
||||
@@ -116,10 +116,10 @@ public class FastNativeDetour : IDetour
|
||||
|
||||
if (debuggerLogSource != null)
|
||||
{
|
||||
debuggerLogSource.LogDebug($"Trampoline allocation: 0x{TrampolinePtr.ToString("X")}");
|
||||
debuggerLogSource.LogDebug("Modified (32) asm");
|
||||
debuggerLogSource.Log(LogLevel.Debug, $"Trampoline allocation: 0x{TrampolinePtr.ToString("X")}");
|
||||
debuggerLogSource.Log(LogLevel.Debug, "Modified (32) asm");
|
||||
DetourGenerator.Disassemble(debuggerLogSource, OriginalFunctionPtr, 32);
|
||||
debuggerLogSource.LogDebug($"Trampoline ({trampolineLength}) asm");
|
||||
debuggerLogSource.Log(LogLevel.Debug, $"Trampoline ({trampolineLength}) asm");
|
||||
DetourGenerator.Disassemble(debuggerLogSource, TrampolinePtr, trampolineLength);
|
||||
}
|
||||
|
||||
|
||||
@@ -275,7 +275,7 @@ public unsafe class IL2CPPDetourMethodPatcher : MethodPatcher
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -91,7 +91,7 @@ public class IL2CPPChainloader : BaseChainloader<BasePlugin>
|
||||
Logger.Log(LogLevel.Debug, "Initializing TLS adapters");
|
||||
Il2CppTlsAdapter.Initialize();
|
||||
|
||||
PreloaderLogger.Log.LogDebug("Runtime invoke patched");
|
||||
PreloaderLogger.Log.Log(LogLevel.Debug, "Runtime invoke patched");
|
||||
}
|
||||
|
||||
private void OnInstallUnityTlsInterface(IntPtr unityTlsInterfaceStruct)
|
||||
|
||||
@@ -91,7 +91,7 @@ public static class Preloader
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.LogFatal(ex);
|
||||
Log.Log(LogLevel.Fatal, ex);
|
||||
|
||||
throw;
|
||||
}
|
||||
@@ -100,11 +100,11 @@ public static class Preloader
|
||||
private static void InitializeUnityVersion()
|
||||
{
|
||||
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))
|
||||
Log.LogDebug("Unity version obtained from main application module.");
|
||||
Log.Log(LogLevel.Debug, "Unity version obtained from main application module.");
|
||||
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)
|
||||
|
||||
@@ -200,7 +200,8 @@ public class AssemblyPatcher : IDisposable
|
||||
}
|
||||
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();
|
||||
|
||||
Reference in new issue
Block a user