mirror of
https://github.com/debug-js/debug.git
synced 2025-12-08 20:59:48 +00:00
122 lines
1.8 KiB
JavaScript
122 lines
1.8 KiB
JavaScript
|
|
/*!
|
|
* debug
|
|
* Copyright(c) 2012 TJ Holowaychuk <tj@vision-media.ca>
|
|
* MIT Licensed
|
|
*/
|
|
|
|
/**
|
|
* Module dependencies.
|
|
*/
|
|
|
|
var tty = require('tty');
|
|
|
|
/**
|
|
* Expose `debug()` as the module.
|
|
*/
|
|
|
|
module.exports = debug;
|
|
|
|
/**
|
|
* Library version.
|
|
*/
|
|
|
|
exports.version = '0.3.0';
|
|
|
|
/**
|
|
* Enabled debuggers.
|
|
*/
|
|
|
|
var names = (process.env.DEBUG || '')
|
|
.split(/[\s,]+/)
|
|
.map(function(name){
|
|
name = name.replace('*', '.*?');
|
|
return new RegExp('^' + name + '$');
|
|
});
|
|
|
|
/**
|
|
* Colors.
|
|
*/
|
|
|
|
var colors = [6, 2, 3, 4, 5, 1];
|
|
|
|
/**
|
|
* Previous debug() call.
|
|
*/
|
|
|
|
var prev = {};
|
|
|
|
/**
|
|
* Previously assigned color.
|
|
*/
|
|
|
|
var prevColor = 0;
|
|
|
|
/**
|
|
* Is stdout a TTY? Colored output is disabled when `true`.
|
|
*/
|
|
|
|
var isatty = tty.isatty(1);
|
|
|
|
/**
|
|
* Select a color.
|
|
*
|
|
* @return {Number}
|
|
* @api private
|
|
*/
|
|
|
|
function color() {
|
|
return colors[prevColor++ % colors.length];
|
|
}
|
|
|
|
/**
|
|
* Pad the given `str` to `len`.
|
|
*
|
|
* @param {String} str
|
|
* @param {String} len
|
|
* @return {String}
|
|
* @api private
|
|
*/
|
|
|
|
function pad(str, len) {
|
|
return Array(Math.max(len, 1)).join(' ') + str;
|
|
}
|
|
|
|
/**
|
|
* Create a debugger with the given `name`.
|
|
*
|
|
* @param {String} name
|
|
* @return {Type}
|
|
* @api public
|
|
*/
|
|
|
|
function debug(name) {
|
|
var match = names.some(function(re){
|
|
return re.test(name);
|
|
});
|
|
|
|
if (!match) return function(){};
|
|
var c = color();
|
|
|
|
function colored(fmt) {
|
|
var curr = new Date;
|
|
var ms = curr - (prev[name] || curr);
|
|
prev[name] = curr;
|
|
|
|
fmt = ' \033[9' + c + 'm' + name + ' '
|
|
+ '\033[3' + c + 'm\033[90m'
|
|
+ fmt + '\033[3' + c + 'm'
|
|
+ ' ' + pad(ms, 40 - fmt.length) + 'ms\033[0m';
|
|
|
|
console.log.apply(this, arguments);
|
|
}
|
|
|
|
function plain(fmt) {
|
|
fmt = new Date().toUTCString()
|
|
+ ' ' + name + ' ' + fmt;
|
|
console.log.apply(this, arguments);
|
|
}
|
|
|
|
return isatty ? colored : plain;
|
|
}
|