Explorar o código

Add log source for Harmony logs

ghorsington %!s(int64=4) %!d(string=hai) anos
pai
achega
055eac2d4f

+ 1 - 1
BepInEx.Preloader/Preloader.cs

@@ -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);

+ 1 - 0
BepInEx/BepInEx.csproj

@@ -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" />

+ 2 - 1
BepInEx/Bootstrap/Chainloader.cs

@@ -80,7 +80,8 @@ namespace BepInEx.Bootstrap
 				ConsoleManager.SetConsoleStreams();
 				ConsoleManager.SetConsoleEncoding();
 			}
-
+			
+			Logger.InitializeInternalLoggers();
 			Logger.Listeners.Add(new UnityLogListener());
 
 			if (ConfigDiskLogging.Value)

+ 46 - 0
BepInEx/Logging/HarmonyLogSource.cs

@@ -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;
+	}
+}

+ 12 - 0
BepInEx/Logging/Logger.cs

@@ -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)