comparison Platforms/Wasm/logger.ts @ 526:548eed46f535 dev

introduced a Logger class that displays timing and source (C++/JS)
author Alain Mazy <alain@mazy.be>
date Thu, 14 Mar 2019 19:04:35 +0100
parents
children 70992b38aa8a
comparison
equal deleted inserted replaced
524:9e241cef32a4 526:548eed46f535
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 info(...args: any[]): void {
14 this._info(LogSource.Typescript, ...args);
15 }
16
17 public infoFromCpp(message: string): void {
18 this._info(LogSource.Cpp, message);
19 }
20
21 public warning(...args: any[]): void {
22 this._warning(LogSource.Typescript, ...args);
23 }
24
25 public error(...args: any[]): void {
26 this._error(LogSource.Typescript, ...args);
27 }
28
29 public errorFromCpp(message: string): void {
30 this._error(LogSource.Cpp, message);
31 }
32
33 public _debug(source: LogSource, ...args: any[]): void {
34 var output = this.getOutput(source, args);
35 console.debug(...output);
36 }
37
38 private _info(source: LogSource, ...args: any[]): void {
39 var output = this.getOutput(source, args);
40 console.info(...output);
41 }
42
43 public _warning(source: LogSource, ...args: any[]): void {
44 var output = this.getOutput(source, args);
45 console.warn(...output);
46 }
47
48 public _error(source: LogSource, ...args: any[]): void {
49 var output = this.getOutput(source, args);
50 console.error(...output);
51 }
52
53
54 private getOutput(source: LogSource, args: any[]): any[] {
55 var prefix = this.getPrefix();
56 var prefixAndSource = [];
57
58 if (prefix != null) {
59 prefixAndSource = [prefix];
60 }
61
62 if (this.showSource) {
63 if (source == LogSource.Typescript) {
64 prefixAndSource = [...prefixAndSource, "TS "];
65 } else if (source == LogSource.Cpp) {
66 prefixAndSource = [...prefixAndSource, "C++"];
67 }
68 }
69
70 if (prefixAndSource.length > 0) {
71 prefixAndSource = [...prefixAndSource, "|"];
72 }
73
74 return [...prefixAndSource, ...args];
75 }
76
77 protected getPrefix(): string {
78 return null;
79 }
80 }
81
82 export class TimeConsoleLogger extends StandardConsoleLogger {
83 protected getPrefix(): string {
84 let now = new Date();
85 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");
86 return timeString;
87 }
88 }
89
90 export var defaultLogger: StandardConsoleLogger = new TimeConsoleLogger();