mirror of
https://github.com/debug-js/debug.git
synced 2026-01-18 16:12:38 +00:00
We modify the `namespaces` argument directly for some reason, which ends up saving input that ends up being invalid RegExp code upon the next `load()`+`enable()` call combo. Avoid that completely by just saving the input directly first thing. Fixes browsers since they actually use the "saved" value.
140 lines
2.5 KiB
JavaScript
140 lines
2.5 KiB
JavaScript
|
|
/**
|
|
* This is the common logic for both the Node.js and web browser
|
|
* implementations of `debug()`.
|
|
*
|
|
* Expose `debug()` as the module.
|
|
*/
|
|
|
|
exports = module.exports = debug;
|
|
exports.coerce = coerce;
|
|
exports.disable = disable;
|
|
exports.enable = enable;
|
|
exports.enabled = enabled;
|
|
exports.humanize = humanize;
|
|
|
|
/**
|
|
* The currently active debug mode names, and names to skip.
|
|
*/
|
|
|
|
exports.names = [];
|
|
exports.skips = [];
|
|
|
|
/**
|
|
* Create a debugger with the given `namespace`.
|
|
*
|
|
* @param {String} namespace
|
|
* @return {Function}
|
|
* @api public
|
|
*/
|
|
|
|
function debug(namespace) {
|
|
|
|
// define the `disabled` version
|
|
function disabled() {
|
|
}
|
|
disabled.enabled = false;
|
|
|
|
// define the `enabled` version
|
|
function enabled(fmt) {
|
|
fmt = exports.coerce(fmt);
|
|
exports.log.apply(enabled, arguments);
|
|
}
|
|
enabled.enabled = true;
|
|
|
|
var fn = exports.enabled(namespace) ? enabled : disabled;
|
|
|
|
fn.namespace = namespace;
|
|
|
|
return fn;
|
|
}
|
|
|
|
/**
|
|
* Enables a debug mode by namespaces. This can include modes
|
|
* separated by a colon and wildcards.
|
|
*
|
|
* @param {String} namespaces
|
|
* @api public
|
|
*/
|
|
|
|
function enable(namespaces) {
|
|
exports.save(namespaces);
|
|
|
|
var split = (namespaces || '').split(/[\s,]+/);
|
|
var len = split.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
namespaces = split[i].replace('*', '.*?');
|
|
if (namespaces[0] === '-') {
|
|
exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
|
|
} else {
|
|
exports.names.push(new RegExp('^' + namespaces + '$'));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Disable debug output.
|
|
*
|
|
* @api public
|
|
*/
|
|
|
|
function disable() {
|
|
exports.enable('');
|
|
}
|
|
|
|
/**
|
|
* Humanize the given `ms`.
|
|
*
|
|
* @param {Number} m
|
|
* @return {String}
|
|
* @api private
|
|
*/
|
|
|
|
function humanize(ms) {
|
|
var sec = 1000;
|
|
var min = 60 * 1000;
|
|
var hour = 60 * min;
|
|
|
|
if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
|
|
if (ms >= min) return (ms / min).toFixed(1) + 'm';
|
|
if (ms >= sec) return (ms / sec | 0) + 's';
|
|
return ms + 'ms';
|
|
}
|
|
|
|
/**
|
|
* Returns true if the given mode name is enabled, false otherwise.
|
|
*
|
|
* @param {String} name
|
|
* @return {Boolean}
|
|
* @api public
|
|
*/
|
|
|
|
function enabled(name) {
|
|
var i, len;
|
|
for (i = 0, len = exports.skips.length; i < len; i++) {
|
|
if (exports.skips[i].test(name)) {
|
|
return false;
|
|
}
|
|
}
|
|
for (i = 0, len = exports.names.length; i < len; i++) {
|
|
if (exports.names[i].test(name)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Coerce `val`.
|
|
*
|
|
* @param {Mixed} val
|
|
* @return {Mixed}
|
|
* @api private
|
|
*/
|
|
|
|
function coerce(val) {
|
|
if (val instanceof Error) return val.stack || val.message;
|
|
return val;
|
|
}
|