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