using System;
namespace RynthCore.Engine;
///
/// Centralised logging — every log call in the Engine routes through here.
/// Each category method writes at that category's emit level (engine.json "LogCategories",
/// editable live from the launcher; see ), gated by the global
/// "LoggingLevel". Messages keep their existing prefix text (e.g. "D3D9VTable: scanning…").
///
internal static class RynthLog
{
// ── Category methods ─────────────────────────────────────────────────
/// D3D9 subsystem: vtable, EndScene, bootstrapper, matrix capture, nav3D.
internal static void D3D9(string msg) => WriteCategory(LogCategory.D3D9, msg);
/// Compatibility hooks: SmartBox, client objects, combat, movement, vitals, chat, etc.
internal static void Compat(string msg) => WriteCategory(LogCategory.Compat, msg);
/// ImGui rendering: context, DX9 backend, Win32 input, shell.
internal static void Render(string msg) => WriteCategory(LogCategory.Render, msg);
/// Plugin system: loader, manager, lifecycle callbacks, plugin host Log lines.
internal static void Plugin(string msg) => WriteCategory(LogCategory.Plugin, msg);
/// UI / Avalonia overlay subsystem.
internal static void UI(string msg) => WriteCategory(LogCategory.UI, msg);
/// Verbose detail (any subsystem). Debug by default; set the Verbose category to Info to see it in the normal log.
internal static void Verbose(string msg) => WriteCategory(LogCategory.Verbose, msg);
/// Same as .
internal static void Debug(string msg) => WriteCategory(LogCategory.Verbose, msg);
/// Trace-level line: written only when the global level is Trace.
internal static void Trace(string msg) => WriteAt(EntryPoint.EngineLogLevel.Trace, msg);
/// Uncategorised line (General category, Info by default).
internal static void Info(string msg) => WriteCategory(LogCategory.General, msg);
/// Same as .
internal static void Warning(string msg) => Warn(msg);
///
/// INFO line for a rare state change a support log must show (the server verdicts): written
/// whenever the global LoggingLevel allows Info, whatever the categories' emit levels say.
/// A category set to Trace under a global Info level would otherwise drop it.
///
internal static void Milestone(string msg)
{
if (EntryPoint.ShouldLog(EntryPoint.EngineLogLevel.Info))
EntryPoint.LogTagged("engine", msg, "INF");
}
// Most recent WRN/ERR text + when, surfaced via the GetEngineStatusJson host bridge so a stuck box
// can be diagnosed remotely without reading the PC log. Recorded even when the line is filtered.
internal static volatile string? LastIssue;
internal static DateTime LastIssueUtc;
/// WARN line — a recoverable problem worth grepping for. Hidden only at level Error/Off.
internal static void Warn(string msg)
{
LastIssue = msg; LastIssueUtc = DateTime.UtcNow;
if (EntryPoint.ShouldLog(EntryPoint.EngineLogLevel.Warning))
EntryPoint.LogTagged("engine", msg, "WRN");
}
///
/// Always-on heartbeat line, written at every LoggingLevel (Off included). The launcher's
/// wedge watchdog reads RynthCore.<pid>.log going quiet for 120 s as a dead engine and
/// kills the client, so this line must never be filtered: on 2026-10-06 LoggingLevel=Info
/// with General=Trace dropped it and healthy in-world clients were killed and relaunched
/// every few minutes.
///
internal static void Heartbeat(string msg) => EntryPoint.LogTagged("engine", msg, "INF");
/// Always-on ERROR line — a fault/crash/disable, written even at level Off. Triage with grep "[ERR]".
internal static void Error(string msg)
{
LastIssue = msg; LastIssueUtc = DateTime.UtcNow;
EntryPoint.LogTagged("engine", msg, "ERR");
}
// ── Diagnostic traces (2026-10-05, Drakkon's held casts on DreamWeave) ──
// A movement packet sent while a cast winds up can cancel the cast or leave the server
// holding it open, and the old movement lines were 1 in 8 with no caller. Both switches
// are on by default; each costs one bool test when off.
/// Every plugin movement call (SetAutoRun, SetMotion, DoMovement/StopMovement,
/// StopCompletely, TurnToHeading, jumps), one "Move:" line each with the calling plugin.
/// Identical calls in a row from one caller (turns: any angle) are counted, not written,
/// for up to MoveRepeatWindowMs; the count rides on the next line written.
internal static bool MoveTraceEnabled = true;
/// Every server UseDone (0x01C7) with its sequence number and error code
/// (before: the first 25 of a session).
internal static bool ActionDoneTraceEnabled = true;
private const long MoveRepeatWindowMs = 1000;
private static readonly object _moveGate = new();
private static string _moveLastKey = "";
private static long _moveLastWrittenMs = long.MinValue;
private static int _moveRepeats;
private static long _moveRepeatFirstMs;
private static string _moveRepeatLastArg = "";
///
/// One plugin movement call (see ).
/// must be a string literal (it also goes into AcActionTrace, which keeps the last 256
/// actions for crash dumps); / are its trace args.
///
internal static void Move(string name, string arg, bool ok, bool collapseByKind = false, uint a = 0, uint b = 0)
{
Compatibility.AcActionTrace.Record(name, a, b);
if (!MoveTraceEnabled) return;
string? line = null;
try
{
string caller = MoveCaller();
lock (_moveGate)
{
long now = Environment.TickCount64;
string key = collapseByKind ? $"{caller}|{name}" : $"{caller}|{name}({arg})|{ok}";
if (key == _moveLastKey && now - _moveLastWrittenMs < MoveRepeatWindowMs)
{
if (_moveRepeats == 0) _moveRepeatFirstMs = now;
_moveRepeats++;
_moveRepeatLastArg = arg;
return;
}
string more = "";
if (_moveRepeats > 0)
{
string[] parts = _moveLastKey.Split('|');
string what = parts.Length > 1 ? parts[1] : "?";
string last = parts.Length > 2 ? "" : $", last {_moveRepeatLastArg}";
more = $" (+{_moveRepeats} more {what}{last} by {parts[0]} before this, over {now - _moveRepeatFirstMs} ms)";
}
line = $"Move: {name}({arg}) by {caller} -> {(ok ? "ok" : "FAILED")}{more}";
_moveLastKey = key;
_moveLastWrittenMs = now;
_moveRepeats = 0;
_moveRepeatLastArg = "";
}
}
catch { return; }
Write(line);
}
/// The plugin whose tick or event is calling, else the thread.
private static string MoveCaller()
{
var plugin = Plugins.PluginManager.CurrentDispatch;
if (plugin != null)
return string.IsNullOrEmpty(plugin.DisplayName) ? plugin.FileName : plugin.DisplayName;
return Compatibility.MainThreadGuard.IsOnMainThread()
? "AC main thread"
: $"thread {Environment.CurrentManagedThreadId}";
}
/// True when a line in would be written (guard expensive messages).
internal static bool IsEnabled(LogCategory category) => LogSettings.IsEnabled(category);
// ── Sink ─────────────────────────────────────────────────────────────
/// Uncategorised line (General category); used by the Move/UseDone traces.
private static void Write(string? message)
{
if (message != null)
WriteCategory(LogCategory.General, message);
}
private static void WriteCategory(LogCategory category, string message)
=> WriteAt(LogSettings.LevelFor(category), message);
private static void WriteAt(EntryPoint.EngineLogLevel level, string message)
{
if (EntryPoint.ShouldLog(level))
EntryPoint.LogTagged("engine", message, LogSettings.Tag(level));
}
}