-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlogger.js
71 lines (57 loc) · 1.59 KB
/
logger.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
class Logger {
constructor() {
this.logArr = [];
this.registeredViews = [];
}
static getInstance() {
if (Logger.instance === null) {
Logger.instance = new Logger();
}
return Logger.instance;
}
getEntries() {
return this.logArr;
}
getLastEntry() {
return this.logArr[this.logArr.length - 1];
}
info(str) {
this.logArr[this.logArr.length] = { str: str, type: Logger.LOG_TYPE_INFO, time: Date.now() };
this.updateViews();
}
warn(str) {
this.logArr[this.logArr.length] = { str: str, type: Logger.LOG_TYPE_WARN, time: Date.now() };
this.updateViews();
}
error(str) {
this.logArr[this.logArr.length] = { str: str, type: Logger.LOG_TYPE_ERROR, time: Date.now() };
this.updateViews();
}
debug(str) {
this.logArr[this.logArr.length] = { str: str, type: Logger.LOG_TYPE_DEBUG, time: Date.now() };
this.updateViews();
}
registerView(_view) {
this.registeredViews[this.registeredViews.length] = _view;
}
unregisterView(_view) {
for (let i = 0, l = this.registeredViews.length; i < l; i++) {
if (this.registeredViews[i] !== _view) continue;
this.registeredViews.splice(i, 1);
i--;
}
}
updateViews() {
for (let i = 0, l = this.registeredViews.length; i < l; i++) {
if (!this.registeredViews[i]) continue;
this.registeredViews[i].update(this);
}
}
}
Logger.instance = null;
Logger.LOG_TYPE_INFO = 0;
Logger.LOG_TYPE_WARN = 1;
Logger.LOG_TYPE_ERROR = 2;
Logger.LOG_TYPE_DEBUG = 3;
// Assign global reference for debug window
export default (window.Logger = Logger);