3 * This is the web browser implementation of `debug()`.
5 * Expose `debug()` as the module.
8 exports = module.exports = require('./debug');
10 exports.formatArgs = formatArgs;
13 exports.useColors = useColors;
14 exports.storage = 'undefined' != typeof chrome
15 && 'undefined' != typeof chrome.storage
16 ? chrome.storage.local
33 * Currently only WebKit-based Web Inspectors, Firefox >= v31,
34 * and the Firebug extension (any Firefox version) are known
35 * to support "%c" CSS customizations.
37 * TODO: add a `localStorage` variable to explicitly enable/disable colors
40 function useColors() {
41 // is webkit? http://stackoverflow.com/a/16459606/376773
42 // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
43 return (typeof document !== 'undefined' && 'WebkitAppearance' in document.documentElement.style) ||
44 // is firebug? http://stackoverflow.com/a/398120/376773
45 (window.console && (console.firebug || (console.exception && console.table))) ||
47 // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
48 (navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31);
52 * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
55 exports.formatters.j = function(v) {
57 return JSON.stringify(v);
59 return '[UnexpectedJSONParseError]: ' + err.message;
65 * Colorize log arguments if enabled.
70 function formatArgs() {
72 var useColors = this.useColors;
74 args[0] = (useColors ? '%c' : '')
76 + (useColors ? ' %c' : ' ')
78 + (useColors ? '%c ' : ' ')
79 + '+' + exports.humanize(this.diff);
81 if (!useColors) return args;
83 var c = 'color: ' + this.color;
84 args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1));
86 // the final "%c" is somewhat tricky, because there could be other
87 // arguments passed either before or after the %c, so we need to
88 // figure out the correct index to insert the CSS into
91 args[0].replace(/%[a-z%]/g, function(match) {
92 if ('%%' === match) return;
95 // we only are interested in the *last* %c
96 // (the user may have provided their own)
101 args.splice(lastC, 0, c);
106 * Invokes `console.log()` when available.
107 * No-op when `console.log` is not a "function".
113 // this hackery is required for IE8/9, where
114 // the `console.log` function doesn't have 'apply'
115 return 'object' === typeof console
117 && Function.prototype.apply.call(console.log, console, arguments);
123 * @param {String} namespaces
127 function save(namespaces) {
129 if (null == namespaces) {
130 exports.storage.removeItem('debug');
132 exports.storage.debug = namespaces;
140 * @return {String} returns the previously persisted debug modes
147 return exports.storage.debug;
150 // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
151 if (typeof process !== 'undefined' && 'env' in process) {
152 return process.env.DEBUG;
157 * Enable namespaces listed in `localStorage.debug` initially.
160 exports.enable(load());
163 * Localstorage attempts to return the localstorage.
165 * This is necessary because safari throws
166 * when a user disables cookies/localstorage
167 * and you attempt to access it.
169 * @return {LocalStorage}
173 function localstorage(){
175 return window.localStorage;