|
|
|
|
|
|
|
|
|
|
|
|
|
function setup(env) { |
|
createDebug.debug = createDebug; |
|
createDebug.default = createDebug; |
|
createDebug.coerce = coerce; |
|
createDebug.disable = disable; |
|
createDebug.enable = enable; |
|
createDebug.enabled = enabled; |
|
createDebug.humanize = require('ms'); |
|
createDebug.destroy = destroy; |
|
|
|
Object.keys(env).forEach(key => { |
|
createDebug[key] = env[key]; |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
createDebug.names = []; |
|
createDebug.skips = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
createDebug.formatters = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function selectColor(namespace) { |
|
let hash = 0; |
|
|
|
for (let i = 0; i < namespace.length; i++) { |
|
hash = ((hash << 5) - hash) + namespace.charCodeAt(i); |
|
hash |= 0; |
|
} |
|
|
|
return createDebug.colors[Math.abs(hash) % createDebug.colors.length]; |
|
} |
|
createDebug.selectColor = selectColor; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function createDebug(namespace) { |
|
let prevTime; |
|
let enableOverride = null; |
|
let namespacesCache; |
|
let enabledCache; |
|
|
|
function debug(...args) { |
|
|
|
if (!debug.enabled) { |
|
return; |
|
} |
|
|
|
const self = debug; |
|
|
|
|
|
const curr = Number(new Date()); |
|
const ms = curr - (prevTime || curr); |
|
self.diff = ms; |
|
self.prev = prevTime; |
|
self.curr = curr; |
|
prevTime = curr; |
|
|
|
args[0] = createDebug.coerce(args[0]); |
|
|
|
if (typeof args[0] !== 'string') { |
|
|
|
args.unshift('%O'); |
|
} |
|
|
|
|
|
let index = 0; |
|
args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => { |
|
|
|
if (match === '%%') { |
|
return '%'; |
|
} |
|
index++; |
|
const formatter = createDebug.formatters[format]; |
|
if (typeof formatter === 'function') { |
|
const val = args[index]; |
|
match = formatter.call(self, val); |
|
|
|
|
|
args.splice(index, 1); |
|
index--; |
|
} |
|
return match; |
|
}); |
|
|
|
|
|
createDebug.formatArgs.call(self, args); |
|
|
|
const logFn = self.log || createDebug.log; |
|
logFn.apply(self, args); |
|
} |
|
|
|
debug.namespace = namespace; |
|
debug.useColors = createDebug.useColors(); |
|
debug.color = createDebug.selectColor(namespace); |
|
debug.extend = extend; |
|
debug.destroy = createDebug.destroy; |
|
|
|
Object.defineProperty(debug, 'enabled', { |
|
enumerable: true, |
|
configurable: false, |
|
get: () => { |
|
if (enableOverride !== null) { |
|
return enableOverride; |
|
} |
|
if (namespacesCache !== createDebug.namespaces) { |
|
namespacesCache = createDebug.namespaces; |
|
enabledCache = createDebug.enabled(namespace); |
|
} |
|
|
|
return enabledCache; |
|
}, |
|
set: v => { |
|
enableOverride = v; |
|
} |
|
}); |
|
|
|
|
|
if (typeof createDebug.init === 'function') { |
|
createDebug.init(debug); |
|
} |
|
|
|
return debug; |
|
} |
|
|
|
function extend(namespace, delimiter) { |
|
const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace); |
|
newDebug.log = this.log; |
|
return newDebug; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function enable(namespaces) { |
|
createDebug.save(namespaces); |
|
createDebug.namespaces = namespaces; |
|
|
|
createDebug.names = []; |
|
createDebug.skips = []; |
|
|
|
let i; |
|
const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/); |
|
const len = split.length; |
|
|
|
for (i = 0; i < len; i++) { |
|
if (!split[i]) { |
|
|
|
continue; |
|
} |
|
|
|
namespaces = split[i].replace(/\*/g, '.*?'); |
|
|
|
if (namespaces[0] === '-') { |
|
createDebug.skips.push(new RegExp('^' + namespaces.slice(1) + '$')); |
|
} else { |
|
createDebug.names.push(new RegExp('^' + namespaces + '$')); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function disable() { |
|
const namespaces = [ |
|
...createDebug.names.map(toNamespace), |
|
...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace) |
|
].join(','); |
|
createDebug.enable(''); |
|
return namespaces; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function enabled(name) { |
|
if (name[name.length - 1] === '*') { |
|
return true; |
|
} |
|
|
|
let i; |
|
let len; |
|
|
|
for (i = 0, len = createDebug.skips.length; i < len; i++) { |
|
if (createDebug.skips[i].test(name)) { |
|
return false; |
|
} |
|
} |
|
|
|
for (i = 0, len = createDebug.names.length; i < len; i++) { |
|
if (createDebug.names[i].test(name)) { |
|
return true; |
|
} |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function toNamespace(regexp) { |
|
return regexp.toString() |
|
.substring(2, regexp.toString().length - 2) |
|
.replace(/\.\*\?$/, '*'); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function coerce(val) { |
|
if (val instanceof Error) { |
|
return val.stack || val.message; |
|
} |
|
return val; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function destroy() { |
|
console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'); |
|
} |
|
|
|
createDebug.enable(createDebug.load()); |
|
|
|
return createDebug; |
|
} |
|
|
|
module.exports = setup; |
|
|