'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var tslib = require('tslib');
var util = _interopDefault(require('util'));
var os = require('os');

// Copyright (c) Microsoft Corporation.
function log(message) {
    var args = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        args[_i - 1] = arguments[_i];
    }
    process.stderr.write("" + util.format.apply(util, tslib.__spread([message], args)) + os.EOL);
}

// Copyright (c) Microsoft Corporation.
var debugEnvVariable = (typeof process !== "undefined" && process.env && process.env.DEBUG) || undefined;
var enabledString;
var enabledNamespaces = [];
var skippedNamespaces = [];
var debuggers = [];
if (debugEnvVariable) {
    enable(debugEnvVariable);
}
var debugObj = Object.assign(function (namespace) {
    return createDebugger(namespace);
}, {
    enable: enable,
    enabled: enabled,
    disable: disable,
    log: log
});
function enable(namespaces) {
    var e_1, _a, e_2, _b;
    enabledString = namespaces;
    enabledNamespaces = [];
    skippedNamespaces = [];
    var wildcard = /\*/g;
    var namespaceList = namespaces.split(",").map(function (ns) { return ns.trim().replace(wildcard, ".*?"); });
    try {
        for (var namespaceList_1 = tslib.__values(namespaceList), namespaceList_1_1 = namespaceList_1.next(); !namespaceList_1_1.done; namespaceList_1_1 = namespaceList_1.next()) {
            var ns = namespaceList_1_1.value;
            if (ns.startsWith("-")) {
                skippedNamespaces.push(new RegExp("^" + ns.substr(1) + "$"));
            }
            else {
                enabledNamespaces.push(new RegExp("^" + ns + "$"));
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (namespaceList_1_1 && !namespaceList_1_1.done && (_a = namespaceList_1.return)) _a.call(namespaceList_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    try {
        for (var debuggers_1 = tslib.__values(debuggers), debuggers_1_1 = debuggers_1.next(); !debuggers_1_1.done; debuggers_1_1 = debuggers_1.next()) {
            var instance = debuggers_1_1.value;
            instance.enabled = enabled(instance.namespace);
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (debuggers_1_1 && !debuggers_1_1.done && (_b = debuggers_1.return)) _b.call(debuggers_1);
        }
        finally { if (e_2) throw e_2.error; }
    }
}
function enabled(namespace) {
    var e_3, _a, e_4, _b;
    if (namespace.endsWith("*")) {
        return true;
    }
    try {
        for (var skippedNamespaces_1 = tslib.__values(skippedNamespaces), skippedNamespaces_1_1 = skippedNamespaces_1.next(); !skippedNamespaces_1_1.done; skippedNamespaces_1_1 = skippedNamespaces_1.next()) {
            var skipped = skippedNamespaces_1_1.value;
            if (skipped.test(namespace)) {
                return false;
            }
        }
    }
    catch (e_3_1) { e_3 = { error: e_3_1 }; }
    finally {
        try {
            if (skippedNamespaces_1_1 && !skippedNamespaces_1_1.done && (_a = skippedNamespaces_1.return)) _a.call(skippedNamespaces_1);
        }
        finally { if (e_3) throw e_3.error; }
    }
    try {
        for (var enabledNamespaces_1 = tslib.__values(enabledNamespaces), enabledNamespaces_1_1 = enabledNamespaces_1.next(); !enabledNamespaces_1_1.done; enabledNamespaces_1_1 = enabledNamespaces_1.next()) {
            var enabledNamespace = enabledNamespaces_1_1.value;
            if (enabledNamespace.test(namespace)) {
                return true;
            }
        }
    }
    catch (e_4_1) { e_4 = { error: e_4_1 }; }
    finally {
        try {
            if (enabledNamespaces_1_1 && !enabledNamespaces_1_1.done && (_b = enabledNamespaces_1.return)) _b.call(enabledNamespaces_1);
        }
        finally { if (e_4) throw e_4.error; }
    }
    return false;
}
function disable() {
    var result = enabledString || "";
    enable("");
    return result;
}
function createDebugger(namespace) {
    var newDebugger = Object.assign(debug, {
        enabled: enabled(namespace),
        destroy: destroy,
        log: debugObj.log,
        namespace: namespace,
        extend: extend
    });
    function debug() {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        if (!newDebugger.enabled) {
            return;
        }
        if (args.length > 0) {
            args[0] = namespace + " " + args[0];
        }
        newDebugger.log.apply(newDebugger, tslib.__spread(args));
    }
    debuggers.push(newDebugger);
    return newDebugger;
}
function destroy() {
    var index = debuggers.indexOf(this);
    if (index >= 0) {
        debuggers.splice(index, 1);
        return true;
    }
    return false;
}
function extend(namespace) {
    var newDebugger = createDebugger(this.namespace + ":" + namespace);
    newDebugger.log = this.log;
    return newDebugger;
}

// Copyright (c) Microsoft Corporation.
var registeredLoggers = new Set();
var logLevelFromEnv = (typeof process !== "undefined" && process.env && process.env.AZURE_LOG_LEVEL) || undefined;
var azureLogLevel;
/**
 * The AzureLogger provides a mechanism for overriding where logs are output to.
 * By default, logs are sent to stderr.
 * Override the `log` method to redirect logs to another location.
 */
var AzureLogger = debugObj("azure");
AzureLogger.log = function () {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    debugObj.log.apply(debugObj, tslib.__spread(args));
};
var AZURE_LOG_LEVELS = ["verbose", "info", "warning", "error"];
if (logLevelFromEnv) {
    // avoid calling setLogLevel because we don't want a mis-set environment variable to crash
    if (isAzureLogLevel(logLevelFromEnv)) {
        setLogLevel(logLevelFromEnv);
    }
    else {
        console.error("AZURE_LOG_LEVEL set to unknown log level '" + logLevelFromEnv + "'; logging is not enabled. Acceptable values: " + AZURE_LOG_LEVELS.join(", ") + ".");
    }
}
/**
 * Immediately enables logging at the specified log level.
 * @param level - The log level to enable for logging.
 * Options from most verbose to least verbose are:
 * - verbose
 * - info
 * - warning
 * - error
 */
function setLogLevel(level) {
    var e_1, _a;
    if (level && !isAzureLogLevel(level)) {
        throw new Error("Unknown log level '" + level + "'. Acceptable values: " + AZURE_LOG_LEVELS.join(","));
    }
    azureLogLevel = level;
    var enabledNamespaces = [];
    try {
        for (var registeredLoggers_1 = tslib.__values(registeredLoggers), registeredLoggers_1_1 = registeredLoggers_1.next(); !registeredLoggers_1_1.done; registeredLoggers_1_1 = registeredLoggers_1.next()) {
            var logger = registeredLoggers_1_1.value;
            if (shouldEnable(logger)) {
                enabledNamespaces.push(logger.namespace);
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (registeredLoggers_1_1 && !registeredLoggers_1_1.done && (_a = registeredLoggers_1.return)) _a.call(registeredLoggers_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    debugObj.enable(enabledNamespaces.join(","));
}
/**
 * Retrieves the currently specified log level.
 */
function getLogLevel() {
    return azureLogLevel;
}
var levelMap = {
    verbose: 400,
    info: 300,
    warning: 200,
    error: 100
};
/**
 * Creates a logger for use by the Azure SDKs that inherits from `AzureLogger`.
 * @param namespace - The name of the SDK package.
 * @hidden
 */
function createClientLogger(namespace) {
    var clientRootLogger = AzureLogger.extend(namespace);
    patchLogMethod(AzureLogger, clientRootLogger);
    return {
        error: createLogger(clientRootLogger, "error"),
        warning: createLogger(clientRootLogger, "warning"),
        info: createLogger(clientRootLogger, "info"),
        verbose: createLogger(clientRootLogger, "verbose")
    };
}
function patchLogMethod(parent, child) {
    child.log = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        parent.log.apply(parent, tslib.__spread(args));
    };
}
function createLogger(parent, level) {
    var logger = Object.assign(parent.extend(level), {
        level: level
    });
    patchLogMethod(parent, logger);
    if (shouldEnable(logger)) {
        var enabledNamespaces = debugObj.disable();
        debugObj.enable(enabledNamespaces + "," + logger.namespace);
    }
    registeredLoggers.add(logger);
    return logger;
}
function shouldEnable(logger) {
    if (azureLogLevel && levelMap[logger.level] <= levelMap[azureLogLevel]) {
        return true;
    }
    else {
        return false;
    }
}
function isAzureLogLevel(logLevel) {
    return AZURE_LOG_LEVELS.includes(logLevel);
}

exports.AzureLogger = AzureLogger;
exports.createClientLogger = createClientLogger;
exports.getLogLevel = getLogLevel;
exports.setLogLevel = setLogLevel;
//# sourceMappingURL=index.js.map
