Skip to content

Commit 3fd5d68

Browse files
committed
ManualLogSource: Add string interpolation for Log* overloads; clean up logging
1 parent 4bb48c5 commit 3fd5d68

File tree

9 files changed

+126
-17
lines changed

9 files changed

+126
-17
lines changed

BepInEx.Core/Logging/BepInExLogInterpolatedStringHandler.cs

+67-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Text;
44
using BepInEx.Logging;
55

6-
namespace BepInEx.Core.Logging;
6+
namespace BepInEx.Core.Logging.Interpolation;
77

88
/// <summary>
99
/// Interpolated string handler for BepInEx <see cref="Logger" />. This allows to conditionally skip logging certain
@@ -99,3 +99,69 @@ public void AppendFormatted(IntPtr t, string format)
9999
/// <inheritdoc />
100100
public override string ToString() => sb?.ToString() ?? string.Empty;
101101
}
102+
103+
/// <inheritdoc />
104+
[InterpolatedStringHandler]
105+
public class BepInExFatalLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
106+
{
107+
/// <inheritdoc />
108+
public BepInExFatalLogInterpolatedStringHandler(int literalLength,
109+
int formattedCount,
110+
out bool isEnabled) : base(literalLength, formattedCount,
111+
LogLevel.Fatal, out isEnabled) { }
112+
}
113+
114+
/// <inheritdoc />
115+
[InterpolatedStringHandler]
116+
public class BepInExErrorLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
117+
{
118+
/// <inheritdoc />
119+
public BepInExErrorLogInterpolatedStringHandler(int literalLength,
120+
int formattedCount,
121+
out bool isEnabled) : base(literalLength, formattedCount,
122+
LogLevel.Error, out isEnabled) { }
123+
}
124+
125+
/// <inheritdoc />
126+
[InterpolatedStringHandler]
127+
public class BepInExWarningLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
128+
{
129+
/// <inheritdoc />
130+
public BepInExWarningLogInterpolatedStringHandler(int literalLength,
131+
int formattedCount,
132+
out bool isEnabled) : base(literalLength, formattedCount,
133+
LogLevel.Warning, out isEnabled) { }
134+
}
135+
136+
/// <inheritdoc />
137+
[InterpolatedStringHandler]
138+
public class BepInExMessageLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
139+
{
140+
/// <inheritdoc />
141+
public BepInExMessageLogInterpolatedStringHandler(int literalLength,
142+
int formattedCount,
143+
out bool isEnabled) : base(literalLength, formattedCount,
144+
LogLevel.Message, out isEnabled) { }
145+
}
146+
147+
/// <inheritdoc />
148+
[InterpolatedStringHandler]
149+
public class BepInExInfoLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
150+
{
151+
/// <inheritdoc />
152+
public BepInExInfoLogInterpolatedStringHandler(int literalLength,
153+
int formattedCount,
154+
out bool isEnabled) : base(literalLength, formattedCount,
155+
LogLevel.Info, out isEnabled) { }
156+
}
157+
158+
/// <inheritdoc />
159+
[InterpolatedStringHandler]
160+
public class BepInExDebugLogInterpolatedStringHandler : BepInExLogInterpolatedStringHandler
161+
{
162+
/// <inheritdoc />
163+
public BepInExDebugLogInterpolatedStringHandler(int literalLength,
164+
int formattedCount,
165+
out bool isEnabled) : base(literalLength, formattedCount,
166+
LogLevel.Debug, out isEnabled) { }
167+
}

BepInEx.Core/Logging/Logger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Runtime.CompilerServices;
4-
using BepInEx.Core.Logging;
4+
using BepInEx.Core.Logging.Interpolation;
55

66
namespace BepInEx.Logging;
77

BepInEx.Core/Logging/ManualLogSource.cs

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Runtime.CompilerServices;
3-
using BepInEx.Core.Logging;
3+
using BepInEx.Core.Logging.Interpolation;
44

55
namespace BepInEx.Logging;
66

@@ -34,8 +34,14 @@ public void Dispose() { }
3434
/// <param name="data">Data to log.</param>
3535
public void Log(LogLevel level, object data) => LogEvent?.Invoke(this, new LogEventArgs(data, level, this));
3636

