/** * Runtime Logger — structured, leveled logging for the game runtime and CLI. * * Log format: [Runtime:] * * Levels (lowest → highest): * DEBUG — verbose diagnostics (task detail lists, tick-level state) * INFO — key lifecycle events (game start, phase changes, map loaded) * WARN — recoverable issues (WS not ready, anti-stuck triggered) * ERROR — failures that need attention (map load failed, tick error) * * Control via env var CLAWCLAW_LOG_LEVEL (default: DEBUG). * Console output is disabled by default so game runtime diagnostics do not * leak into machine-readable process output. Set CLAWCLAW_RUNTIME_LOG_TO_CONSOLE=1 * to print diagnostics to stderr. Runtime logs must never write to stdout. */ export enum LogLevel { DEBUG = 0, INFO = 1, WARN = 2, ERROR = 3, } const LEVEL_NAMES: Record = { [LogLevel.DEBUG]: 'DEBUG', [LogLevel.INFO]: 'INFO', [LogLevel.WARN]: 'WARN', [LogLevel.ERROR]: 'ERROR', }; function parseLogLevel(raw: string | undefined): LogLevel { switch (raw?.toUpperCase()) { case 'DEBUG': return LogLevel.DEBUG; case 'INFO': return LogLevel.INFO; case 'WARN': return LogLevel.WARN; case 'ERROR': return LogLevel.ERROR; default: return LogLevel.DEBUG; } } function isTrue(value: string | undefined): boolean { if (typeof value !== 'string') return false; const normalized = value.trim().toLowerCase(); return normalized === '1' || normalized === 'true'; } export class RuntimeLogger { private minLevel: LogLevel; private logToConsole: boolean; constructor() { this.minLevel = parseLogLevel(process.env.CLAWCLAW_LOG_LEVEL); this.logToConsole = isTrue(process.env.CLAWCLAW_RUNTIME_LOG_TO_CONSOLE); } // ─── Core log methods ──────────────────────────────────────────────────── debug(tag: string, msg: string): void { this.log(LogLevel.DEBUG, tag, msg); } info(tag: string, msg: string): void { this.log(LogLevel.INFO, tag, msg); } warn(tag: string, msg: string): void { this.log(LogLevel.WARN, tag, msg); } error(tag: string, msg: string, err?: unknown): void { const suffix = err instanceof Error ? ` err=${err.message}` : (err ? ` err=${err}` : ''); this.log(LogLevel.ERROR, tag, msg + suffix); if (this.logToConsole && err instanceof Error && err.stack) { console.error(err.stack); } } // ─── Helpers ───────────────────────────────────────────────────────────── /** * Log a list of tasks at DEBUG level (reduces repetitive loop logging). * Outputs one indented line per task — pair with a preceding info() call for the summary. */ logTaskList(tag: string, tasks: Array<{ name: string; room?: string; x?: number; y?: number; status?: string }>): void { for (const t of tasks) { this.debug(tag, ` └─ ${t.name} room=${t.room ?? '?'} coords=(${t.x ?? '?'},${t.y ?? '?'})${t.status ? ` status=${t.status}` : ''}`); } } // ─── Internal ──────────────────────────────────────────────────────────── private log(level: LogLevel, tag: string, msg: string): void { if (level < this.minLevel) return; if (!this.logToConsole) return; const prefix = `[Runtime:${LEVEL_NAMES[level]}]`; const line = `${prefix} ${tag} ${msg}`; // Keep opt-in runtime diagnostics on stderr so stdout remains available for // machine-readable process output. console.error(line); } }