Skip to content

Commit

Permalink
Fix ZLoggerUnityDebugLoggerProvider is not thread safe #170
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jul 17, 2024
1 parent 38dad79 commit d91c8a6
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void Post(IZLoggerEntry log)
var msg = FormatToString(log, formatter);
var unityLogType = log.LogInfo.AsUnityLogType();

if (UnityEngine.Application.GetStackTraceLogType(unityLogType) != StackTraceLogType.None && options.PrettyStacktrace)
if (LogTypeMapper.GetStackTraceLogType(unityLogType) != StackTraceLogType.None && options.PrettyStacktrace)
{
var stacktrace = new StackTrace(5, true);
msg = $"{msg}{Environment.NewLine}{DiagnosticsHelper.CleanupStackTrace(stacktrace)}{Environment.NewLine}---";
Expand Down Expand Up @@ -176,4 +176,30 @@ public void SetScopeProvider(IExternalScopeProvider scopeProvider)
this.scopeProvider = scopeProvider;
}
}

static class LogTypeMapper
{
static StackTraceLogType[]? stackTraceLogTypes = default!;

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Init()
{
var logTypes = new[] { LogType.Error, LogType.Assert, LogType.Warning, LogType.Log, LogType.Exception };
stackTraceLogTypes = new StackTraceLogType[logTypes.Length];
for (int i = 0; i < logTypes.Length; i++)
{
stackTraceLogTypes[i] = UnityEngine.Application.GetStackTraceLogType(logTypes[i]);
}
}

public static StackTraceLogType GetStackTraceLogType(LogType logType)
{
var i = (int)logType;
if (stackTraceLogTypes != null && i >= 0 && i < stackTraceLogTypes.Length)
{
return stackTraceLogTypes[i];
}
return StackTraceLogType.None;
}
}
}

0 comments on commit d91c8a6

Please sign in to comment.