mirror of
https://github.com/BepInEx/BepInEx.git
synced 2026-10-20 01:46:13 +00:00
Add log source for Harmony logs
This commit is contained in:
1 parent
1b538fa624
commit
055eac2d4f
5 files changed
+62
-2
No files matched your search
@@ -10,7 +10,6 @@ using BepInEx.Preloader.RuntimeFixes;
|
||||
using HarmonyLib;
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Cil;
|
||||
using MonoMod.RuntimeDetour;
|
||||
using MonoMod.Utils;
|
||||
using MethodAttributes = Mono.Cecil.MethodAttributes;
|
||||
|
||||
@@ -41,6 +40,7 @@ namespace BepInEx.Preloader
|
||||
UnityPatches.Apply();
|
||||
}, out var runtimePatchException);
|
||||
|
||||
Logger.InitializeInternalLoggers();
|
||||
Logger.Sources.Add(TraceLogSource.CreateSource());
|
||||
|
||||
PreloaderLog = new PreloaderConsoleListener(ConfigPreloaderCOutLogging.Value);
|
||||
|
||||
@@ -102,6 +102,7 @@
|
||||
<Compile Include="Bootstrap\Chainloader.cs" />
|
||||
<Compile Include="Contract\BaseUnityPlugin.cs" />
|
||||
<Compile Include="Logging\DiskLogListener.cs" />
|
||||
<Compile Include="Logging\HarmonyLogSource.cs" />
|
||||
<Compile Include="Logging\LogEventArgs.cs" />
|
||||
<Compile Include="Logging\Logger.cs" />
|
||||
<Compile Include="Logging\LogLevel.cs" />
|
||||
|
||||
@@ -80,7 +80,8 @@ namespace BepInEx.Bootstrap
|
||||
ConsoleManager.SetConsoleStreams();
|
||||
ConsoleManager.SetConsoleEncoding();
|
||||
}
|
||||
|
||||
|
||||
Logger.InitializeInternalLoggers();
|
||||
Logger.Listeners.Add(new UnityLogListener());
|
||||
|
||||
if (ConfigDiskLogging.Value)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using BepInEx.Configuration;
|
||||
using HarmonyLogger = HarmonyLib.Tools.Logger;
|
||||
|
||||
namespace BepInEx.Logging
|
||||
{
|
||||
internal class HarmonyLogSource : ILogSource
|
||||
{
|
||||
private static readonly ConfigEntry<HarmonyLogger.LogChannel> LogChannels = ConfigFile.CoreConfig.Bind(
|
||||
"Harmony.Logger",
|
||||
"LogChannels",
|
||||
HarmonyLogger.LogChannel.Warn | HarmonyLogger.LogChannel.Error,
|
||||
"Specifies which Harmony log channels to listen to.\nNOTE: IL channel dumps the whole patch methods, use only when needed!");
|
||||
|
||||
private static readonly Dictionary<HarmonyLogger.LogChannel, LogLevel> LevelMap = new Dictionary<HarmonyLogger.LogChannel, LogLevel>
|
||||
{
|
||||
[HarmonyLogger.LogChannel.Info] = LogLevel.Info,
|
||||
[HarmonyLogger.LogChannel.Warn] = LogLevel.Warning,
|
||||
[HarmonyLogger.LogChannel.Error] = LogLevel.Error,
|
||||
[HarmonyLogger.LogChannel.IL] = LogLevel.Debug
|
||||
};
|
||||
|
||||
public HarmonyLogSource()
|
||||
{
|
||||
HarmonyLogger.ChannelFilter = LogChannels.Value;
|
||||
HarmonyLogger.MessageReceived += HandleHarmonyMessage;
|
||||
}
|
||||
|
||||
private void HandleHarmonyMessage(object sender, HarmonyLib.Tools.Logger.LogEventArgs e)
|
||||
{
|
||||
if (!LevelMap.TryGetValue(e.LogChannel, out var level))
|
||||
return;
|
||||
|
||||
LogEvent?.Invoke(this, new LogEventArgs(e.Message, level, this));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
HarmonyLogger.MessageReceived -= HandleHarmonyMessage;
|
||||
}
|
||||
|
||||
public string SourceName { get; } = "Harmony";
|
||||
public event EventHandler<LogEventArgs> LogEvent;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,18 @@ namespace BepInEx.Logging
|
||||
|
||||
private static readonly ManualLogSource InternalLogSource = CreateLogSource("BepInEx");
|
||||
|
||||
private static bool internalLogsInitialized;
|
||||
|
||||
internal static void InitializeInternalLoggers()
|
||||
{
|
||||
if (internalLogsInitialized)
|
||||
return;
|
||||
|
||||
Sources.Add(new HarmonyLogSource());
|
||||
|
||||
internalLogsInitialized = true;
|
||||
}
|
||||
|
||||
internal static void InternalLogEvent(object sender, LogEventArgs eventArgs)
|
||||
{
|
||||
foreach (var listener in Listeners)
|
||||
|
||||
Reference in new issue
Block a user