Files
BepInEx/Runtimes/Unity/BepInEx.Unity.IL2CPP/Logging/IL2CPPUnityLogSource.cs
T
3448ef75c2 Log Unity stack traces on IL2CPP (#1424)
UnityLogCallback ignored its second parameter, so Unity exceptions and
errors logged only their message. Append the trace for Exception and Error
when Unity gives one. The Mono source already does this for Exception. Log
and Warning stay as they are, since in the game I tested Unity passed a
trace for most of those.

Fixes #660

Co-authored-by: mirfalltnixein <22786734+mirfalltnixein@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 19:33:09 +01:00

43 lines
1.3 KiB
C#

using System;
using BepInEx.Logging;
using UnityEngine;
namespace BepInEx.Unity.IL2CPP.Logging;
public class IL2CPPUnityLogSource : ILogSource
{
public IL2CPPUnityLogSource()
{
Application.s_LogCallbackHandler = new Action<string, string, LogType>(UnityLogCallback);
Il2CppInterop.Runtime.IL2CPP
.ResolveICall<
SetLogCallbackDefinedDelegate>("UnityEngine.Application::SetLogCallbackDefined")(true);
}
public string SourceName { get; } = "Unity";
public event EventHandler<LogEventArgs> LogEvent;
public void Dispose() { }
public void UnityLogCallback(string logLine, string exception, LogType type)
{
var level = type switch
{
LogType.Error => LogLevel.Error,
LogType.Assert => LogLevel.Debug,
LogType.Warning => LogLevel.Warning,
LogType.Log => LogLevel.Message,
LogType.Exception => LogLevel.Error,
_ => LogLevel.Message
};
if (type is LogType.Exception or LogType.Error && !string.IsNullOrEmpty(exception))
logLine += $"\nStack trace:\n{exception}";
LogEvent?.Invoke(this, new LogEventArgs(logLine, level, this));
}
private delegate IntPtr SetLogCallbackDefinedDelegate(bool defined);
}