UNPKG

koneko-cli

Version:

Your CLI for reading manga from the terminal

76 lines (75 loc) โ€ข 3.02 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.logger = void 0; exports.setupDebugLogger = setupDebugLogger; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); const SettingsConfig_1 = __importDefault(require("../SettingsConfig")); let initialized = false; const logFilePath = path_1.default.join(os_1.default.homedir(), '.config', 'koneko', 'debug.log'); function formatLogLine(type, args) { const timeStamp = new Date().toISOString(); const message = args.map(arg => typeof arg === 'string' ? arg : JSON.stringify(arg, null, 2)).join(' '); return `[${timeStamp}] [${type.toUpperCase()}] ${message}\n`; } async function setupDebugLogger() { var _a, _b; if (initialized) return; initialized = true; await SettingsConfig_1.default.read(); const isDebugEnabled = (_b = (_a = SettingsConfig_1.default.data) === null || _a === void 0 ? void 0 : _a.Settings) === null || _b === void 0 ? void 0 : _b.DebugLogging; if (!isDebugEnabled) return; // ๐Ÿงน fully remove the log file on each start try { if (fs_1.default.existsSync(logFilePath)) { fs_1.default.unlinkSync(logFilePath); console.log(`๐Ÿ—‘๏ธ Deleted old debug log at ${logFilePath}`); } } catch (err) { console.error(`Failed to delete old debug log:`, err); } const originalConsole = { log: console.log, info: console.info, warn: console.warn, error: console.error, }; console.log = (...args) => { const line = formatLogLine('log', args); fs_1.default.appendFile(logFilePath, line, () => { }); originalConsole.log(...args); }; console.info = (...args) => { const line = formatLogLine('info', args); fs_1.default.appendFile(logFilePath, line, () => { }); originalConsole.info(...args); }; console.warn = (...args) => { const line = formatLogLine('warn', args); fs_1.default.appendFile(logFilePath, line, () => { }); originalConsole.warn(...args); }; console.error = (...args) => { const line = formatLogLine('error', args); fs_1.default.appendFile(logFilePath, line, () => { }); originalConsole.error(...args); }; originalConsole.log(`โœ… Debug logging started. Logs saved to ${logFilePath}`); } exports.logger = { async debug(...args) { var _a, _b; await SettingsConfig_1.default.read(); if ((_b = (_a = SettingsConfig_1.default.data) === null || _a === void 0 ? void 0 : _a.Settings) === null || _b === void 0 ? void 0 : _b.DebugLogging) { const line = formatLogLine('debug', args); fs_1.default.appendFile(logFilePath, line, () => { }); } } };