-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathBroadcastLogger.ts
117 lines (101 loc) · 3.19 KB
/
BroadcastLogger.ts
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
import { type ILogger, type ILogLevel, LogLevel } from '@powersync/common';
import { type WrappedSyncPort } from './SharedSyncImplementation';
/**
* Broadcasts logs to all clients
*/
export class BroadcastLogger implements ILogger {
TRACE: ILogLevel;
DEBUG: ILogLevel;
INFO: ILogLevel;
TIME: ILogLevel;
WARN: ILogLevel;
ERROR: ILogLevel;
OFF: ILogLevel;
constructor(protected clients: WrappedSyncPort[]) {
this.TRACE = LogLevel.TRACE;
this.DEBUG = LogLevel.DEBUG;
this.INFO = LogLevel.INFO;
this.TIME = LogLevel.TIME;
this.WARN = LogLevel.WARN;
this.ERROR = LogLevel.ERROR;
this.OFF = LogLevel.OFF;
}
trace(...x: any[]): void {
console.trace(...x);
const sanitized = this.sanitizeArgs(x);
this.iterateClients((client) => client.clientProvider.trace(...sanitized));
}
debug(...x: any[]): void {
console.debug(...x);
const sanitized = this.sanitizeArgs(x);
this.iterateClients((client) => client.clientProvider.debug(...sanitized));
}
info(...x: any[]): void {
console.info(...x);
const sanitized = this.sanitizeArgs(x);
this.iterateClients((client) => client.clientProvider.info(...sanitized));
}
log(...x: any[]): void {
console.log(...x);
const sanitized = this.sanitizeArgs(x);
this.iterateClients((client) => client.clientProvider.log(...sanitized));
}
warn(...x: any[]): void {
console.warn(...x);
const sanitized = this.sanitizeArgs(x);
this.iterateClients((client) => client.clientProvider.warn(...sanitized));
}
error(...x: any[]): void {
console.error(...x);
const sanitized = this.sanitizeArgs(x);
this.iterateClients((client) => client.clientProvider.error(...sanitized));
}
time(label: string): void {
console.time(label);
this.iterateClients((client) => client.clientProvider.time(label));
}
timeEnd(label: string): void {
console.timeEnd(label);
this.iterateClients((client) => client.clientProvider.timeEnd(label));
}
setLevel(level: ILogLevel): void {
// Levels are not adjustable on this level.
}
getLevel(): ILogLevel {
// Levels are not adjustable on this level.
return LogLevel.INFO;
}
enabledFor(level: ILogLevel): boolean {
// Levels are not adjustable on this level.
return true;
}
/**
* Iterates all clients, catches individual client exceptions
* and proceeds to execute for all clients.
*/
protected async iterateClients(callback: (client: WrappedSyncPort) => Promise<void>) {
for (const client of this.clients) {
try {
await callback(client);
} catch (ex) {
console.error('Caught exception when iterating client', ex);
}
}
}
/**
* Guards against any logging errors.
* We don't want a logging exception to cause further issues upstream
*/
protected sanitizeArgs(x: any[]): any[] {
const sanitizedParams = x.map((param) => {
try {
// Try and clone here first. If it fails it won't be passable over a MessagePort
return structuredClone(param);
} catch (ex) {
console.error(ex);
return 'Could not serialize log params. Check shared worker logs for more details.';
}
});
return sanitizedParams;
}
}