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 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;
64 function debug(...args) {
72 // Set `diff` timestamp
73 const curr = Number(new Date());
74 const ms = curr - (prevTime || curr);
80 args[0] = createDebug.coerce(args[0]);
82 if (typeof args[0] !== 'string') {
83 // Anything else let's inspect with %O
87 // Apply any `formatters` transformations
89 args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
90 // If we encounter an escaped % then don't increase the array index
95 const formatter = createDebug.formatters[format];
96 if (typeof formatter === 'function') {
97 const val = args[index];
98 match = formatter.call(self, val);
100 // Now we need to remove `args[index]` since it's inlined in the `format`
101 args.splice(index, 1);
107 // Apply env-specific formatting (colors, etc.)
108 createDebug.formatArgs.call(self, args);
110 const logFn = self.log || createDebug.log;
111 logFn.apply(self, args);
114 debug.namespace = namespace;
115 debug.useColors = createDebug.useColors();
116 debug.color = createDebug.selectColor(namespace);
117 debug.extend = extend;
118 debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.
120 Object.defineProperty(debug, 'enabled', {
123 get: () => enableOverride === null ? createDebug.enabled(namespace) : enableOverride,
129 // Env-specific initialization logic for debug instances
130 if (typeof createDebug.init === 'function') {
131 createDebug.init(debug);
137 function extend(namespace, delimiter) {
138 const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
139 newDebug.log = this.log;
144 * Enables a debug mode by namespaces. This can include modes
145 * separated by a colon and wildcards.
147 * @param {String} namespaces
150 function enable(namespaces) {
151 createDebug.save(namespaces);
153 createDebug.names = [];
154 createDebug.skips = [];
157 const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
158 const len = split.length;
160 for (i = 0; i < len; i++) {
162 // ignore empty strings
166 namespaces = split[i].replace(/\*/g, '.*?');
168 if (namespaces[0] === '-') {
169 createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
171 createDebug.names.push(new RegExp('^' + namespaces + '$'));
177 * Disable debug output.
179 * @return {String} namespaces
184 ...createDebug.names.map(toNamespace),
185 ...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace)
187 createDebug.enable('');
192 * Returns true if the given mode name is enabled, false otherwise.
194 * @param {String} name
198 function enabled(name) {
199 if (name[name.length - 1] === '*') {
206 for (i = 0, len = createDebug.skips.length; i < len; i++) {
207 if (createDebug.skips[i].test(name)) {
212 for (i = 0, len = createDebug.names.length; i < len; i++) {
213 if (createDebug.names[i].test(name)) {
222 * Convert regexp to namespace
224 * @param {RegExp} regxep
225 * @return {String} namespace
228 function toNamespace(regexp) {
229 return regexp.toString()
230 .substring(2, regexp.toString().length - 2)
231 .replace(/\.\*\?$/, '*');
241 function coerce(val) {
242 if (val instanceof Error) {
243 return val.stack || val.message;
249 * XXX DO NOT USE. This is a temporary stub function.
250 * XXX It WILL be removed in the next major release.
253 console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
256 createDebug.enable(createDebug.load());
261 module.exports = setup;