forked from buritica/debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.require.js
More file actions
191 lines (158 loc) · 4 KB
/
debug.require.js
File metadata and controls
191 lines (158 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
define(function(require, exports, module) {
/**
* Expose `debug()` as the module.
*/
module.exports = window.debug = debug;
/**
* Create a debugger with the given `name`.
*
* @param {String} name
* @return {Type}
* @api public
*/
function debug(name) {
if (!debug.enabled(name)) return function(){};
return function(fmt){
fmt = coerce(fmt);
var args;
var curr = new Date;
var ms = curr - (debug[name] || curr);
debug[name] = curr;
fmt = '@'
+ name
+ ' -> '
+ fmt
+ ' +' + debug.humanize(ms);
if (debug.colorSupport) {
fmt = '%c ' + fmt;
}
args = Array.prototype.slice.call(arguments);
if (debug.colorSupport) {
args.splice(1, 0, debug.color(name));
}
// add lineNumber
var stack = new Error().stack;
if (typeof stack !== 'undefined' && typeof args !== 'undefined') {
stack = stack.split('\n');
var lineNumber = stack[2];
if (lineNumber.indexOf('(') !== - 1) {
lineNumber = lineNumber.substring(lineNumber.lastIndexOf('(')+1,lineNumber.lastIndexOf(')'));
}
args.push(lineNumber);
}
// This hackery is required for IE8
// where `console.log` doesn't have 'apply'
window.console
&& console.log
&& Function.prototype.apply.call(console.log, console, args || arguments);
}
}
/**
* The currently active debug mode names.
*/
debug.names = [];
debug.skips = [];
debug.colors = {};
/**
* Enables a debug mode by name. This can include modes
* separated by a colon and wildcards.
*
* @param {String} name
* @api public
*/
debug.enable = function(name) {
try {
localStorage.debug = name;
} catch(e){}
var split = (name || '').split(/[\s,]+/)
, len = split.length;
for (var i = 0; i < len; i++) {
name = split[i].replace('*', '.*?');
if (name[0] === '-') {
debug.skips.push(new RegExp('^' + name.substr(1) + '$'));
}
else {
debug.names.push(new RegExp('^' + name + '$'));
}
}
};
/**
* Disable debug output.
*
* @api public
*/
debug.disable = function(){
debug.enable('');
};
/**
* Humanize the given `ms`.
*
* @param {Number} m
* @return {String}
* @api private
*/
debug.humanize = function(ms) {
var sec = 1000
, min = 60 * 1000
, hour = 60 * min;
if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
if (ms >= min) return (ms / min).toFixed(1) + 'm';
if (ms >= sec) return (ms / sec | 0) + 's';
return ms + 'ms';
};
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
debug.enabled = function(name) {
for (var i = 0, len = debug.skips.length; i < len; i++) {
if (debug.skips[i].test(name)) {
return false;
}
}
for (var i = 0, len = debug.names.length; i < len; i++) {
if (debug.names[i].test(name)) {
return true;
}
}
return false;
};
/**
* Coerce `val`.
*/
function coerce(val) {
if (val instanceof Error) return val.stack || val.message;
return val;
}
/**
* Check if browser supports color
*/
(function colorSupportCheck() {
if(window.chrome || window.console && (console.exception && console.table || console.colorized)) {
debug.colorSupport = true;
return;
}
debug.colorSupport = false;
})();
/**
* Returns color assigned to given name,
* assigns color to name if needed
*
* @param {String} name
* @return {String}
* @api public
*/
debug.color = function(name) {
if (typeof debug.colors[name] === 'undefined') {
debug.colors[name] = 'color: #' +('00000'+(Math.random()*16777216<<0).toString(16)).substr(-6)
}
return debug.colors[name];
};
// persist
try {
if (window.localStorage) debug.enable(localStorage.debug);
} catch(e){}
});