using System; using System.Collections.Generic; using UniRx; using UnityEngine; public class XLogPresenter { public List LogsAll = new List(); // 筛选日志集合 public ReactiveCollection ShowLogs = new ReactiveCollection(); // public IntReactiveProperty LogsCount = new IntReactiveProperty(0); public IntReactiveProperty WarningsCount = new IntReactiveProperty(0); public IntReactiveProperty ErrorsCount = new IntReactiveProperty(0); // 是否筛选Log public bool IsShowLog = true; // 是否筛选警告Log public bool IsShowWarning = true; // 是否筛选错误Log public bool IsShowError = true; public XLogPresenter() { LogHelper.LogCallbackAsObservable().Subscribe(x => { switch (x.Type) { case LogType.Error: case LogType.Exception: if (IsShowError) { ShowLogs.Add(x); } ErrorsCount.Value++; break; case LogType.Warning: if (IsShowWarning) { ShowLogs.Add(x); } WarningsCount.Value++; break; case LogType.Log: if (IsShowLog) { ShowLogs.Add(x); } LogsCount.Value++; break; } // 添加到集合 LogsAll.Add(x); }); } // 筛选所有指定类型的LOG public void ShowLog(bool isShow) { IsShowLog = isShow; GetData(); } public void ShowWarning(bool isShow) { IsShowWarning = isShow; GetData(); } public void ShowError(bool isShow) { IsShowError = isShow; GetData(); } public void GetData() { ShowLogs.Clear(); foreach (var item in LogsAll) { switch (item.Type) { case LogType.Log: if (IsShowLog) { ShowLogs.Add(item); } break; case LogType.Warning: if (IsShowWarning) { ShowLogs.Add(item); } break; case LogType.Exception: case LogType.Error: if (IsShowError) { ShowLogs.Add(item); } break; } } } // 清空所有LOG public void ClearLogs() { LogsCount.Value = 0; WarningsCount.Value = 0; ErrorsCount.Value = 0; LogsAll.Clear(); ShowLogs.Clear(); } } public class XLog { public string Condition; public string StackTrace; public LogType Type; public bool IsSelected; public DateTime SendTime; public int Id; } public static class LogHelper { static int count = 0; public static UniRx.IObservable LogCallbackAsObservable() { return Observable.FromEvent ( h => (condition, stackTrace, type) => h(new XLog { Condition = condition, StackTrace = stackTrace, Type = type, IsSelected = false, SendTime = DateTime.Now, Id = count++ }), h => Application.logMessageReceived += h, h => Application.logMessageReceived -= h ); } }