"use strict";
/*
 * Tencent is pleased to support the open source community by making
 * Hippy available.
 *
 * Copyright (C) 2017-2019 THL A29 Limited, a Tencent company.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserLogger = exports.TunnelLogger = exports.Logger = void 0;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const util_1 = tslib_1.__importDefault(require("util"));
const safe_1 = tslib_1.__importDefault(require("colors/safe"));
const winston_1 = require("winston");
const lodash_1 = require("lodash");
const enum_1 = require("@debug-server-next/@types/enum");
const config_1 = require("@debug-server-next/config");
const aegis_1 = require("@debug-server-next/utils/aegis");
require("winston-daily-rotate-file");
class Logger {
    constructor(label = '', color, logFilename, hideInConsole) {
        this.label = label;
        this.color = color || getRandomColor();
        this.logFilename = logFilename || '%DATE%.log';
        this.hideInConsole = hideInConsole;
        this.initLoggerInstance();
    }
    info(...args) {
        this.log(enum_1.LogLevel.Info, ...args);
    }
    verbose(...args) {
        this.log(enum_1.LogLevel.Verbose, ...args);
    }
    debug(...args) {
        this.log(enum_1.LogLevel.Debug, ...args);
    }
    silly(...args) {
        this.log(enum_1.LogLevel.Silly, ...args);
    }
    warn(...args) {
        this.log(enum_1.LogLevel.Warn, ...args);
    }
    error(...args) {
        this.log(enum_1.LogLevel.Error, ...args);
    }
    log(level, ...args) {
        const msg = util_1.default.format(...args);
        this.loggerInstance.log(level, msg);
        try {
            if (level === enum_1.LogLevel.Error) {
                aegis_1.aegis.report(new Error(msg));
            }
            else if ([enum_1.LogLevel.Warn].includes(level)) {
                aegis_1.aegis.infoAll(msg);
            }
        }
        catch (e) { }
    }
    initLoggerInstance() {
        var _a;
        const label = safe_1.default[this.color](this.label);
        this.loggerInstance = winston_1.createLogger({
            format: winston_1.format.combine(winston_1.format.errors({ stack: true }), winston_1.format.label({ label }), winston_1.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }), winston_1.format.colorize(), winston_1.format.printf(({ level, message, label, timestamp }) => `${timestamp} ${label} ${level} ${message}`)),
            transports: [
                new winston_1.transports.DailyRotateFile({
                    filename: path_1.default.join(config_1.config.logPath, this.logFilename),
                    datePattern: 'YYYY-MM-DD-HH',
                    zippedArchive: false,
                    maxSize: '20m',
                    maxFiles: '7d',
                    level: enum_1.LogLevel.Verbose,
                }),
                !this.hideInConsole &&
                    new winston_1.transports.Console({
                        level: ((_a = global.debugAppArgv) === null || _a === void 0 ? void 0 : _a.log) || enum_1.LogLevel.Info,
                    }),
            ].filter(Boolean),
        });
    }
}
exports.Logger = Logger;
class TunnelLogger extends Logger {
    constructor(label = '', color, logFilename) {
        super(label, color, logFilename || '%DATE%.tunnel.log', true);
    }
}
exports.TunnelLogger = TunnelLogger;
class UserLogger extends Logger {
}
exports.UserLogger = UserLogger;
const winstonColors = lodash_1.uniq(Object.values(enum_1.WinstonColor).map(lodash_1.lowerFirst));
function getRandomColor() {
    return winstonColors[lodash_1.random(winstonColors.length - 1)];
}
