Initial commit
[yaffs-website] / node_modules / debug / debug.js
1
2 /**
3  * This is the common logic for both the Node.js and web browser
4  * implementations of `debug()`.
5  *
6  * Expose `debug()` as the module.
7  */
8
9 exports = module.exports = debug;
10 exports.coerce = coerce;
11 exports.disable = disable;
12 exports.enable = enable;
13 exports.enabled = enabled;
14 exports.humanize = require('ms');
15
16 /**
17  * The currently active debug mode names, and names to skip.
18  */
19
20 exports.names = [];
21 exports.skips = [];
22
23 /**
24  * Map of special "%n" handling functions, for the debug "format" argument.
25  *
26  * Valid key names are a single, lowercased letter, i.e. "n".
27  */
28
29 exports.formatters = {};
30
31 /**
32  * Previously assigned color.
33  */
34
35 var prevColor = 0;
36
37 /**
38  * Previous log timestamp.
39  */
40
41 var prevTime;
42
43 /**
44  * Select a color.
45  *
46  * @return {Number}
47  * @api private
48  */
49
50 function selectColor() {
51   return exports.colors[prevColor++ % exports.colors.length];
52 }
53
54 /**
55  * Create a debugger with the given `namespace`.
56  *
57  * @param {String} namespace
58  * @return {Function}
59  * @api public
60  */
61
62 function debug(namespace) {
63
64   // define the `disabled` version
65   function disabled() {
66   }
67   disabled.enabled = false;
68
69   // define the `enabled` version
70   function enabled() {
71
72     var self = enabled;
73
74     // set `diff` timestamp
75     var curr = +new Date();
76     var ms = curr - (prevTime || curr);
77     self.diff = ms;
78     self.prev = prevTime;
79     self.curr = curr;
80     prevTime = curr;
81
82     // add the `color` if not set
83     if (null == self.useColors) self.useColors = exports.useColors();
84     if (null == self.color && self.useColors) self.color = selectColor();
85
86     var args = Array.prototype.slice.call(arguments);
87
88     args[0] = exports.coerce(args[0]);
89
90     if ('string' !== typeof args[0]) {
91       // anything else let's inspect with %o
92       args = ['%o'].concat(args);
93     }
94
95     // apply any `formatters` transformations
96     var index = 0;
97     args[0] = args[0].replace(/%([a-z%])/g, function(match, format) {
98       // if we encounter an escaped % then don't increase the array index
99       if (match === '%%') return match;
100       index++;
101       var formatter = exports.formatters[format];
102       if ('function' === typeof formatter) {
103         var val = args[index];
104         match = formatter.call(self, val);
105
106         // now we need to remove `args[index]` since it's inlined in the `format`
107         args.splice(index, 1);
108         index--;
109       }
110       return match;
111     });
112
113     if ('function' === typeof exports.formatArgs) {
114       args = exports.formatArgs.apply(self, args);
115     }
116     var logFn = enabled.log || exports.log || console.log.bind(console);
117     logFn.apply(self, args);
118   }
119   enabled.enabled = true;
120
121   var fn = exports.enabled(namespace) ? enabled : disabled;
122
123   fn.namespace = namespace;
124
125   return fn;
126 }
127
128 /**
129  * Enables a debug mode by namespaces. This can include modes
130  * separated by a colon and wildcards.
131  *
132  * @param {String} namespaces
133  * @api public
134  */
135
136 function enable(namespaces) {
137   exports.save(namespaces);
138
139   var split = (namespaces || '').split(/[\s,]+/);
140   var len = split.length;
141
142   for (var i = 0; i < len; i++) {
143     if (!split[i]) continue; // ignore empty strings
144     namespaces = split[i].replace(/\*/g, '.*?');
145     if (namespaces[0] === '-') {
146       exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
147     } else {
148       exports.names.push(new RegExp('^' + namespaces + '$'));
149     }
150   }
151 }
152
153 /**
154  * Disable debug output.
155  *
156  * @api public
157  */
158
159 function disable() {
160   exports.enable('');
161 }
162
163 /**
164  * Returns true if the given mode name is enabled, false otherwise.
165  *
166  * @param {String} name
167  * @return {Boolean}
168  * @api public
169  */
170
171 function enabled(name) {
172   var i, len;
173   for (i = 0, len = exports.skips.length; i < len; i++) {
174     if (exports.skips[i].test(name)) {
175       return false;
176     }
177   }
178   for (i = 0, len = exports.names.length; i < len; i++) {
179     if (exports.names[i].test(name)) {
180       return true;
181     }
182   }
183   return false;
184 }
185
186 /**
187  * Coerce `val`.
188  *
189  * @param {Mixed} val
190  * @return {Mixed}
191  * @api private
192  */
193
194 function coerce(val) {
195   if (val instanceof Error) return val.stack || val.message;
196   return val;
197 }