3 * This is the common logic for both the Node.js and web browser
4 * implementations of `debug()`.
8 createDebug.debug = createDebug;
9 createDebug.default = createDebug;
10 createDebug.coerce = coerce;
11 createDebug.disable = disable;
12 createDebug.enable = enable;
13 createDebug.enabled = enabled;
14 createDebug.humanize = require('ms');
15 createDebug.destroy = destroy;
17 Object.keys(env).forEach(key => {
18 createDebug[key] = env[key];
22 * The currently active debug mode names, and names to skip.
25 createDebug.names = [];
26 createDebug.skips = [];
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".
33 createDebug.formatters = {};
36 * Selects a color for a debug namespace
37 * @param {String} namespace The namespace string for the debug instance to be colored
38 * @return {Number|String} An ANSI color code for the given namespace
41 function selectColor(namespace) {
44 for (let i = 0; i < namespace.length; i++) {
45 hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
46 hash |= 0; // Convert to 32bit integer
49 return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
51 createDebug.selectColor = selectColor;
54 * Create a debugger with the given `namespace`.
56 * @param {String} namespace
60 function createDebug(namespace) {
62 let enableOverride = null;
66 function debug(...args) {
74 // Set `diff` timestamp
75 const curr = Number(new Date());
76 const ms = curr - (prevTime || curr);
82 args[0] = createDebug.coerce(args[0]);
84 if (typeof args[0] !== 'string') {
85 // Anything else let's inspect with %O
89 // Apply any `formatters` transformations
91 args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
92 // If we encounter an escaped % then don't increase the array index
97 const formatter = createDebug.formatters[format];
98 if (typeof formatter === 'function') {
99 const val = args[index];
100 match = formatter.call(self, val);
102 // Now we need to remove `args[index]` since it's inlined in the `format`
103 args.splice(index, 1);
109 // Apply env-specific formatting (colors, etc.)
110 createDebug.formatArgs.call(self, args);
112 const logFn = self.log || createDebug.log;
113 logFn.apply(self, args);
116 debug.namespace = namespace;
117 debug.useColors = createDebug.useColors();
118 debug.color = createDebug.selectColor(namespace);
119 debug.extend = extend;
120 debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.
122 Object.defineProperty(debug, 'enabled', {
126 if (enableOverride !== null) {
127 return enableOverride;
129 if (namespacesCache !== createDebug.namespaces) {
130 namespacesCache = createDebug.namespaces;
131 enabledCache = createDebug.enabled(namespace);
141 // Env-specific initialization logic for debug instances
142 if (typeof createDebug.init === 'function') {
143 createDebug.init(debug);
149 function extend(namespace, delimiter) {
150 const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
151 newDebug.log = this.log;
156 * Enables a debug mode by namespaces. This can include modes
157 * separated by a colon and wildcards.
159 * @param {String} namespaces
162 function enable(namespaces) {
163 createDebug.save(namespaces);
164 createDebug.namespaces = namespaces;
166 createDebug.names = [];
167 createDebug.skips = [];
170 const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
171 const len = split.length;
173 for (i = 0; i < len; i++) {
175 // ignore empty strings
179 namespaces = split[i].replace(/\*/g, '.*?');
181 if (namespaces[0] === '-') {
182 createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
184 createDebug.names.push(new RegExp('^' + namespaces + '$'));
190 * Disable debug output.
192 * @return {String} namespaces
197 ...createDebug.names.map(toNamespace),
198 ...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace)
200 createDebug.enable('');
205 * Returns true if the given mode name is enabled, false otherwise.
207 * @param {String} name
211 function enabled(name) {
212 if (name[name.length - 1] === '*') {
219 for (i = 0, len = createDebug.skips.length; i < len; i++) {
220 if (createDebug.skips[i].test(name)) {
225 for (i = 0, len = createDebug.names.length; i < len; i++) {
226 if (createDebug.names[i].test(name)) {
235 * Convert regexp to namespace
237 * @param {RegExp} regxep
238 * @return {String} namespace
241 function toNamespace(regexp) {
242 return regexp.toString()
243 .substring(2, regexp.toString().length - 2)
244 .replace(/\.\*\?$/, '*');
254 function coerce(val) {
255 if (val instanceof Error) {
256 return val.stack || val.message;
262 * XXX DO NOT USE. This is a temporary stub function.
263 * XXX It WILL be removed in the next major release.
266 console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
269 createDebug.enable(createDebug.load());
274 module.exports = setup;