TeamIDE-methodology/templates/scripts/console-logger.ts

109 lines
3.1 KiB
TypeScript

/**
* Console Logger Boot File (Tauri)
*
* Intercepts console.log/warn/error/info/debug and forwards them to the
* Rust backend via Tauri invoke. This allows all frontend console output
* to be captured in the build.sh console logs.
*
* The original console methods are preserved and still work normally,
* so you'll see output in both the WebView devtools AND the terminal logs.
*
* SETUP:
* 1. Copy this file to: app/src/boot/console-logger.ts
* 2. Add 'console-logger' to boot array in quasar.config.js
* 3. Add log_to_stdout command to Rust (see below)
* 4. Add chrono = "0.4" to Cargo.toml dependencies
*
* RUST COMMAND (add to src-tauri/src/commands.rs):
*
* use chrono::Local;
*
* #[tauri::command]
* pub fn log_to_stdout(level: String, message: String) {
* let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S%.3f");
* let prefix = match level.as_str() {
* "error" => "[ERROR]",
* "warn" => "[WARN]",
* "info" => "[INFO]",
* "debug" => "[DEBUG]",
* _ => "[LOG]",
* };
* println!("[{}] {} {}", timestamp, prefix, message);
* }
*
* Don't forget to register it in lib.rs:
* commands::log_to_stdout,
*/
import { invoke } from '@tauri-apps/api/core';
// Store original console methods
const originalConsole = {
log: console.log.bind(console),
warn: console.warn.bind(console),
error: console.error.bind(console),
info: console.info.bind(console),
debug: console.debug.bind(console),
};
// Helper to stringify arguments
function formatArgs(args: unknown[]): string {
return args
.map((arg) => {
if (typeof arg === 'string') return arg;
if (arg instanceof Error) return `${arg.name}: ${arg.message}\n${arg.stack}`;
try {
return JSON.stringify(arg, null, 2);
} catch {
return String(arg);
}
})
.join(' ');
}
// Forward to Rust backend
function logToBackend(level: string, args: unknown[]) {
const message = formatArgs(args);
invoke('log_to_stdout', { level, message }).catch(() => {
// Silently fail if Tauri isn't available (e.g., in browser dev mode)
});
}
// Override console methods
console.log = (...args: unknown[]) => {
originalConsole.log(...args);
logToBackend('log', args);
};
console.warn = (...args: unknown[]) => {
originalConsole.warn(...args);
logToBackend('warn', args);
};
console.error = (...args: unknown[]) => {
originalConsole.error(...args);
logToBackend('error', args);
};
console.info = (...args: unknown[]) => {
originalConsole.info(...args);
logToBackend('info', args);
};
console.debug = (...args: unknown[]) => {
originalConsole.debug(...args);
logToBackend('debug', args);
};
// Also capture unhandled errors and promise rejections
window.addEventListener('error', (event) => {
logToBackend('error', [`Unhandled error: ${event.message} at ${event.filename}:${event.lineno}`]);
});
window.addEventListener('unhandledrejection', (event) => {
logToBackend('error', [`Unhandled promise rejection: ${event.reason}`]);
});
// Log that the console logger is active
console.info('[Console Logger] Active - all console output will be logged to stdout');