comparison Resources/Graveyard/Deprecated/Platforms/Wasm/logger.ts @ 1503:553084468225

moving /Deprecated/ to /Resources/Graveyard/Deprecated/
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Jun 2020 11:38:13 +0200
parents Deprecated/Platforms/Wasm/logger.ts@419d0320c344
children
comparison
equal deleted inserted replaced
1502:e5729dab3f67 1503:553084468225
1 export enum LogSource {
2 Cpp,
3 Typescript
4 }
5
6 export class StandardConsoleLogger {
7 public showSource: boolean = true;
8
9 public debug(...args: any[]): void {
10 this._debug(LogSource.Typescript, ...args);
11 }
12
13 public debugFromCpp(...args: any[]): void {
14 this._debug(LogSource.Cpp, ...args);
15 }
16
17 public info(...args: any[]): void {
18 this._info(LogSource.Typescript, ...args);
19 }
20
21 public infoFromCpp(message: string): void {
22 this._info(LogSource.Cpp, message);
23 }
24
25 public warning(...args: any[]): void {
26 this._warning(LogSource.Typescript, ...args);
27 }
28
29 public warningFromCpp(message: string): void {
30 this._warning(LogSource.Cpp, message);
31 }
32
33 public error(...args: any[]): void {
34 this._error(LogSource.Typescript, ...args);
35 }
36
37 public errorFromCpp(message: string): void {
38 this._error(LogSource.Cpp, message);
39 }
40
41 public _debug(source: LogSource, ...args: any[]): void {
42 if ((<any> window).IsTraceLevelEnabled)
43 {
44 if ((<any> window).IsTraceLevelEnabled())
45 {
46 var output = this.getOutput(source, args);
47 console.debug(...output);
48 }
49 }
50 }
51
52 private _info(source: LogSource, ...args: any[]): void {
53 if ((<any> window).IsInfoLevelEnabled)
54 {
55 if ((<any> window).IsInfoLevelEnabled())
56 {
57 var output = this.getOutput(source, args);
58 console.info(...output);
59 }
60 }
61 }
62
63 public _warning(source: LogSource, ...args: any[]): void {
64 var output = this.getOutput(source, args);
65 console.warn(...output);
66 }
67
68 public _error(source: LogSource, ...args: any[]): void {
69 var output = this.getOutput(source, args);
70 console.error(...output);
71 }
72
73
74 private getOutput(source: LogSource, args: any[]): any[] {
75 var prefix = this.getPrefix();
76 var prefixAndSource = Array<string>();
77
78 if (prefix != null) {
79 prefixAndSource = [prefix];
80 }
81
82 if (this.showSource) {
83 if (source == LogSource.Typescript) {
84 prefixAndSource = [...prefixAndSource, "TS "];
85 } else if (source == LogSource.Cpp) {
86 prefixAndSource = [...prefixAndSource, "C++"];
87 }
88 }
89
90 if (prefixAndSource.length > 0) {
91 prefixAndSource = [...prefixAndSource, "|"];
92 }
93
94 return [...prefixAndSource, ...args];
95 }
96
97 protected getPrefix(): string | null {
98 return null;
99 }
100 }
101
102 export class TimeConsoleLogger extends StandardConsoleLogger {
103 protected getPrefix(): string {
104 let now = new Date();
105 let timeString = now.getHours().toString().padStart(2, "0") + ":" + now.getMinutes().toString().padStart(2, "0") + ":" + now.getSeconds().toString().padStart(2, "0") + "." + now.getMilliseconds().toString().padStart(3, "0");
106 return timeString;
107 }
108 }
109
110 export var defaultLogger: StandardConsoleLogger = new TimeConsoleLogger();
111