UNPKG

summarizely-cli

Version:

YouTube summarizer that respects your existing subscriptions. No API keys required.

198 lines 6.62 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.debugHelpers = exports.Debug = exports.DebugCategory = void 0; exports.debug = debug; exports.debugTrace = debugTrace; exports.debugTime = debugTime; exports.debugTimeEnd = debugTimeEnd; const config_1 = require("./config"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); var DebugCategory; (function (DebugCategory) { DebugCategory["PROVIDER"] = "provider"; DebugCategory["CACHE"] = "cache"; DebugCategory["BATCH"] = "batch"; DebugCategory["NETWORK"] = "network"; DebugCategory["FILESYSTEM"] = "filesystem"; DebugCategory["PERFORMANCE"] = "performance"; DebugCategory["CONFIG"] = "config"; DebugCategory["ERROR"] = "error"; DebugCategory["GENERAL"] = "general"; })(DebugCategory || (exports.DebugCategory = DebugCategory = {})); class Debug { static initialize() { const config = (0, config_1.getConfig)(); this.enabled = config.debug.enabled || process.env.DEBUG === 'summarizely'; this.verbose = config.debug.verbose || process.env.SUMMARIZELY_DEBUG_VERBOSE === 'true'; // Parse debug categories from env const debugCategories = process.env.SUMMARIZELY_DEBUG_CATEGORIES; if (debugCategories) { debugCategories.split(',').forEach(cat => { this.categories.add(cat.trim()); }); } else if (this.enabled) { // Enable all categories if debug is on but no specific categories set Object.values(DebugCategory).forEach(cat => this.categories.add(cat)); } // Setup log file if specified const logFilePath = process.env.SUMMARIZELY_DEBUG_FILE; if (logFilePath) { this.logFile = path_1.default.resolve(logFilePath); this.ensureLogDir(); } } static ensureLogDir() { if (this.logFile) { const dir = path_1.default.dirname(this.logFile); if (!fs_1.default.existsSync(dir)) { fs_1.default.mkdirSync(dir, { recursive: true }); } } } static log(category, message, data) { if (!this.enabled || !this.categories.has(category)) return; const timestamp = new Date().toISOString(); const prefix = `[${timestamp}] [${category.toUpperCase()}]`; const fullMessage = `${prefix} ${message}`; // Format data if present let dataStr = ''; if (data !== undefined) { if (this.verbose) { dataStr = '\n' + JSON.stringify(data, null, 2); } else { dataStr = ' ' + JSON.stringify(data); } } const output = fullMessage + dataStr; // Write to stderr process.stderr.write(output + '\n'); // Write to file if configured if (this.logFile) { try { fs_1.default.appendFileSync(this.logFile, output + '\n', 'utf8'); } catch (e) { // Silently fail if can't write to log file } } } static trace(category, message) { if (!this.verbose) return; const stack = new Error().stack; const caller = stack?.split('\n')[2]?.trim() || 'unknown'; this.log(category, `${message} [${caller}]`); } static time(label) { if (!this.enabled) return; console.time(`[DEBUG] ${label}`); } static timeEnd(label) { if (!this.enabled) return; console.timeEnd(`[DEBUG] ${label}`); } static group(label) { if (!this.enabled || !this.verbose) return; console.group(`[DEBUG] ${label}`); } static groupEnd() { if (!this.enabled || !this.verbose) return; console.groupEnd(); } static table(data) { if (!this.enabled || !this.verbose) return; console.table(data); } static assert(condition, message) { if (!this.enabled) return; console.assert(condition, `[DEBUG] ${message}`); } static memory() { if (!this.enabled || !this.verbose) return; const usage = process.memoryUsage(); const formatted = { rss: `${Math.round(usage.rss / 1024 / 1024)} MB`, heapTotal: `${Math.round(usage.heapTotal / 1024 / 1024)} MB`, heapUsed: `${Math.round(usage.heapUsed / 1024 / 1024)} MB`, external: `${Math.round(usage.external / 1024 / 1024)} MB` }; this.log(DebugCategory.PERFORMANCE, 'Memory usage', formatted); } static setEnabled(enabled) { this.enabled = enabled; } static setVerbose(verbose) { this.verbose = verbose; } static addCategory(category) { this.categories.add(category); } static removeCategory(category) { this.categories.delete(category); } static isEnabled() { return this.enabled; } static isVerbose() { return this.verbose; } } exports.Debug = Debug; _a = Debug; Debug.enabled = false; Debug.verbose = false; Debug.logFile = null; Debug.categories = new Set(); (() => { _a.initialize(); })(); // Convenience functions function debug(category, message, data) { Debug.log(category, message, data); } function debugTrace(category, message) { Debug.trace(category, message); } function debugTime(label) { Debug.time(label); } function debugTimeEnd(label) { Debug.timeEnd(label); } // Debug helpers for common scenarios exports.debugHelpers = { provider: (action, provider, data) => { Debug.log(DebugCategory.PROVIDER, `${action}: ${provider}`, data); }, cache: (action, key, hit) => { Debug.log(DebugCategory.CACHE, `${action}: ${key} (${hit ? 'HIT' : 'MISS'})`); }, batch: (action, current, total) => { Debug.log(DebugCategory.BATCH, `${action}: ${current}/${total}`); }, network: (method, url, status) => { Debug.log(DebugCategory.NETWORK, `${method} ${url}${status ? ` [${status}]` : ''}`); }, error: (error, context) => { Debug.log(DebugCategory.ERROR, context || 'Error occurred', { message: error.message, stack: Debug.isVerbose() ? error.stack : undefined }); } }; //# sourceMappingURL=debug.js.map