(client) feat:窗口新增独占、占用输入属性,添加部分行为树节点和工作类
This commit is contained in:
91
Client/Assets/Scripts/Logging/LogCapturer.cs
Normal file
91
Client/Assets/Scripts/Logging/LogCapturer.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Logging
|
||||
{
|
||||
public static class LogCapturer
|
||||
{
|
||||
// 日志条目结构
|
||||
public struct LogEntry
|
||||
{
|
||||
public DateTime Timestamp;
|
||||
public LogType Type;
|
||||
public string Message;
|
||||
public string StackTrace;
|
||||
|
||||
public override string ToString() =>
|
||||
$"[{Timestamp:HH:mm:ss}] [{Type}] {Message}" +
|
||||
(Type == LogType.Exception ? $"\n{StackTrace}" : "");
|
||||
}
|
||||
|
||||
private static readonly Queue<LogEntry> _logs = new Queue<LogEntry>();
|
||||
private static readonly object _lock = new object(); // 线程锁
|
||||
private static int _maxLogs = 1000; // 默认容量
|
||||
|
||||
// 最大日志容量属性
|
||||
public static int MaxLogs
|
||||
{
|
||||
get => _maxLogs;
|
||||
set {
|
||||
lock (_lock) {
|
||||
_maxLogs = Mathf.Max(value, 1); // 最小值为1
|
||||
TrimExcess();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static LogCapturer()
|
||||
{
|
||||
// 注册全局日志回调
|
||||
Application.logMessageReceivedThreaded += HandleLog;
|
||||
}
|
||||
|
||||
// 日志处理回调
|
||||
private static void HandleLog(string message, string stackTrace, LogType type)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var entry = new LogEntry
|
||||
{
|
||||
Timestamp = DateTime.Now,
|
||||
Type = type,
|
||||
Message = message,
|
||||
StackTrace = stackTrace
|
||||
};
|
||||
|
||||
_logs.Enqueue(entry);
|
||||
TrimExcess();
|
||||
}
|
||||
}
|
||||
|
||||
// 日志队列修剪
|
||||
private static void TrimExcess()
|
||||
{
|
||||
while (_logs.Count > _maxLogs)
|
||||
{
|
||||
_logs.Dequeue();
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前所有日志(倒序:最新在前)
|
||||
public static List<LogEntry> GetLogs(bool reverseOrder = true)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var list = new List<LogEntry>(_logs);
|
||||
if (reverseOrder) list.Reverse();
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
// 清空日志
|
||||
public static void Clear()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_logs.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user