comunicationLeap/node_modules/engine.io-client/node_modules/debug/lib/debug.js
node_modules/engine.io-client/node_modules/debug/lib/debug.jsRaw
1/**
2 * Module dependencies.
3 */
4
5var tty = require('tty');
6
7/**
8 * Expose `debug()` as the module.
9 */
10
11module.exports = debug;
12
13/**
14 * Enabled debuggers.
15 */
16
17var names = []
18 , skips = [];
19
20(process.env.DEBUG || '')
21 .split(/[\s,]+/)
22 .forEach(function(name){
23 name = name.replace('*', '.*?');
24 if (name[0] === '-') {
25 skips.push(new RegExp('^' + name.substr(1) + '$'));
26 } else {
27 names.push(new RegExp('^' + name + '$'));
28 }
29 });
30
31/**
32 * Colors.
33 */
34
35var colors = [6, 2, 3, 4, 5, 1];
36
37/**
38 * Previous debug() call.
39 */
40
41var prev = {};
42
43/**
44 * Previously assigned color.
45 */
46
47var prevColor = 0;
48
49/**
50 * Is stdout a TTY? Colored output is disabled when `true`.
51 */
52
53var isatty = tty.isatty(2);
54
55/**
56 * Select a color.
57 *
58 * @return {Number}
59 * @api private
60 */
61
62function color() {
63 return colors[prevColor++ % colors.length];
64}
65
66/**
67 * Humanize the given `ms`.
68 *
69 * @param {Number} m
70 * @return {String}
71 * @api private
72 */
73
74function humanize(ms) {
75 var sec = 1000
76 , min = 60 * 1000
77 , hour = 60 * min;
78
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';
82 return ms + 'ms';
83}
84
85/**
86 * Create a debugger with the given `name`.
87 *
88 * @param {String} name
89 * @return {Type}
90 * @api public
91 */
92
93function debug(name) {
94 function disabled(){}
95 disabled.enabled = false;
96
97 var match = skips.some(function(re){
98 return re.test(name);
99 });
100
101 if (match) return disabled;
102
103 match = names.some(function(re){
104 return re.test(name);
105 });
106
107 if (!match) return disabled;
108 var c = color();
109
110 function colored(fmt) {
111 fmt = coerce(fmt);
112
113 var curr = new Date;
114 var ms = curr - (prev[name] || curr);
115 prev[name] = curr;
116
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';
121
122 console.error.apply(this, arguments);
123 }
124
125 function plain(fmt) {
126 fmt = coerce(fmt);
127
128 fmt = new Date().toUTCString()
129 + ' ' + name + ' ' + fmt;
130 console.error.apply(this, arguments);
131 }
132
133 colored.enabled = plain.enabled = true;
134
135 return isatty || process.env.DEBUG_COLORS
136 ? colored
137 : plain;
138}
139
140/**
141 * Coerce `val`.
142 */
143
144function coerce(val) {
145 if (val instanceof Error) return val.stack || val.message;
146 return val;
147}