1.1.2
by Leo Iannacone
Import upstream version 2.0.0+dfsg |
1 |
/**
|
2 |
* This is the web browser implementation of `debug()`.
|
|
3 |
*
|
|
4 |
* Expose `debug()` as the module.
|
|
5 |
*/
|
|
6 |
||
7 |
exports = module.exports = require('./debug'); |
|
8 |
exports.log = log; |
|
9 |
exports.formatArgs = formatArgs; |
|
10 |
exports.save = save; |
|
11 |
exports.load = load; |
|
12 |
exports.useColors = useColors; |
|
13 |
||
14 |
/**
|
|
15 |
* Colors.
|
|
16 |
*/
|
|
17 |
||
18 |
exports.colors = [ |
|
19 |
'lightseagreen', |
|
20 |
'forestgreen', |
|
21 |
'goldenrod', |
|
22 |
'dodgerblue', |
|
23 |
'darkorchid', |
|
24 |
'crimson'
|
|
25 |
];
|
|
26 |
||
27 |
/**
|
|
28 |
* Currently only WebKit-based Web Inspectors, Firefox >= v31,
|
|
29 |
* and the Firebug extension (any Firefox version) are known
|
|
30 |
* to support "%c" CSS customizations.
|
|
31 |
*
|
|
32 |
* TODO: add a `localStorage` variable to explicitly enable/disable colors
|
|
33 |
*/
|
|
34 |
||
35 |
function useColors() { |
|
36 |
// is webkit? http://stackoverflow.com/a/16459606/376773
|
|
37 |
return ('WebkitAppearance' in document.documentElement.style) || |
|
38 |
// is firebug? http://stackoverflow.com/a/398120/376773
|
|
39 |
(window.console && (console.firebug || (console.exception && console.table))) || |
|
40 |
// is firefox >= v31?
|
|
41 |
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
|
|
42 |
(navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31); |
|
43 |
}
|
|
44 |
||
45 |
/**
|
|
46 |
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
|
|
47 |
*/
|
|
48 |
||
49 |
exports.formatters.j = function(v) { |
|
50 |
return JSON.stringify(v); |
|
51 |
};
|
|
52 |
||
53 |
||
54 |
/**
|
|
55 |
* Colorize log arguments if enabled.
|
|
56 |
*
|
|
57 |
* @api public
|
|
58 |
*/
|
|
59 |
||
60 |
function formatArgs() { |
|
61 |
var args = arguments; |
|
62 |
var useColors = this.useColors; |
|
63 |
||
64 |
args[0] = (useColors ? '%c' : '') |
|
65 |
+ this.namespace |
|
66 |
+ (useColors ? ' %c' : ' ') |
|
67 |
+ args[0] |
|
68 |
+ (useColors ? '%c ' : ' ') |
|
69 |
+ '+' + exports.humanize(this.diff); |
|
70 |
||
71 |
if (!useColors) return args; |
|
72 |
||
73 |
var c = 'color: ' + this.color; |
|
74 |
args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1)); |
|
75 |
||
76 |
// the final "%c" is somewhat tricky, because there could be other
|
|
77 |
// arguments passed either before or after the %c, so we need to
|
|
78 |
// figure out the correct index to insert the CSS into
|
|
79 |
var index = 0; |
|
80 |
var lastC = 0; |
|
81 |
args[0].replace(/%[a-z%]/g, function(match) { |
|
82 |
if ('%%' === match) return; |
|
83 |
index++; |
|
84 |
if ('%c' === match) { |
|
85 |
// we only are interested in the *last* %c
|
|
86 |
// (the user may have provided their own)
|
|
87 |
lastC = index; |
|
88 |
}
|
|
89 |
});
|
|
90 |
||
91 |
args.splice(lastC, 0, c); |
|
92 |
return args; |
|
93 |
}
|
|
94 |
||
95 |
/**
|
|
96 |
* Invokes `console.log()` when available.
|
|
97 |
* No-op when `console.log` is not a "function".
|
|
98 |
*
|
|
99 |
* @api public
|
|
100 |
*/
|
|
101 |
||
102 |
function log() { |
|
103 |
// This hackery is required for IE8,
|
|
104 |
// where the `console.log` function doesn't have 'apply'
|
|
105 |
return 'object' == typeof console |
|
106 |
&& 'function' == typeof console.log |
|
107 |
&& Function.prototype.apply.call(console.log, console, arguments); |
|
108 |
}
|
|
109 |
||
110 |
/**
|
|
111 |
* Save `namespaces`.
|
|
112 |
*
|
|
113 |
* @param {String} namespaces
|
|
114 |
* @api private
|
|
115 |
*/
|
|
116 |
||
117 |
function save(namespaces) { |
|
118 |
try { |
|
119 |
if (null == namespaces) { |
|
120 |
localStorage.removeItem('debug'); |
|
121 |
} else { |
|
122 |
localStorage.debug = namespaces; |
|
123 |
}
|
|
124 |
} catch(e) {} |
|
125 |
}
|
|
126 |
||
127 |
/**
|
|
128 |
* Load `namespaces`.
|
|
129 |
*
|
|
130 |
* @return {String} returns the previously persisted debug modes
|
|
131 |
* @api private
|
|
132 |
*/
|
|
133 |
||
134 |
function load() { |
|
135 |
var r; |
|
136 |
try { |
|
137 |
r = localStorage.debug; |
|
138 |
} catch(e) {} |
|
139 |
return r; |
|
140 |
}
|
|
141 |
||
142 |
/**
|
|
143 |
* Enable namespaces listed in `localStorage.debug` initially.
|
|
144 |
*/
|
|
145 |
||
146 |
exports.enable(load()); |
|
147 |