Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 915 | Details | Compare with Previous | Last modification | View Log | RSS feed

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