3 * This is the common logic for both the Node.js and web browser
4 * implementations of `debug()`.
6 * Expose `debug()` as the module.
9 exports = module.exports = createDebug.debug = createDebug['default'] = createDebug;
10 exports.coerce = coerce;
11 exports.disable = disable;
12 exports.enable = enable;
13 exports.enabled = enabled;
14 exports.humanize = require('ms');
17 * Active `debug` instances.
19 exports.instances = [];
22 * The currently active debug mode names, and names to skip.
29 * Map of special "%n" handling functions, for the debug "format" argument.
31 * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
34 exports.formatters = {};
38 * @param {String} namespace
43 function selectColor(namespace) {
46 for (i in namespace) {
47 hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
48 hash |= 0; // Convert to 32bit integer
51 return exports.colors[Math.abs(hash) % exports.colors.length];
55 * Create a debugger with the given `namespace`.
57 * @param {String} namespace
62 function createDebug(namespace) {
68 if (!debug.enabled) return;
72 // set `diff` timestamp
73 var curr = +new Date();
74 var ms = curr - (prevTime || curr);
80 // turn the `arguments` into a proper Array
81 var args = new Array(arguments.length);
82 for (var i = 0; i < args.length; i++) {
83 args[i] = arguments[i];
86 args[0] = exports.coerce(args[0]);
88 if ('string' !== typeof args[0]) {
89 // anything else let's inspect with %O
93 // apply any `formatters` transformations
95 args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) {
96 // if we encounter an escaped % then don't increase the array index
97 if (match === '%%') return match;
99 var formatter = exports.formatters[format];
100 if ('function' === typeof formatter) {
101 var val = args[index];
102 match = formatter.call(self, val);
104 // now we need to remove `args[index]` since it's inlined in the `format`
105 args.splice(index, 1);
111 // apply env-specific formatting (colors, etc.)
112 exports.formatArgs.call(self, args);
114 var logFn = debug.log || exports.log || console.log.bind(console);
115 logFn.apply(self, args);
118 debug.namespace = namespace;
119 debug.enabled = exports.enabled(namespace);
120 debug.useColors = exports.useColors();
121 debug.color = selectColor(namespace);
122 debug.destroy = destroy;
124 // env-specific initialization logic for debug instances
125 if ('function' === typeof exports.init) {
129 exports.instances.push(debug);
134 function destroy () {
135 var index = exports.instances.indexOf(this);
137 exports.instances.splice(index, 1);
145 * Enables a debug mode by namespaces. This can include modes
146 * separated by a colon and wildcards.
148 * @param {String} namespaces
152 function enable(namespaces) {
153 exports.save(namespaces);
159 var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
160 var len = split.length;
162 for (i = 0; i < len; i++) {
163 if (!split[i]) continue; // ignore empty strings
164 namespaces = split[i].replace(/\*/g, '.*?');
165 if (namespaces[0] === '-') {
166 exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
168 exports.names.push(new RegExp('^' + namespaces + '$'));
172 for (i = 0; i < exports.instances.length; i++) {
173 var instance = exports.instances[i];
174 instance.enabled = exports.enabled(instance.namespace);
179 * Disable debug output.
189 * Returns true if the given mode name is enabled, false otherwise.
191 * @param {String} name
196 function enabled(name) {
197 if (name[name.length - 1] === '*') {
201 for (i = 0, len = exports.skips.length; i < len; i++) {
202 if (exports.skips[i].test(name)) {
206 for (i = 0, len = exports.names.length; i < len; i++) {
207 if (exports.names[i].test(name)) {
222 function coerce(val) {
223 if (val instanceof Error) return val.stack || val.message;