// IltLog.cs - RynthILT's diagnostics log: the ILT features' trace categories and key events, // written to %APPDATA%\RynthCore\RynthILT\Logs\RynthILT_yyyy-MM-dd.txt. // // The ILT Hub logged through RynthAi's RynthLog (LogCat.Ilt*, LogEvents.Ilt*) while it lived in // RynthAi; the moved code keeps the same calls (RynthLog.Write / Trace / Exception / Event), so // this is a small RynthLog of RynthILT's own: // Write file (always) and the engine log // Trace file, only while that category is traced ("/ilt trace on|off") // Exception file and the engine log, always // Event as Write for the key events (login, options refreshed, ...); trace-only events // as Trace // "/ilt debug on" echoes trace and exception lines to chat (local only). Lines are queued on any // thread and written by Pump() on the plugin pump. using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Text; using RynthCore.PluginSdk; namespace RynthCore.Plugin.RynthILT; /// One trace category per ILT feature (the names RynthAi's ILT Hub used). internal enum LogCat { IltHub, IltOptions, IltChat, IltStore, IltBanking, IltPets, IltQuests, IltProgression, IltRates, IltGear, IltGames, } /// Named key events (the ILT Hub's catalogue in RynthAi's RynthLog). internal static class LogEvents { public const string IltLogin = "IltHub.Login"; public const string IltWorldCheck = "IltHub.WorldCheck"; public const string IltOptionsRefreshStart = "IltHub.OptionsRefreshStart"; public const string IltOptionsRefreshed = "IltHub.OptionsRefreshed"; public const string IltWindowShown = "IltHub.WindowShown"; public const string IltWindowHidden = "IltHub.WindowHidden"; public const string IltCommand = "IltHub.Command"; public const string IltConfirmation = "IltHub.Confirmation"; public const string IltLogout = "IltHub.Logout"; /// /aug ApplyAugLines outcome (Progress Refresh current / login probe). public const string IltAugLoad = "IltHub.AugLoad"; /// Events written as a normal log line; the rest only while their category is traced. internal static readonly Dictionary Catalog = new(StringComparer.Ordinal) { [IltLogin] = (LogCat.IltHub, true), [IltWorldCheck] = (LogCat.IltOptions, true), [IltOptionsRefreshStart] = (LogCat.IltOptions, false), [IltOptionsRefreshed] = (LogCat.IltOptions, true), [IltWindowShown] = (LogCat.IltHub, true), [IltWindowHidden] = (LogCat.IltHub, false), [IltCommand] = (LogCat.IltHub, false), [IltConfirmation] = (LogCat.IltHub, false), [IltLogout] = (LogCat.IltHub, false), [IltAugLoad] = (LogCat.IltProgression, true), }; } internal static class RynthLog { private static readonly ConcurrentQueue Pending = new(); private static readonly ConcurrentQueue PendingChat = new(); private static readonly bool[] Traced = new bool[Enum.GetValues().Length]; private static RynthCoreHost _host; private static bool _hasHost; private static string _directory = string.Empty; /// Echo trace and exception lines to the chat window ("/ilt debug on"). public static volatile bool DebugToChat; /// Where the daily files go (tests point it at a scratch folder). public static string Directory => _directory; public static void Init(RynthCoreHost host, string? directory = null) { _host = host; _hasHost = true; _directory = directory ?? Path.Combine(RynthILTPlugin.SettingsRoot, "Logs"); } /// Drops the host (plugin shutdown): nothing is written to it afterwards. public static void Shutdown() { Pump(); _hasHost = false; } public static void Write(LogCat cat, string message) { Enqueue(cat, message); HostLog(message); } public static void Trace(LogCat cat, string message) { if (!IsTracing(cat)) return; Enqueue(cat, message); if (DebugToChat) Chat($"[RynthILT {cat}] {message}"); } public static void Exception(LogCat cat, Exception ex, string context = "") { string line = $"EXCEPTION {(context.Length > 0 ? context + ": " : "")}{ex.GetType().Name}: {ex.Message}"; Enqueue(cat, line + Environment.NewLine + ex.StackTrace); HostLog(line); if (DebugToChat) Chat($"[RynthILT {cat}] {line}"); } public static void Event(string eventKey, string message) { if (!LogEvents.Catalog.TryGetValue(eventKey, out var e)) { Write(LogCat.IltHub, $"[{eventKey}] {message}"); return; } if (e.Info) Write(e.Cat, $"[{eventKey}] {message}"); else Trace(e.Cat, $"[{eventKey}] {message}"); } /// True when would record the event (an Info event, or a traced Trace one). public static bool IsEventEnabled(string eventKey) => !LogEvents.Catalog.TryGetValue(eventKey, out var e) || e.Info || IsTracing(e.Cat); public static bool IsTracing(LogCat cat) => (int)cat >= 0 && (int)cat < Traced.Length && Traced[(int)cat]; public static void SetTracing(LogCat cat, bool on) { if ((int)cat >= 0 && (int)cat < Traced.Length) Traced[(int)cat] = on; } /// Every category on or off; returns how many. public static int SetTracingAll(bool on) { for (int i = 0; i < Traced.Length; i++) Traced[i] = on; return Traced.Length; } public static bool TryParseCategory(string name, out LogCat cat) { if (Enum.TryParse(name, ignoreCase: true, out cat)) return true; return Enum.TryParse("Ilt" + name, ignoreCase: true, out cat); } public static string TracedList() { var on = new List(); foreach (LogCat c in Enum.GetValues()) if (IsTracing(c)) on.Add(c.ToString()); return on.Count == 0 ? "none" : string.Join(", ", on); } /// Writes the queued lines (plugin pump). public static void Pump() { int lines = 0; while (lines++ < 20 && PendingChat.TryDequeue(out string? chat)) { if (!_hasHost) continue; try { if (_host.HasWriteToChat) _host.WriteToChat(chat, 1); } catch { } } if (Pending.IsEmpty || _directory.Length == 0) return; var sb = new StringBuilder(); int n = 0; while (n++ < 2000 && Pending.TryDequeue(out string? line)) sb.Append(line).Append(Environment.NewLine); try { System.IO.Directory.CreateDirectory(_directory); File.AppendAllText(Path.Combine(_directory, $"RynthILT_{DateTime.Now:yyyy-MM-dd}.txt"), sb.ToString()); } catch { /* a log that can't be written must never break a feature */ } } private static void Enqueue(LogCat cat, string message) { if (Pending.Count > 20000) return; // nothing is draining (no pump): don't grow forever Pending.Enqueue($"{DateTime.Now:HH:mm:ss.fff} [{cat}] {message}"); } private static void HostLog(string message) { if (!_hasHost) return; try { _host.Log("[RynthILT] " + message); } catch { } } /// Queued for the pump (trace lines can come from the render thread). private static void Chat(string text) { if (PendingChat.Count < 200) PendingChat.Enqueue(text); } }