-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleLogger.js
131 lines (122 loc) · 4.22 KB
/
SimpleLogger.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
// Simple logger.
import { ceil, downloadBlob, getStackTrace } from "./essential.js";
export default class SimpleLogger {
#originTime;
node;
allLogs = [];
#errorCount;
#warningCount;
#errorCountNode;
#warningCountNode;
constructor(node) {
this.node = node;
this.node.classList.add("simpleLogger");
let logHeader = document.createElement("div");
logHeader.classList.add("logHeader");
logHeader.innerText = "Logs";
this.#errorCountNode = document.createElement("span");
this.#errorCountNode.innerText = "\u{1F6A8}0";
logHeader.appendChild(this.#errorCountNode);
this.#warningCountNode = document.createElement("span");
this.#warningCountNode.innerText = "\u26A0\uFE0F0";
logHeader.appendChild(this.#warningCountNode);
let downloadLogsButton = document.createElement("button");
downloadLogsButton.innerText = "Download all logs";
downloadLogsButton.setAttribute("type", "button");
downloadLogsButton.onEvent("click", () => {
downloadBlob(new Blob([JSON.stringify(this.allLogs)]), "holoprint_logs.json");
});
logHeader.appendChild(downloadLogsButton);
this.node.appendChild(logHeader);
this.#errorCount = 0;
this.#warningCount = 0;
this.#originTime = performance.now();
if(!document.querySelector(`link[href="SimpleLogger.css"]`)) {
let l = document.createElement("link");
l.rel = "stylesheet";
l.href = "SimpleLogger.css";
document.head.appendChild(l);
}
}
warn(text) {
if(this.#genericLogWithClass(text, "warning")) {
this.#warningCountNode.innerText = `\u26A0\uFE0F${++this.#warningCount}`;
}
}
error(text) {
if(this.#genericLogWithClass(text, "error")) {
this.#errorCountNode.innerText = `\u{1F6A8}${++this.#errorCount}`;
}
}
info(text) {
this.#genericLogWithClass(text, "info");
}
debug(text) {
this.#genericLogWithClass(text, "debug");
}
setOriginTime(originTime) {
this.#originTime = originTime;
}
#genericLogWithClass(text, logLevel) {
let stackTrace = getStackTrace().slice(2);
this.allLogs.push({
text,
level: logLevel,
stackTrace
});
let currentURLOrigin = location.href.slice(0, location.href.lastIndexOf("/")); // location.origin is null on Firefox when on local files
if(stackTrace.some(loc => !loc.includes(currentURLOrigin) && !loc.includes("<anonymous>"))) {
return;
}
if(logLevel == "debug") {
return;
}
let el = document.createElement("p");
el.classList.add("log");
if(logLevel) {
el.classList.add(logLevel);
}
let timestamp = this.#createTimestamp();
el.appendChild(timestamp);
let textSpan = document.createElement("span");
textSpan.classList.add("logText");
textSpan.innerText = text;
el.appendChild(textSpan);
let shouldScrollToBottom = ceil(this.node.scrollTop + this.node.getBoundingClientRect().height) >= this.node.scrollHeight;
this.node.appendChild(el);
if(shouldScrollToBottom) {
this.node.scrollTop = this.node.scrollHeight;
}
return true;
}
#createTimestamp() {
let el = document.createElement("span");
el.classList.add("timestamp");
let d = new Date(performance.now() - this.#originTime);
let text = `${d.getUTCMinutes().toString().padStart(2, "0")}:${d.getUTCSeconds().toString().padStart(2, "0")}.${d.getUTCMilliseconds().toString().padStart(3, "0")}`;
if(d.getUTCHours() > 0) {
text = `${d.getUTCHours().toString().padStart(2, "0")}:${text}`
}
el.innerText = text;
return el;
}
patchConsoleMethods() {
[console._warn, console._error, console._info, console._debug] = [console.warn, console.error, console.info, console.debug];
console.warn = (...args) => {
this.warn(args.join(" "));
return console._warn.apply(console, [getStackTrace()[1].match(/\/([^\/]+\.[^\.]+:\d+:\d+)/)[1] + "\n", ...args]);
};
console.error = (...args) => {
this.error(args.join(" "));
return console._error.apply(console, [getStackTrace()[1].match(/\/([^\/]+\.[^\.]+:\d+:\d+)/)[1] + "\n", ...args]);
};
console.info = (...args) => {
this.info(args.join(" "));
return console._info.apply(console, [getStackTrace()[1].match(/\/([^\/]+\.[^\.]+:\d+:\d+)/)[1] + "\n", ...args]);
};
console.debug = (...args) => {
this.debug(args.join(" "));
return console._debug.apply(console, [getStackTrace()[1].match(/\/([^\/]+\.[^\.]+:\d+:\d+)/)[1] + "\n", ...args]);
};
}
}