|
| 1 | +/** |
| 2 | + * Ring buffer for main-process console output. |
| 3 | + * |
| 4 | + * Captures the last `capacity` lines written via console.info / console.warn / |
| 5 | + * console.error / console.log into a single in-memory buffer. Disabled by |
| 6 | + * default — install only when verbose diagnostics are wanted, e.g. when |
| 7 | + * OPENSCREEN_DIAGNOSTIC=1 is set or when a developer wants a more complete |
| 8 | + * "Save Diagnostics" payload for an upstream bug report. |
| 9 | + * |
| 10 | + * Cost when enabled: one array.push + occasional shift per console call, |
| 11 | + * negligible against the rest of the app. Cost when disabled: zero, the |
| 12 | + * original console methods are kept untouched. |
| 13 | + */ |
| 14 | + |
| 15 | +const DEFAULT_CAPACITY = 500; |
| 16 | + |
| 17 | +export interface MainLogEntry { |
| 18 | + timestampMs: number; |
| 19 | + level: "info" | "warn" | "error" | "log"; |
| 20 | + text: string; |
| 21 | +} |
| 22 | + |
| 23 | +export class MainLogBuffer { |
| 24 | + private readonly capacity: number; |
| 25 | + private readonly entries: MainLogEntry[] = []; |
| 26 | + private installed = false; |
| 27 | + private readonly originals: Partial< |
| 28 | + Record<"log" | "info" | "warn" | "error", (...args: unknown[]) => void> |
| 29 | + > = {}; |
| 30 | + |
| 31 | + constructor(capacity = DEFAULT_CAPACITY) { |
| 32 | + this.capacity = Math.max(1, capacity); |
| 33 | + } |
| 34 | + |
| 35 | + install(): void { |
| 36 | + if (this.installed) return; |
| 37 | + this.installed = true; |
| 38 | + const console_ = console as unknown as Record< |
| 39 | + "log" | "info" | "warn" | "error", |
| 40 | + (...args: unknown[]) => void |
| 41 | + >; |
| 42 | + for (const level of ["log", "info", "warn", "error"] as const) { |
| 43 | + this.originals[level] = console_[level].bind(console); |
| 44 | + console_[level] = (...args: unknown[]) => { |
| 45 | + this.push(level, args); |
| 46 | + this.originals[level]?.(...args); |
| 47 | + }; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + uninstall(): void { |
| 52 | + if (!this.installed) return; |
| 53 | + this.installed = false; |
| 54 | + const console_ = console as unknown as Record< |
| 55 | + "log" | "info" | "warn" | "error", |
| 56 | + (...args: unknown[]) => void |
| 57 | + >; |
| 58 | + for (const level of ["log", "info", "warn", "error"] as const) { |
| 59 | + if (this.originals[level]) { |
| 60 | + console_[level] = this.originals[level] as (...args: unknown[]) => void; |
| 61 | + } |
| 62 | + } |
| 63 | + this.originals.log = undefined; |
| 64 | + this.originals.info = undefined; |
| 65 | + this.originals.warn = undefined; |
| 66 | + this.originals.error = undefined; |
| 67 | + } |
| 68 | + |
| 69 | + snapshot(): MainLogEntry[] { |
| 70 | + return this.entries.slice(); |
| 71 | + } |
| 72 | + |
| 73 | + clear(): void { |
| 74 | + this.entries.length = 0; |
| 75 | + } |
| 76 | + |
| 77 | + private push(level: MainLogEntry["level"], args: unknown[]): void { |
| 78 | + const text = args |
| 79 | + .map((arg) => { |
| 80 | + if (typeof arg === "string") return arg; |
| 81 | + try { |
| 82 | + return JSON.stringify(arg); |
| 83 | + } catch { |
| 84 | + return String(arg); |
| 85 | + } |
| 86 | + }) |
| 87 | + .join(" "); |
| 88 | + this.entries.push({ timestampMs: Date.now(), level, text }); |
| 89 | + if (this.entries.length > this.capacity) { |
| 90 | + this.entries.splice(0, this.entries.length - this.capacity); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export const mainLogBuffer = new MainLogBuffer(); |
| 96 | + |
| 97 | +export function isDiagnosticModeEnabled(): boolean { |
| 98 | + const raw = process.env.OPENSCREEN_DIAGNOSTIC; |
| 99 | + if (!raw) return false; |
| 100 | + const lowered = raw.trim().toLowerCase(); |
| 101 | + return lowered === "1" || lowered === "true" || lowered === "yes"; |
| 102 | +} |
0 commit comments