Skip to content

Commit c3670cc

Browse files
committed
SimpleChatTC:ToolsConsole:Cleanup a bit, add basic set of notes
Try ensure as well as verify that original console.log is saved and not overwritten. Throw an exception if things seem off wrt same. Also ensure to add a newline at end of console.log messages
1 parent 6fa4440 commit c3670cc

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

tools/server/public_simplechat/toolsconsole.mjs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
//
55

66

7+
/** The redirected console.log's capture-data-space */
78
export let gConsoleStr = ""
89
/**
10+
* Maintain original console.log, when needed
911
* @type { {(...data: any[]): void} | null}
1012
*/
11-
export let gOrigConsoleLog = null
13+
let gOrigConsoleLog = null
1214

1315

1416
/**
17+
* The trapping console.log
1518
* @param {any[]} args
1619
*/
1720
export function console_trapped(...args) {
@@ -22,17 +25,33 @@ export function console_trapped(...args) {
2225
return String(arg);
2326
}
2427
}).join(' ');
25-
gConsoleStr += res;
28+
gConsoleStr += `${res}\n`;
2629
}
2730

31+
/**
32+
* Save the original console.log, if needed.
33+
* Setup redir of console.log.
34+
* Clear the redirected console.log's capture-data-space.
35+
*/
2836
export function console_redir() {
29-
gOrigConsoleLog = console.log
37+
if (gOrigConsoleLog == null) {
38+
if (console.log == console_trapped) {
39+
throw new Error("ERRR:ToolsConsole:ReDir:Original Console.Log lost???");
40+
}
41+
gOrigConsoleLog = console.log
42+
}
3043
console.log = console_trapped
3144
gConsoleStr = ""
3245
}
3346

47+
/**
48+
* Revert the redirected console.log to the original console.log, if possible.
49+
*/
3450
export function console_revert() {
3551
if (gOrigConsoleLog !== null) {
52+
if (gOrigConsoleLog == console_trapped) {
53+
throw new Error("ERRR:ToolsConsole:Revert:Original Console.Log lost???");
54+
}
3655
console.log = gOrigConsoleLog
3756
}
3857
}

0 commit comments

Comments
 (0)