log_log.js

/**
 * @file lib/log/log.js
 * @copyright @spmhome @_2025
 * @author Scott Meesseman @spmeesseman
 *//** */

const { isString, isNumber, isNulled } = require("@spmhome/type-utils");
const { SpmhCliLogger, hasColor, HasErrorText } = require("@spmhome/log-utils");


/**
 * @class WpwLogger
 * @augments SpmhCliLogger
 */
class WpwLogger extends SpmhCliLogger
{
    /**
     * @override
     * @param {Partial<IWpwLogOptions>} opts
     * @param {Partial<IWpwLogOptions>} [defaults]
     * @returns {WpwLogger}
     */
    static getLoggerInst = SpmhCliLogger.getLoggerInst.bind(this);


    /**
     * @param {string | undefined} msg message to print
     * @param {SpmhLogLevel} [level] logging level, `0` for 'off', use `undefined` for 'always'
     * @returns {this}
     */
    hookstart(msg, level)
    {
        if (!this.isLevelLogged(level)) { return this; }
        const fMsg = isString(msg) ? msg : this.formatObjectMessage(msg, "", false, false),
              fMsgTagSp = fMsg.includes("]") && hasColor(fMsg) ? "" : " ";
        return this.write(`${this.tag("hookstart", null, null, false, true)}${fMsgTagSp}${fMsg}`, level);
    }


    /**
     * @template {any} [T=undefined]
     * @param {any} msg message to print
     * @param {SpmhLogLevel} [level] logging level, `0` for 'off', use `undefined` for 'always'
     * @param {boolean} [failed] message to print
     * @param {T} [rtn]
     * @returns {T | undefined}
     */
    hookdone(msg, level, failed, rtn)
    {
        if (!this.isLevelLogged(level)) { return; }
        const fMsg = isString(msg) ? msg : this.formatObjectMessage(msg, "", false, false),
              fMsgTagSp = fMsg.includes("]") && hasColor(fMsg) ? "" : " ";
        if (failed !== true && (!isString(msg) || !HasErrorText.test(fMsg))) {
            this.write(`${this.tag("hookdone", null, null, false, true)}${fMsgTagSp}${fMsg}`, level);
        }
        else
        {   this.write(
                `${this.tag("failed", null, "rosered", false, true)}${fMsgTagSp}${fMsg}`, level, "", this.icons.color.error
            );
        }
        return rtn;
    }


    /**
     * Evaluates if the specified value is a valid log-level, i.e. a # between 0 and 5, inclusive
     *
     * @param {any} value
     * @type {SpmhTypeValidationResult<WpwLoggerLevel>}
     */
    static isWpwLogLevel(value) { return isNumber(value) && value >= 0 && value <=5; }


    /**
     * Evaluates if the specified value is a valid log-level, i.e. a # between 0 and 5, inclusive
     *
     * @param {any} value
     * @type {SpmhTypeValidationResult<WpwLoggerLevel>}
     */
    isWpwLogLevel(value) { return WpwLogger.isWpwLogLevel(value); }


    /**
     * Evaluates if the specified value is a valid log-level, i.e. a # between 0 and 5, inclusive
     *
     * @param {any} level
     * @type {SpmhTypeValidationResult<WebpackLogLevel>}
     */
    static isWpLogLevel(level)
    {
        return isString(level) && [ "info", "log", "level", "error", "verbose", "warn", "none" ].includes(level);
    }


    /**
     * Evaluates if the specified value is a valid log-level, i.e. a # between 0 and 5, inclusive
     *
     * @param {any} level
     * @type {SpmhTypeValidationResult<WebpackLogLevel>}
     */
    isWpLogLevel(level)
    {
        return WpwLogger.isWpLogLevel(level);
    }


    /**
     * @param {WpwLoggerLevel | WebpackLogLevel | undefined} level
     * @returns {WpwLoggerLevel}
     */
    static toWpwLogLevel(level)
    {
        if (this.isWpwLogLevel(level)) {
            return level;
        }
        else if (this.isWpLogLevel(level))
        {   if (level === "error") { return 1; }
            if (level === "warn") { return 2; }
            if (level === "info") { return 3; }
            if (level === "log") { return 4; }
            if (level === "verbose") { return 5; }
        }
        return 2;
    };


    /**
     * @param {WpwLoggerLevel | WebpackLogLevel | undefined} level
     * @returns {WpwLoggerLevel}
     */
    toWpwLogLevel(level) { return WpwLogger.toWpwLogLevel(level); }


    /**
     * Evaluates if the specified value is a valid log-level, i.e. a # between 0 and 5, inclusive
     *
     * @param {WpwLoggerLevel | WebpackLogLevel | undefined} level
     * @returns {WebpackLogLevel}
     */
    static toWpLogLevel(level)
    {
        if (this.isWpLogLevel(level)) {
            return level;
        }
        else if (this.isWpwLogLevel(level))
        {   if (level === 0) { return "none"; }
            if (level === 1) { return "error"; }
            if (level === 2 || isNulled(level)) { return "warn"; }
            if (level === 3) { return "info"; }
            if (level === 4) { return "log"; }
            if (level === 5) { return "verbose"; }
        }
        return "warn";
    }

    /**
     * Evaluates if the specified value is a valid log-level, i.e. a # between 0 and 5, inclusive
     *
     * @param {WpwLoggerLevel | WebpackLogLevel | undefined} level
     * @returns {WebpackLogLevel}
     */
    toWpLogLevel(level) { return WpwLogger.toWpLogLevel(level); }
}


module.exports = WpwLogger;