"use strict";
exports.id = 7554;
exports.ids = [7554];
exports.modules = {

/***/ 227554:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


/*
Debug logger for any node.js server.

There is used both by the hub(s) and project(s).

This is an implementation of basically how winston works for us,
but using the vastly simpler super-popular debug module.
*/
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.setCounter = exports.getLogger = void 0;
var debug_1 = __importDefault(__webpack_require__(511050));
var fs_1 = __webpack_require__(657147);
var util_1 = __webpack_require__(473837);
var path_1 = __webpack_require__(371017);
var data_1 = __webpack_require__(221945);
process.env.DEBUG_HIDE_DATE = "yes"; // since we supply it ourselves
var COCALC = (0, debug_1.default)("cocalc");
function myFormat() {
    var e_1, _a;
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    if (args.length > 1 && typeof args[0] == "string" && !args[0].includes("%")) {
        // This is something where we didn't use printf formatting.
        var v = [];
        try {
            for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
                var x = args_1_1.value;
                try {
                    v.push(typeof x == "object" ? JSON.stringify(x) : "".concat(x));
                }
                catch (_) {
                    // better to not crash everything just for logging
                    v.push("".concat(x));
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return v.join(" ");
    }
    // use printf formatting.
    return util_1.format.apply(void 0, __spreadArray([], __read(args), false));
}
function defaultTransports() {
    if (process.env.SMC_TEST) {
        return {};
    }
    else if (process.env.COCALC_DOCKER) {
        return { file: "/var/log/hub/log" };
    }
    else if (true) {
        return { console: true };
    }
    else {}
}
function initTransports() {
    if (!process.env.DEBUG) {
        // if DEBUG isn't set then the debug logger will be silent anyways.
        return;
    }
    var transports = defaultTransports();
    if (process.env.DEBUG_CONSOLE != null) {
        transports.console =
            process.env.DEBUG_CONSOLE != "no" && process.env.DEBUG_CONSOLE != "false";
    }
    if (process.env.DEBUG_FILE != null) {
        transports.file = process.env.DEBUG_FILE;
    }
    var fileStream;
    if (transports.file) {
        // ensure directory exists
        (0, fs_1.mkdirSync)((0, path_1.dirname)(transports.file), { recursive: true });
        // create the file stream; using a stream ensures
        // that everything is written in the right order with
        // no corruption/collision between different logging.
        fileStream = (0, fs_1.createWriteStream)(transports.file);
    }
    var firstLog = true;
    COCALC.log = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        if (!transports.file && !transports.console)
            return;
        if (firstLog && transports.file) {
            console.error("***\n\nLogging to \"".concat(transports.file, "\"").concat(transports.console ? " and console.error" : "", " via the debug module\nwith  DEBUG='").concat(process.env.DEBUG, "'.\nUse   DEBUG_FILE='path' and DEBUG_CONSOLE=[yes|no] to override.\nUsing DEBUG='cocalc:*,-cocalc:silly:*' to control log levels.\n\n***"));
            firstLog = false;
        }
        // Similar as in debug source code, except I stuck a timestamp
        // at the beginning, which I like... except also aware of
        // non-printf formatting.
        var line = "".concat(new Date().toISOString(), ": ").concat(myFormat.apply(void 0, __spreadArray([], __read(args), false)), "\n");
        if (transports.console) {
            // the console transport:
            console.error(line);
        }
        if (transports.file) {
            // the file transport
            fileStream.write(line);
        }
    };
}
initTransports();
var DEBUGGERS = {
    error: COCALC.extend("error"),
    warn: COCALC.extend("warn"),
    info: COCALC.extend("info"),
    http: COCALC.extend("http"),
    verbose: COCALC.extend("verbose"),
    debug: COCALC.extend("debug"),
    silly: COCALC.extend("silly"),
};
var LEVELS = [
    "error",
    "warn",
    "info",
    "http",
    "verbose",
    "debug",
    "silly",
];
var Logger = /** @class */ (function () {
    function Logger(name) {
        var e_2, _a;
        var _this = this;
        this.debuggers = {};
        this.name = name;
        var _loop_1 = function (level) {
            this_1.debuggers[level] = DEBUGGERS[level].extend(name);
            this_1[level] = function () {
                var _a;
                var args = [];
                for (var _i = 0; _i < arguments.length; _i++) {
                    args[_i] = arguments[_i];
                }
                _this.counter("error");
                // @ts-ignore
                (_a = _this.debuggers)[level].apply(_a, __spreadArray([], __read(args), false));
            };
        };
        var this_1 = this;
        try {
            for (var LEVELS_1 = __values(LEVELS), LEVELS_1_1 = LEVELS_1.next(); !LEVELS_1_1.done; LEVELS_1_1 = LEVELS_1.next()) {
                var level = LEVELS_1_1.value;
                _loop_1(level);
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (LEVELS_1_1 && !LEVELS_1_1.done && (_a = LEVELS_1.return)) _a.call(LEVELS_1);
            }
            finally { if (e_2) throw e_2.error; }
        }
    }
    Logger.prototype.extend = function (name) {
        return new Logger("".concat(this.name, ":").concat(name));
    };
    Logger.prototype.counter = function (level) {
        if (counter == null)
            return;
        counter.labels(this.name, level).inc(1);
    };
    return Logger;
}());
var cache = {};
function getLogger(name) {
    if (cache[name] != null) {
        return cache[name];
    }
    // smash it over since we build Logger pretty generically so typescript
    // doesn't get it.  But we care that all *client* code uses the WinstonLogger
    // interface.
    return (cache[name] = new Logger(name));
}
exports["default"] = getLogger;
exports.getLogger = getLogger;
var counter = undefined;
function setCounter(f) {
    counter = f;
}
exports.setCounter = setCounter;
//# sourceMappingURL=logger.js.map

/***/ })

};
;