namespace CommandTerminal { using System.Collections.Generic; using System.Diagnostics; using System.Linq; using DataStructures; using UnityEngine; public enum TerminalLogType { Error = LogType.Error, Assert = LogType.Assert, Warning = LogType.Warning, Message = LogType.Log, Exception = LogType.Exception, Input, ShellMessage, } public readonly struct LogItem { public readonly TerminalLogType type; public readonly string message; public readonly string stackTrace; public LogItem(TerminalLogType type, string message, string stackTrace) { this.type = type; this.message = message ?? string.Empty; this.stackTrace = stackTrace ?? string.Empty; } } public sealed class CommandLog { public IReadOnlyList Logs => _logs; public int Capacity => _logs.Capacity; public readonly HashSet ignoredLogTypes; private readonly CyclicBuffer _logs; public CommandLog(int maxItems, IEnumerable ignoredLogTypes = null) { _logs = new CyclicBuffer(maxItems); this.ignoredLogTypes = new HashSet( ignoredLogTypes ?? Enumerable.Empty() ); } public bool HandleLog(string message, TerminalLogType type, bool includeStackTrace = false) { string stackTrace; if (includeStackTrace) { StackTrace stack = new(); stackTrace = stack.ToString(); } else { stackTrace = string.Empty; } return HandleLog(message, stackTrace, type); } public bool HandleLog(string message, string stackTrace, TerminalLogType type) { if (ignoredLogTypes.Contains(type)) { return false; } LogItem log = new(type, message, stackTrace); _logs.Add(log); return true; } public int Clear() { int logCount = _logs.Count; _logs.Clear(); return logCount; } public void Resize(int newCapacity) { _logs.Resize(newCapacity); } } }