5 var tty = require('tty');
8 * Expose `debug()` as the module.
11 module.exports = debug;
20 (process.env.DEBUG || '')
22 .forEach(function(name){
23 name = name.replace('*', '.*?');
24 if (name[0] === '-') {
25 skips.push(new RegExp('^' + name.substr(1) + '$'));
27 names.push(new RegExp('^' + name + '$'));
35 var colors = [6, 2, 3, 4, 5, 1];
38 * Previous debug() call.
44 * Previously assigned color.
50 * Is stdout a TTY? Colored output is disabled when `true`.
53 var isatty = tty.isatty(2);
63 return colors[prevColor++ % colors.length];
67 * Humanize the given `ms`.
74 function humanize(ms) {
79 if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
80 if (ms >= min) return (ms / min).toFixed(1) + 'm';
81 if (ms >= sec) return (ms / sec | 0) + 's';
86 * Create a debugger with the given `name`.
88 * @param {String} name
93 function debug(name) {
95 disabled.enabled = false;
97 var match = skips.some(function(re){
101 if (match) return disabled;
103 match = names.some(function(re){
104 return re.test(name);
107 if (!match) return disabled;
110 function colored(fmt) {
114 var ms = curr - (prev[name] || curr);
117 fmt = ' \u001b[9' + c + 'm' + name + ' '
118 + '\u001b[3' + c + 'm\u001b[90m'
119 + fmt + '\u001b[3' + c + 'm'
120 + ' +' + humanize(ms) + '\u001b[0m';
122 console.error.apply(this, arguments);
125 function plain(fmt) {
128 fmt = new Date().toUTCString()
129 + ' ' + name + ' ' + fmt;
130 console.error.apply(this, arguments);
133 colored.enabled = plain.enabled = true;
135 return isatty || process.env.DEBUG_COLORS
144 function coerce(val) {
145 if (val instanceof Error) return val.stack || val.message;