generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlogger.ts
61 lines (54 loc) · 1.58 KB
/
logger.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
export type Options = {
level: "debug" | "info" | "trace";
};
export class Logger {
constructor(
private nvim: {
outWriteLine: (message: string) => Promise<void>;
errWrite: (message: string) => Promise<void>;
errWriteLine: (message: string) => Promise<void>;
},
private options: Options = { level: "debug" },
) {}
log(message: string) {
console.log(message);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.nvim.outWriteLine(message);
}
debug(message: string) {
if (this.options.level == "debug" || this.options.level == "trace") {
console.log(message);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.nvim.outWriteLine(message);
}
}
trace(message: string) {
if (this.options.level == "trace") {
console.log(message);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.nvim.outWriteLine(message);
}
}
error(error: Error | string) {
try {
console.error(error);
} catch {
// nothing to do
}
if (typeof error == "string") {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.nvim.errWriteLine(error);
} else {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.nvim.errWriteLine(error.message);
if (error.stack) {
try {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.nvim.errWrite(error.stack);
} catch {
// nothing to do
}
}
}
}
}