debug/debug.js
2012-02-01 12:18:13 -08:00

91 lines
1.7 KiB
JavaScript

/*!
* debug
* Copyright(c) 2012 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Create a debugger with the given `name`.
*
* @param {String} name
* @return {Type}
* @api public
*/
function debug(name) {
if (!debug.enabled(name)) return function(){};
function plain(fmt) {
var curr = new Date;
var ms = curr - (debug[name] || curr);
debug[name] = curr;
fmt = name
+ ' '
+ fmt
+ ' ' + debug.pad(ms, 40 - fmt.length)
+ 'ms';
// This hackery is required for IE8, where `console.log` doesn't have 'apply'
window.console && console.log &&
Function.prototype.apply.call(console.log, console, arguments);
}
return plain;
}
/**
* The currently active debug mode names.
*/
debug.names = [];
/**
* Pad the given `str` to `len`.
*
* @param {String} str
* @param {String} len
* @return {String}
* @api private
*/
debug.pad = function(str, len) {
return Array(Math.max(len, 1)).join(' ') + str;
};
/**
* Enables a debug mode by name. This can include modes
* separated by a colon and wildcards.
*
* @param {String} name
* @api public
*/
debug.enable = function(name) {
var split = (name || '').split(/[\s,]+/)
, len = split.length;
for (var i = 0; i < len; i++) {
name = split[i].replace('*', '.*?');
debug.names.push(new RegExp('^' + name + '$'));
}
};
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
debug.enabled = function(name) {
for (var i = 0, len = debug.names.length; i < len; i++) {
if (debug.names[i].test(name)) {
return true;
}
}
return false;
};