37+
/// <summary>
38+
/// Logs an interpolated string with the specified log level.
39+
/// </summary>
40+
/// <param name="level">Log levels to attach to the message. Multiple can be used with bitwise ORing.</param>
41+
/// <param name="logHandler">Handler for the interpolated string.</param>
3742
public void Log(LogLevel level,
38-
[InterpolatedStringHandlerArgument("level")] BepInExLogInterpolatedStringHandler logHandler)
43+
[InterpolatedStringHandlerArgument("level")]
44+
BepInExLogInterpolatedStringHandler logHandler)
3945
{
4046
if (logHandler.Enabled)
4147
LogEvent?.Invoke(this, new LogEventArgs(logHandler.ToString(), level, this));
@@ -47,33 +53,69 @@ public void Log(LogLevel level,
4753
/// <param name="data">Data to log.</param>
4854
public void LogFatal(object data) => Log(LogLevel.Fatal, data);
4955

56+
/// <summary>
57+
/// Logs an interpolated string with <see cref="LogLevel.Fatal" /> level.
58+
/// </summary>
59+
/// <param name="logHandler">Handler for the interpolated string.</param>
60+
public void LogFatal(BepInExFatalLogInterpolatedStringHandler logHandler) => Log(LogLevel.Fatal, logHandler);
61+
5062
/// <summary>
5163
/// Logs a message with <see cref="LogLevel.Error" /> level.
5264
/// </summary>
5365
/// <param name="data">Data to log.</param>
5466
public void LogError(object data) => Log(LogLevel.Error, data);
5567

68+
/// <summary>
69+
/// Logs an interpolated string with <see cref="LogLevel.Error" /> level.
70+
/// </summary>
71+
/// <param name="logHandler">Handler for the interpolated string.</param>
72+
public void LogError(BepInExErrorLogInterpolatedStringHandler logHandler) => Log(LogLevel.Error, logHandler);
73+
5674
/// <summary>
5775
/// Logs a message with <see cref="LogLevel.Warning" /> level.
5876
/// </summary>
5977
/// <param name="data">Data to log.</param>
6078
public void LogWarning(object data) => Log(LogLevel.Warning, data);
6179

80+
/// <summary>
81+
/// Logs an interpolated string with <see cref="LogLevel.Warning" /> level.
82+
/// </summary>
83+
/// <param name="logHandler">Handler for the interpolated string.</param>
84+
public void LogWarning(BepInExWarningLogInterpolatedStringHandler logHandler) => Log(LogLevel.Warning, logHandler);
85+
6286
/// <summary>
6387
/// Logs a message with <see cref="LogLevel.Message" /> level.
6488
/// </summary>
6589
/// <param name="data">Data to log.</param>
6690
public void LogMessage(object data) => Log(LogLevel.Message, data);
6791

92+
/// <summary>
93+
/// Logs an interpolated string with <see cref="LogLevel.Message" /> level.
94+
/// </summary>
95+
/// <param name="logHandler">Handler for the interpolated string.</param>
96+
public void LogMessage(BepInExMessageLogInterpolatedStringHandler logHandler) => Log(LogLevel.Message, logHandler);
97+
6898
/// <summary>
6999
/// Logs a message with <see cref="LogLevel.Info" /> level.
70100
/// </summary>
71101
/// <param name="data">Data to log.</param>
72102
public void LogInfo(object data) => Log(LogLevel.Info, data);
73103

104+
/// <summary>
105+
/// Logs an interpolated string with <see cref="LogLevel.Info" /> level.
106+
/// </summary>
107+
/// <param name="logHandler">Handler for the interpolated string.</param>
108+
public void LogInfo(BepInExInfoLogInterpolatedStringHandler logHandler) => Log(LogLevel.Info, logHandler);
109+
74110
/// <summary>
75111
/// Logs a message with <see cref="LogLevel.Debug" /> level.
76112
/// </summary>
77113
/// <param name="data">Data to log.</param>
78114
public void LogDebug(object data) => Log(LogLevel.Debug, data);
115+
116+
/// <summary>
117+
/// Logs an interpolated string with <see cref="LogLevel.Debug" /> level.
118+
/// </summary>
119+
/// <param name="logHandler">Handler for the interpolated string.</param>
120+
public void LogDebug(BepInExDebugLogInterpolatedStringHandler logHandler) => Log(LogLevel.Debug, logHandler);
79121
}

BepInEx.Core/Logging/TraceLogSource.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public static ILogSource CreateSource()
4848
/// Writes a message to the underlying <see cref="ManualLogSource" /> instance.
4949
/// </summary>
5050
/// <param name="message">The message to write.</param>
51-
public override void Write(string message) => LogSource.LogInfo(message);
51+
public override void Write(string message) => LogSource.Log(LogLevel.Info, message);
5252

5353
/// <summary>
5454
/// Writes a message and a newline to the underlying <see cref="ManualLogSource" /> instance.
5555
/// </summary>
5656
/// <param name="message">The message to write.</param>
57-
public override void WriteLine(string message) => LogSource.LogInfo(message);
57+
public override void WriteLine(string message) => LogSource.Log(LogLevel.Info, message);
5858

5959
/// <inheritdoc />
6060
public override void TraceEvent(TraceEventCache eventCache,

BepInEx.IL2CPP/Hook/FastNativeDetour.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void Apply(ManualLogSource debuggerLogSource)
104104
{
105105
debuggerLogSource
106106
.LogDebug($"Detouring 0x{OriginalFunctionPtr.ToString("X")} -> 0x{DetourFunctionPtr.ToString("X")}");
107-
debuggerLogSource.LogDebug("Original (32) asm");
107+
debuggerLogSource.Log(LogLevel.Debug, "Original (32) asm");
108108
DetourGenerator.Disassemble(debuggerLogSource, OriginalFunctionPtr, 32);
109109
}
110110

@@ -116,10 +116,10 @@ public void Apply(ManualLogSource debuggerLogSource)
116116

117117
if (debuggerLogSource != null)
118118
{
119-
debuggerLogSource.LogDebug($"Trampoline allocation: 0x{TrampolinePtr.ToString("X")}");
120-
debuggerLogSource.LogDebug("Modified (32) asm");
119+
debuggerLogSource.Log(LogLevel.Debug, $"Trampoline allocation: 0x{TrampolinePtr.ToString("X")}");
120+
debuggerLogSource.Log(LogLevel.Debug, "Modified (32) asm");
121121
DetourGenerator.Disassemble(debuggerLogSource, OriginalFunctionPtr, 32);
122-
debuggerLogSource.LogDebug($"Trampoline ({trampolineLength}) asm");
122+
debuggerLogSource.Log(LogLevel.Debug, $"Trampoline ({trampolineLength}) asm");
123123
DetourGenerator.Disassemble(debuggerLogSource, TrampolinePtr, trampolineLength);
124124
}
125125

BepInEx.IL2CPP/Hook/IL2CPPDetourMethodPatcher.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private DynamicMethodDefinition GenerateNativeToManagedTrampoline(MethodInfo tar
275275
return dmd;
276276
}
277277

278-
private static void ReportException(Exception ex) => DetourLogger.LogError(ex.ToString());
278+
private static void ReportException(Exception ex) => DetourLogger.Log(LogLevel.Error, ex.ToString());
279279

280280
private static Type ConvertManagedTypeToIL2CPPType(Type managedType)
281281
{

BepInEx.IL2CPP/IL2CPPChainloader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public override void Initialize(string gameExePath = null)
9191
Logger.Log(LogLevel.Debug, "Initializing TLS adapters");
9292
Il2CppTlsAdapter.Initialize();
9393

94-
PreloaderLogger.Log.LogDebug("Runtime invoke patched");
94+
PreloaderLogger.Log.Log(LogLevel.Debug, "Runtime invoke patched");
9595
}
9696

9797
private void OnInstallUnityTlsInterface(IntPtr unityTlsInterfaceStruct)

BepInEx.IL2CPP/Preloader.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static void Run()
9191
}
9292
catch (Exception ex)
9393
{
94-
Log.LogFatal(ex);
94+
Log.Log(LogLevel.Fatal, ex);
9595

9696
throw;
9797
}
@@ -100,11 +100,11 @@ public static void Run()
100100
private static void InitializeUnityVersion()
101101
{
102102
if (TryInitializeUnityVersion(ConfigUnityVersion.Value))
103-
Log.LogWarning("Unity version obtained from the config.");
103+
Log.Log(LogLevel.Warning, "Unity version obtained from the config.");
104104
else if (TryInitializeUnityVersion(Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion))
105-
Log.LogDebug("Unity version obtained from main application module.");
105+
Log.Log(LogLevel.Debug, "Unity version obtained from main application module.");
106106
else
107-
Log.LogError("Running under default Unity version. UnityVersionHandler is not initialized.");
107+
Log.Log(LogLevel.Error, "Running under default Unity version. UnityVersionHandler is not initialized.");
108108
}
109109

110110
private static bool TryInitializeUnityVersion(string version)

BepInEx.Preloader.Core/Patching/AssemblyPatcher.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ void AddDefinition(PatchDefinition definition)
200200
}
201201
catch (Exception e)
202202
{
203-
Logger.LogError($"Failed to load patchers from type [{patcherPlugin.TypeName}]: {(e is ReflectionTypeLoadException re ? TypeLoader.TypeLoadExceptionToString(re) : e)}");
203+
Logger.Log(LogLevel.Error,
204+
$"Failed to load patchers from type [{patcherPlugin.TypeName}]: {(e is ReflectionTypeLoadException re ? TypeLoader.TypeLoadExceptionToString(re) : e.ToString())}");
204205
}
205206

206207
var assName = ass.GetName();

0 commit comments

Comments
 (0)