"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerLogger = void 0;
/**
 * Custom console logger that captures logs for servers
 */
class ServerLogger {
    logPrefix;
    original;
    logs = {
        stdout: [],
        stderr: [],
        maxLogLines: 1000, // Maximum number of log lines to keep
    };
    constructor(logPrefix = '') {
        this.logPrefix = logPrefix;
        this.original = { ...console };
    }
    log(...args) {
        const message = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : String(arg)).join(' ');
        const prefixedMessage = this.logPrefix ? `[${this.logPrefix}] ${message}` : message;
        this.logs.stdout.push(`[${new Date().toISOString()}] ${prefixedMessage}`);
        // Trim if exceeds max lines
        if (this.logs.stdout.length > this.logs.maxLogLines) {
            this.logs.stdout = this.logs.stdout.slice(-this.logs.maxLogLines);
        }
        // Call original console.log
        this.original.log(prefixedMessage);
    }
    error(...args) {
        const message = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : String(arg)).join(' ');
        const prefixedMessage = this.logPrefix ? `[${this.logPrefix}] ${message}` : message;
        this.logs.stderr.push(`[${new Date().toISOString()}] ${prefixedMessage}`);
        // Trim if exceeds max lines
        if (this.logs.stderr.length > this.logs.maxLogLines) {
            this.logs.stderr = this.logs.stderr.slice(-this.logs.maxLogLines);
        }
        // Call original console.error
        this.original.error(prefixedMessage);
    }
    // Get logs by type
    getLogs(type) {
        if (type === 'stdout')
            return this.logs.stdout;
        if (type === 'stderr')
            return this.logs.stderr;
        return {
            stdout: this.logs.stdout,
            stderr: this.logs.stderr
        };
    }
    // Clear logs by type
    clearLogs(type) {
        if (type === 'stdout' || type === 'all')
            this.logs.stdout = [];
        if (type === 'stderr' || type === 'all')
            this.logs.stderr = [];
        return true;
    }
}
exports.ServerLogger = ServerLogger;
//# sourceMappingURL=serverLogger.js.map