forked from SpringRoll/SpringRoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugger.js
217 lines (204 loc) · 5.07 KB
/
Debugger.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* @typedef {object} DebuggerParams
* @property {boolean} emitEnabled
* @property {boolean} enabled
* @property {number} minLevel
* @class Debugger
* @param {Object} params - Options
* @param {boolean} [params.emitEnabled=false] If this should emit events to the window.
* @param {'GENERAL' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR'} [params.minLevel='GENERAL'] The starting log level for the logger.
*/
export class Debugger {
/**
* Returns the params of the debugger.
* @returns {DebuggerParams}
* @readonly
* @static
* @memberof Debugger
*/
static get params() {
Debugger.initParams();
return window[Debugger.paramKey];
}
/**
* Sets the logging level of the debugger.
* @param {string | number} level The name of the level.
* @return {void}
*/
static minLevel(level) {
Debugger.initParams();
if ('number' === typeof level) {
window[Debugger.paramKey].minLevel = level;
return;
}
level = level.toUpperCase();
if (Debugger.isValidLevelName(level)) {
window[Debugger.paramKey].minLevel = Debugger.LEVEL[level];
} else {
window[Debugger.paramKey].minLevel = Debugger.LEVEL['GENERAL'];
}
}
/**
* Setup the params if not set already.
* @private
* @static
* @memberof Debugger
*/
static initParams() {
if (!window[Debugger.paramKey]) {
window[Debugger.paramKey] = {
emitEnabled: false,
enabled: true,
minLevel: 1
};
}
}
/**
* If emitting is enabled for this instance, then it will dispatch an event on the window.
* @param {string} [eventName='Debugger'] Name of the event
*/
static emit(eventName = 'Debugger') {
Debugger.initParams();
if (Debugger.params.emitEnabled) {
window.dispatchEvent(new Event(eventName));
}
}
/**
* Returns logging levels.
* @readonly
* @returns {object}
* @static
* @memberof Debugger
*/
static get LEVEL() {
return {
GENERAL: 1,
DEBUG: 2,
INFO: 3,
WARN: 4,
ERROR: 5
};
}
/**
* Function to test if level meets requirements.
* @param {string} [level='GENERAL']
* @returns {boolean}
* @private
*/
static meetsLevelRequirement(level = 'GENERAL') {
Debugger.initParams();
if (Debugger.isValidLevelName(level)) {
if (Debugger.LEVEL[level] >= Debugger.params.minLevel) {
return true;
}
}
return false;
}
/**
* Console logs all supplied arguments if the log level is low enough for them to be logged.
* @param {'log' | 'general' | 'warn'| 'error' | 'debug' | 'info'} [type='log'] minimum level for this log to run at
* @param {*[]} args Arguments you wish to log.
*/
static log(type = 'log', ...args) {
Debugger.initParams();
if (Debugger.isEnabled()) {
switch (type.toLowerCase()) {
case 'info':
if (Debugger.meetsLevelRequirement('INFO')) {
console.info(...args);
Debugger.emit();
return true;
}
return false;
case 'debug':
if (Debugger.meetsLevelRequirement('DEBUG')) {
console.debug(...args);
Debugger.emit();
return true;
}
return false;
case 'error':
if (Debugger.meetsLevelRequirement('ERROR')) {
console.error(...args);
Debugger.emit();
return true;
}
return false;
case 'warn':
if (Debugger.meetsLevelRequirement('WARN')) {
console.warn(...args);
Debugger.emit();
return true;
}
return false;
case 'log':
case 'general':
default:
if (Debugger.meetsLevelRequirement('GENERAL')) {
console.log(...args);
Debugger.emit();
return true;
}
return false;
}
}
}
/**
* Checks to see if the string argument is a valid level name.
* @param {string} level The level name.
* @return {boolean}
* @private
*/
static isValidLevelName(level) {
Debugger.initParams();
if (
'GENERAL' == level ||
'DEBUG' == level ||
'INFO' == level ||
'WARN' == level ||
'ERROR' == level
) {
return true;
}
return false;
}
/**
* Will throw if statement is false.
* @static
* @param {boolean} isTrue The expression to evaluate.
*/
static assert(isTrue) {
Debugger.initParams();
if (!isTrue) {
throw `Assert Error: ${isTrue}`;
}
}
/**
* Returns a boolean indicating if the debugger has been enabled or not.
* @static
* @returns {boolean}
*/
static isEnabled() {
return window[Debugger.paramKey].enabled;
}
/**
* Disables or enables all debugger instances.
* TODO: Remove the parameter here, and add a disable method as well
* @static
* @param {boolean} flag
* @returns {void}
*/
static enable(flag) {
Debugger.initParams();
window[Debugger.paramKey].enabled = flag;
}
/**
* Returns the global params key.
* @readonly
* @static
* @memberof Debugger
*/
static get paramKey() {
return '__spring_roll_debugger_params__';
}
}