Skip to content

Commit

Permalink
Add log source for Harmony logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ghorsington committed Jun 3, 2020
1 parent 1b538fa commit 055eac2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion BepInEx.Preloader/Preloader.cs
Expand Up @@ -10,7 +10,6 @@
using HarmonyLib;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MonoMod.RuntimeDetour;
using MonoMod.Utils;
using MethodAttributes = Mono.Cecil.MethodAttributes;

Expand Down Expand Up @@ -41,6 +40,7 @@ public static void Run()
UnityPatches.Apply();
}, out var runtimePatchException);

Logger.InitializeInternalLoggers();
Logger.Sources.Add(TraceLogSource.CreateSource());

PreloaderLog = new PreloaderConsoleListener(ConfigPreloaderCOutLogging.Value);
Expand Down
1 change: 1 addition & 0 deletions BepInEx/BepInEx.csproj
Expand Up @@ -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" />
Expand Down
3 changes: 2 additions & 1 deletion BepInEx/Bootstrap/Chainloader.cs
Expand Up @@ -80,7 +80,8 @@ public static void Initialize(string gameExePath, bool startConsole = true, ICol
ConsoleManager.SetConsoleStreams();
ConsoleManager.SetConsoleEncoding();
}


Logger.InitializeInternalLoggers();
Logger.Listeners.Add(new UnityLogListener());

if (ConfigDiskLogging.Value)
Expand Down
46 changes: 46 additions & 0 deletions 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 changes: 12 additions & 0 deletions BepInEx/Logging/Logger.cs
Expand Up @@ -14,6 +14,18 @@ public static class Logger

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)
Expand Down

0 comments on commit 055eac2

Please sign in to comment.