(client) feat:UI更新 chore:LogUI性能更好,并且修复反复打开Log消失的bug,删除部分非预期的警告
This commit is contained in:
@ -14,14 +14,21 @@ namespace Logging
|
||||
public string Message;
|
||||
public string StackTrace;
|
||||
|
||||
public override string ToString() =>
|
||||
$"[{Timestamp:HH:mm:ss}] [{Type}] {Message}" +
|
||||
(Type == LogType.Exception ? $"\n{StackTrace}" : "");
|
||||
public override string ToString()
|
||||
{
|
||||
// 逻辑修改:扩展条件,使LogType.Error和LogType.Assert也能显示堆栈信息
|
||||
if (Type == LogType.Exception || Type == LogType.Error || Type == LogType.Assert)
|
||||
{
|
||||
return $"[{Timestamp:HH:mm:ss}] [{Type}] {Message}\n{StackTrace}";
|
||||
}
|
||||
return $"[{Timestamp:HH:mm:ss}] [{Type}] {Message}";
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Queue<LogEntry> _logs = new Queue<LogEntry>();
|
||||
private static readonly object _lock = new object(); // 线程锁
|
||||
private static int _maxLogs = 1000; // 默认容量
|
||||
private static bool _isInitialized = false; // 逻辑修改:添加一个私有标志来跟踪是否已初始化
|
||||
|
||||
// 最大日志容量属性
|
||||
public static int MaxLogs
|
||||
@ -35,10 +42,40 @@ namespace Logging
|
||||
}
|
||||
}
|
||||
|
||||
// 逻辑修改:添加 [RuntimeInitializeOnLoadMethod] 特性,确保 Init() 方法在 Unity 启动时自动执行
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
|
||||
public static void Init()
|
||||
{
|
||||
Application.logMessageReceivedThreaded += HandleLog;
|
||||
|
||||
// 确保线程安全地检查和设置初始化状态
|
||||
lock (_lock)
|
||||
{
|
||||
if (_isInitialized)
|
||||
{
|
||||
Debug.LogWarning("[LogCapturer] Init() called multiple times. LogCapturer is already initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
Application.logMessageReceivedThreaded += HandleLog;
|
||||
_isInitialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 逻辑修改:添加一个 Shutdown 方法来解除事件注册和清理
|
||||
public static void Shutdown()
|
||||
{
|
||||
// 确保线程安全地检查和解除注册
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_isInitialized)
|
||||
{
|
||||
Debug.LogWarning("[LogCapturer] Shutdown() called but LogCapturer was not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
Application.logMessageReceivedThreaded -= HandleLog;
|
||||
_isInitialized = false;
|
||||
_logs.Clear(); // 逻辑修改:在关闭时清空所有捕获的日志,确保下次启用时是全新状态。
|
||||
}
|
||||
}
|
||||
|
||||
// 日志处理回调
|
||||
@ -88,4 +125,4 @@ namespace Logging
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user