"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = void 0;
exports.getLogLevel = getLogLevel;
exports.setLogLevel = setLogLevel;
const LOG_LEVELS = {
    error: 0,
    warn: 1,
    info: 2,
    debug: 3,
};
let runtimeLevel = null;
const envLevel = parseLevel(process.env.LOG_LEVEL);
function parseLevel(raw) {
    const level = (raw || 'info').toLowerCase();
    return (level in LOG_LEVELS) ? level : 'info';
}
/** Get the current effective log level. */
function getLogLevel() {
    return runtimeLevel ?? envLevel;
}
function getCurrentLevel() {
    return LOG_LEVELS[getLogLevel()];
}
/** Set log level at runtime. Pass null to revert to env var default. */
function setLogLevel(level) {
    if (level !== null && !(level in LOG_LEVELS)) {
        throw new Error(`Invalid log level: ${level}. Valid: ${Object.keys(LOG_LEVELS).join(', ')}`);
    }
    runtimeLevel = level;
}
exports.logger = {
    error: (tag, message, ...args) => {
        console.error(`[${tag}] ${message}`, ...args);
    },
    warn: (tag, message, ...args) => {
        if (getCurrentLevel() >= LOG_LEVELS.warn) {
            console.warn(`[${tag}] ${message}`, ...args);
        }
    },
    info: (tag, message, ...args) => {
        if (getCurrentLevel() >= LOG_LEVELS.info) {
            console.log(`[${tag}] ${message}`, ...args);
        }
    },
    debug: (tag, message, ...args) => {
        if (getCurrentLevel() >= LOG_LEVELS.debug) {
            console.log(`[${tag}] ${message}`, ...args);
        }
    },
    /** Log SQL queries only in debug mode */
    sql: (tag, sql, durationMs) => {
        if (getCurrentLevel() >= LOG_LEVELS.debug) {
            const truncated = sql.length > 200 ? sql.substring(0, 200) + '...' : sql;
            if (durationMs !== undefined) {
                console.log(`[${tag}] SQL (${durationMs}ms): ${truncated}`);
            }
            else {
                console.log(`[${tag}] SQL: ${truncated}`);
            }
        }
    },
};
exports.default = exports.logger;
//# sourceMappingURL=logger.js.map