|
| 1 | + |
| 2 | +/** |
| 3 | + * This is the common logic for both the Node.js and web browser |
| 4 | + * implementations of `debug()`. |
| 5 | + */ |
| 6 | + |
| 7 | +module.exports = function setup(env) { |
| 8 | + createDebug.debug = createDebug['default'] = createDebug; |
| 9 | + createDebug.coerce = coerce; |
| 10 | + createDebug.disable = disable; |
| 11 | + createDebug.enable = enable; |
| 12 | + createDebug.enabled = enabled; |
| 13 | + createDebug.humanize = require('ms'); |
| 14 | + |
| 15 | + Object.keys(env).forEach(function(key) { |
| 16 | + createDebug[key] = env[key]; |
| 17 | + }); |
| 18 | + |
| 19 | + /** |
| 20 | + * Active `debug` instances. |
| 21 | + */ |
| 22 | + createDebug.instances = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * The currently active debug mode names, and names to skip. |
| 26 | + */ |
| 27 | + |
| 28 | + createDebug.names = []; |
| 29 | + createDebug.skips = []; |
| 30 | + |
| 31 | + /** |
| 32 | + * Map of special "%n" handling functions, for the debug "format" argument. |
| 33 | + * |
| 34 | + * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N". |
| 35 | + */ |
| 36 | + |
| 37 | + createDebug.formatters = {}; |
| 38 | + |
| 39 | + /** |
| 40 | + * Select a color. |
| 41 | + * @param {String} namespace |
| 42 | + * @return {Number} |
| 43 | + * @api private |
| 44 | + */ |
| 45 | + |
| 46 | + function selectColor(namespace) { |
| 47 | + var hash = 0, i; |
| 48 | + |
| 49 | + for (i in namespace) { |
| 50 | + hash = ((hash << 5) - hash) + namespace.charCodeAt(i); |
| 51 | + hash |= 0; // Convert to 32bit integer |
| 52 | + } |
| 53 | + |
| 54 | + return createDebug.colors[Math.abs(hash) % createDebug.colors.length]; |
| 55 | + } |
| 56 | + createDebug.selectColor = selectColor; |
| 57 | + |
| 58 | + /** |
| 59 | + * Create a debugger with the given `namespace`. |
| 60 | + * |
| 61 | + * @param {String} namespace |
| 62 | + * @return {Function} |
| 63 | + * @api public |
| 64 | + */ |
| 65 | + |
| 66 | + function createDebug(namespace) { |
| 67 | + var prevTime; |
| 68 | + |
| 69 | + function debug() { |
| 70 | + // disabled? |
| 71 | + if (!debug.enabled) return; |
| 72 | + |
| 73 | + var self = debug; |
| 74 | + |
| 75 | + // set `diff` timestamp |
| 76 | + var curr = +new Date(); |
| 77 | + var ms = curr - (prevTime || curr); |
| 78 | + self.diff = ms; |
| 79 | + self.prev = prevTime; |
| 80 | + self.curr = curr; |
| 81 | + prevTime = curr; |
| 82 | + |
| 83 | + // turn the `arguments` into a proper Array |
| 84 | + var args = new Array(arguments.length); |
| 85 | + for (var i = 0; i < args.length; i++) { |
| 86 | + args[i] = arguments[i]; |
| 87 | + } |
| 88 | + |
| 89 | + args[0] = createDebug.coerce(args[0]); |
| 90 | + |
| 91 | + if ('string' !== typeof args[0]) { |
| 92 | + // anything else let's inspect with %O |
| 93 | + args.unshift('%O'); |
| 94 | + } |
| 95 | + |
| 96 | + // apply any `formatters` transformations |
| 97 | + var index = 0; |
| 98 | + args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) { |
| 99 | + // if we encounter an escaped % then don't increase the array index |
| 100 | + if (match === '%%') return match; |
| 101 | + index++; |
| 102 | + var formatter = createDebug.formatters[format]; |
| 103 | + if ('function' === typeof formatter) { |
| 104 | + var val = args[index]; |
| 105 | + match = formatter.call(self, val); |
| 106 | + |
| 107 | + // now we need to remove `args[index]` since it's inlined in the `format` |
| 108 | + args.splice(index, 1); |
| 109 | + index--; |
| 110 | + } |
| 111 | + return match; |
| 112 | + }); |
| 113 | + |
| 114 | + // apply env-specific formatting (colors, etc.) |
| 115 | + createDebug.formatArgs.call(self, args); |
| 116 | + |
| 117 | + var logFn = self.log || createDebug.log; |
| 118 | + logFn.apply(self, args); |
| 119 | + } |
| 120 | + |
| 121 | + debug.namespace = namespace; |
| 122 | + debug.enabled = createDebug.enabled(namespace); |
| 123 | + debug.useColors = createDebug.useColors(); |
| 124 | + debug.color = selectColor(namespace); |
| 125 | + debug.destroy = destroy; |
| 126 | + //debug.formatArgs = formatArgs; |
| 127 | + //debug.rawLog = rawLog; |
| 128 | + |
| 129 | + // env-specific initialization logic for debug instances |
| 130 | + if ('function' === typeof createDebug.init) { |
| 131 | + createDebug.init(debug); |
| 132 | + } |
| 133 | + |
| 134 | + createDebug.instances.push(debug); |
| 135 | + |
| 136 | + return debug; |
| 137 | + } |
| 138 | + |
| 139 | + function destroy () { |
| 140 | + var index = createDebug.instances.indexOf(this); |
| 141 | + if (index !== -1) { |
| 142 | + createDebug.instances.splice(index, 1); |
| 143 | + return true; |
| 144 | + } else { |
| 145 | + return false; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Enables a debug mode by namespaces. This can include modes |
| 151 | + * separated by a colon and wildcards. |
| 152 | + * |
| 153 | + * @param {String} namespaces |
| 154 | + * @api public |
| 155 | + */ |
| 156 | + |
| 157 | + function enable(namespaces) { |
| 158 | + createDebug.save(namespaces); |
| 159 | + |
| 160 | + createDebug.names = []; |
| 161 | + createDebug.skips = []; |
| 162 | + |
| 163 | + var i; |
| 164 | + var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/); |
| 165 | + var len = split.length; |
| 166 | + |
| 167 | + for (i = 0; i < len; i++) { |
| 168 | + if (!split[i]) continue; // ignore empty strings |
| 169 | + namespaces = split[i].replace(/\*/g, '.*?'); |
| 170 | + if (namespaces[0] === '-') { |
| 171 | + createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); |
| 172 | + } else { |
| 173 | + createDebug.names.push(new RegExp('^' + namespaces + '$')); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + for (i = 0; i < createDebug.instances.length; i++) { |
| 178 | + var instance = createDebug.instances[i]; |
| 179 | + instance.enabled = createDebug.enabled(instance.namespace); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Disable debug output. |
| 185 | + * |
| 186 | + * @api public |
| 187 | + */ |
| 188 | + |
| 189 | + function disable() { |
| 190 | + createDebug.enable(''); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Returns true if the given mode name is enabled, false otherwise. |
| 195 | + * |
| 196 | + * @param {String} name |
| 197 | + * @return {Boolean} |
| 198 | + * @api public |
| 199 | + */ |
| 200 | + |
| 201 | + function enabled(name) { |
| 202 | + if (name[name.length - 1] === '*') { |
| 203 | + return true; |
| 204 | + } |
| 205 | + var i, len; |
| 206 | + for (i = 0, len = createDebug.skips.length; i < len; i++) { |
| 207 | + if (createDebug.skips[i].test(name)) { |
| 208 | + return false; |
| 209 | + } |
| 210 | + } |
| 211 | + for (i = 0, len = createDebug.names.length; i < len; i++) { |
| 212 | + if (createDebug.names[i].test(name)) { |
| 213 | + return true; |
| 214 | + } |
| 215 | + } |
| 216 | + return false; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Coerce `val`. |
| 221 | + * |
| 222 | + * @param {Mixed} val |
| 223 | + * @return {Mixed} |
| 224 | + * @api private |
| 225 | + */ |
| 226 | + |
| 227 | + function coerce(val) { |
| 228 | + if (val instanceof Error) return val.stack || val.message; |
| 229 | + return val; |
| 230 | + } |
| 231 | + |
| 232 | + createDebug.enable(createDebug.load()); |
| 233 | + |
| 234 | + return createDebug; |
| 235 | +} |
0 commit comments