Monorepo for Aesthetic.Computer aesthetic.computer
at main 169 lines 4.1 kB view raw
1/** 2 * Logger utility that routes output through TUI dashboard when enabled, 3 * or uses normal console when TUI is disabled. 4 */ 5 6let dashboard = null; 7let useTUI = false; 8 9/** 10 * Initialize the logger with TUI dashboard 11 */ 12export function initLogger(dashboardInstance, tuiEnabled) { 13 dashboard = dashboardInstance; 14 useTUI = tuiEnabled; 15} 16 17/** 18 * Log an info message 19 */ 20export function logInfo(message) { 21 if (useTUI && dashboard) { 22 dashboard.addLog('info', message); 23 } else { 24 console.log(message); 25 } 26} 27 28/** 29 * Log an error message 30 */ 31export function logError(message) { 32 if (useTUI && dashboard) { 33 dashboard.addLog('error', message); 34 } else { 35 console.error(message); 36 } 37} 38 39/** 40 * Log a warning message 41 */ 42export function logWarning(message) { 43 if (useTUI && dashboard) { 44 dashboard.addLog('warning', message); 45 } else { 46 console.warn(message); 47 } 48} 49 50/** 51 * Override console methods when TUI is active 52 */ 53export function overrideConsole() { 54 if (useTUI) { 55 // Store originals 56 const originalLog = console.log; 57 const originalError = console.error; 58 const originalWarn = console.warn; 59 const originalStdoutWrite = process.stdout.write; 60 const originalStderrWrite = process.stderr.write; 61 62 // Override console methods 63 console.log = (...args) => { 64 if (dashboard) { 65 dashboard.addLog('info', args.join(' ')); 66 } 67 }; 68 69 console.error = (...args) => { 70 if (dashboard) { 71 dashboard.addLog('error', args.join(' ')); 72 } 73 }; 74 75 console.warn = (...args) => { 76 if (dashboard) { 77 dashboard.addLog('warning', args.join(' ')); 78 } 79 }; 80 81 // Override stdout/stderr to catch direct writes (but allow blessed through) 82 process.stdout.write = function(chunk, encoding, callback) { 83 // Allow blessed terminal control sequences through 84 if (typeof chunk === 'string' && (chunk.includes('\x1b[') || chunk.includes('\u001b['))) { 85 return originalStdoutWrite.call(this, chunk, encoding, callback); 86 } 87 88 // Suppress other direct writes in TUI mode 89 if (typeof encoding === 'function') { 90 encoding(); // callback 91 } else if (typeof callback === 'function') { 92 callback(); 93 } 94 return true; 95 }; 96 97 process.stderr.write = function(chunk, encoding, callback) { 98 // Allow blessed terminal control sequences through 99 if (typeof chunk === 'string' && (chunk.includes('\x1b[') || chunk.includes('\u001b['))) { 100 return originalStderrWrite.call(this, chunk, encoding, callback); 101 } 102 103 // Suppress other direct writes in TUI mode 104 if (typeof encoding === 'function') { 105 encoding(); // callback 106 } else if (typeof callback === 'function') { 107 callback(); 108 } 109 return true; 110 }; 111 112 // Return restore function 113 return () => { 114 console.log = originalLog; 115 console.error = originalError; 116 console.warn = originalWarn; 117 process.stdout.write = originalStdoutWrite; 118 process.stderr.write = originalStderrWrite; 119 }; 120 } 121 return () => {}; // No-op restore function 122} 123 124/** 125 * Log timing information (performance logs) 126 */ 127export function logTiming(message) { 128 if (useTUI && dashboard) { 129 dashboard.addLog('timing', message); 130 } else { 131 console.log(message); 132 } 133} 134 135/** 136 * Log export information 137 */ 138export function logExport(message) { 139 if (useTUI && dashboard) { 140 dashboard.addLog('export', message); 141 } else { 142 console.log(message); 143 } 144} 145 146/** 147 * Update progress (no-op since we removed progress bar from dashboard) 148 */ 149export function updateProgress(current, total, message) { 150 // No-op - progress is now shown in status panel as "Frame X/Y" 151} 152 153/** 154 * Update memory stats (TUI only) 155 */ 156export function updateMemoryStats(rss, heap, delta) { 157 if (useTUI && dashboard) { 158 dashboard.updateMemoryStats({ rssMemory: rss, heapMemory: heap, memoryDelta: delta }); 159 } 160} 161 162/** 163 * Update status (TUI only) 164 */ 165export function updateStatus(stats) { 166 if (useTUI && dashboard) { 167 dashboard.updateStatus(stats); 168 } 169}