-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathdebug.js
117 lines (103 loc) · 2.65 KB
/
debug.js
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
import debug from 'debug';
var settings = {
colors: {
debug: 'gray',
error: 'red',
info: 'gray',
warn: 'orange'
}
};
/**
* Overwrite `debug` so we can colorize error/warning messages and remove Time Diff
*
* (See issue: https://github.com/debug-js/debug/issues/582#issuecomment-1755718739)
*/
debug.formatArgs = formatArgs;
function formatArgs (args) {
args[0] =
(this.useColors ? '%c' : '') +
this.namespace +
(this.useColors ? ' %c' : ' ') +
args[0] +
(this.useColors ? '%c ' : ' ');
if (!this.useColors) {
return;
}
this.color = getDebugNamespaceColor(this.namespace);
var c = 'color: ' + this.color;
args.splice(1, 0, c, 'color: inherit');
// The final "%c" is somewhat tricky, because there could be other
// arguments passed either before or after the %c, so we need to
// figure out the correct index to insert the CSS into
var index = 0;
var lastC = 0;
args[0].replace(/%[a-zA-Z%]/g, function (match) {
if (match === '%%') {
return;
}
index++;
if (match === '%c') {
// We only are interested in the *last* %c
// (the user may have provided their own)
lastC = index;
}
});
args.splice(lastC, 0, c);
}
/**
* Returns the type of the namespace (e.g., `error`, `warn`).
*
* @param {string} namespace
* The debug logger's namespace (e.g., `components:geometry:warn`).
* @returns {string} The type of the namespace (e.g., `warn`).
* @api private
*/
function getDebugNamespaceType (namespace) {
var chunks = namespace.split(':');
return chunks[chunks.length - 1]; // Return the last one
}
/**
* Returns the color of the namespace (e.g., `orange`).
*
* @param {string} namespace
* The debug logger's namespace (e.g., `components:geometry:warn`).
* @returns {string} The color of the namespace (e.g., `orange`).
* @api private
*/
function getDebugNamespaceColor (namespace) {
var type = getDebugNamespaceType(namespace);
var color = settings.colors && settings.colors[type];
return color || null;
}
/**
* Returns `localStorage` if possible.
*
* This is necessary because Safari throws when a user disables
* cookies or `localStorage` and you attempt to access it.
*
* @returns {localStorage}
* @api private
*/
function storage () {
try {
return window.localStorage;
} catch (e) {
}
}
/**
* To enable console logging, type this in the Console of your Dev Tools:
*
* localStorage.logs = 1
*
* To disable console logging:
*
* localStorage.logs = 0
*
*/
var ls = storage();
if (ls && (parseInt(ls.logs, 10) || ls.logs === 'true')) {
debug.enable('*');
} else {
debug.enable('*:error,*:info,*:warn');
}
export default debug;