|
1 | | -/* |
2 | | - * logging.ts |
3 | | - * |
4 | | - * Copyright (C) 2023 by Posit Software, PBC |
5 | | - * Copyright (c) Microsoft Corporation. All rights reserved. |
6 | | - * |
7 | | - * Unless you have received this program directly from Posit Software pursuant |
8 | | - * to the terms of a commercial license agreement with Posit Software, then |
9 | | - * this program is licensed to you under the terms of version 3 of the |
10 | | - * GNU Affero General Public License. This program is distributed WITHOUT |
11 | | - * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, |
12 | | - * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the |
13 | | - * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. |
14 | | - * |
15 | | - */ |
16 | | - |
17 | | -// based on: |
18 | | -// https://github.com/microsoft/vscode/blob/main/extensions/markdown-language-features/server/src/logging.ts |
19 | | - |
20 | | - |
21 | | -import { Disposable } from 'core'; |
22 | | - |
23 | | -import { ILogger, LogLevel } from "./service"; |
24 | | - |
25 | | -import { ConfigurationManager } from './config'; |
26 | | - |
27 | | -export class LogFunctionLogger extends Disposable implements ILogger { |
28 | | - |
29 | | - private static now(): string { |
30 | | - const now = new Date(); |
31 | | - return String(now.getUTCHours()).padStart(2, '0') |
32 | | - + ':' + String(now.getMinutes()).padStart(2, '0') |
33 | | - + ':' + String(now.getUTCSeconds()).padStart(2, '0') + '.' + String(now.getMilliseconds()).padStart(3, '0'); |
34 | | - } |
35 | | - |
36 | | - private static data2String(data: unknown): string { |
37 | | - if (data instanceof Error) { |
38 | | - if (typeof data.stack === 'string') { |
39 | | - return data.stack; |
40 | | - } |
41 | | - return data.message; |
42 | | - } |
43 | | - if (typeof data === 'string') { |
44 | | - return data; |
45 | | - } |
46 | | - return JSON.stringify(data, undefined, 2); |
47 | | - } |
48 | | - |
49 | | - private _logLevel: LogLevel; |
50 | | - |
51 | | - constructor( |
52 | | - private readonly _logFn: typeof console.log, |
53 | | - private readonly _config: ConfigurationManager, |
54 | | - ) { |
55 | | - super(); |
56 | | - |
57 | | - this._register(this._config.onDidChangeConfiguration(() => { |
58 | | - this._logLevel = LogFunctionLogger.readLogLevel(this._config); |
59 | | - })); |
60 | | - |
61 | | - this._logLevel = LogFunctionLogger.readLogLevel(this._config); |
62 | | - } |
63 | | - |
64 | | - private static readLogLevel(config: ConfigurationManager): LogLevel { |
65 | | - switch (config.getSettings().markdown.server.log) { |
66 | | - case 'trace': return LogLevel.Trace; |
67 | | - case 'debug': return LogLevel.Debug; |
68 | | - case 'off': |
69 | | - default: |
70 | | - return LogLevel.Off; |
71 | | - } |
72 | | - } |
73 | | - |
74 | | - get level(): LogLevel { return this._logLevel; } |
75 | | - |
76 | | - public log(level: LogLevel, message: string, data?: unknown): void { |
77 | | - if (this.level < level) { |
78 | | - return; |
79 | | - } |
80 | | - |
81 | | - this.appendLine(`[${this.toLevelLabel(level)} ${LogFunctionLogger.now()}] ${message}`); |
82 | | - if (data) { |
83 | | - this.appendLine(LogFunctionLogger.data2String(data)); |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - private toLevelLabel(level: LogLevel): string { |
88 | | - switch (level) { |
89 | | - case LogLevel.Off: return 'Off'; |
90 | | - case LogLevel.Debug: return 'Debug'; |
91 | | - case LogLevel.Trace: return 'Trace'; |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - private appendLine(value: string): void { |
96 | | - this._logFn(value); |
97 | | - } |
98 | | -} |
99 | | - |
| 1 | +/* |
| 2 | + * logging.ts |
| 3 | + * |
| 4 | + * Copyright (C) 2023 by Posit Software, PBC |
| 5 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 6 | + * |
| 7 | + * Unless you have received this program directly from Posit Software pursuant |
| 8 | + * to the terms of a commercial license agreement with Posit Software, then |
| 9 | + * this program is licensed to you under the terms of version 3 of the |
| 10 | + * GNU Affero General Public License. This program is distributed WITHOUT |
| 11 | + * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, |
| 12 | + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the |
| 13 | + * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +// based on: |
| 18 | +// https://github.com/microsoft/vscode/blob/main/extensions/markdown-language-features/server/src/logging.ts |
| 19 | + |
| 20 | + |
| 21 | +import { Disposable } from 'core'; |
| 22 | + |
| 23 | +import { ILogger, LogLevel } from "./service"; |
| 24 | + |
| 25 | +import { ConfigurationManager } from './config'; |
| 26 | + |
| 27 | +export class LogFunctionLogger extends Disposable implements ILogger { |
| 28 | + |
| 29 | + private static now(): string { |
| 30 | + const now = new Date(); |
| 31 | + return String(now.getUTCHours()).padStart(2, '0') |
| 32 | + + ':' + String(now.getMinutes()).padStart(2, '0') |
| 33 | + + ':' + String(now.getUTCSeconds()).padStart(2, '0') + '.' + String(now.getMilliseconds()).padStart(3, '0'); |
| 34 | + } |
| 35 | + |
| 36 | + private static data2String(data: unknown): string { |
| 37 | + if (data instanceof Error) { |
| 38 | + if (typeof data.stack === 'string') { |
| 39 | + return data.stack; |
| 40 | + } |
| 41 | + return data.message; |
| 42 | + } |
| 43 | + if (typeof data === 'string') { |
| 44 | + return data; |
| 45 | + } |
| 46 | + return JSON.stringify(data, undefined, 2); |
| 47 | + } |
| 48 | + |
| 49 | + private _logLevel: LogLevel; |
| 50 | + |
| 51 | + constructor( |
| 52 | + private readonly _logFn: typeof console.log, |
| 53 | + private readonly _config: ConfigurationManager, |
| 54 | + ) { |
| 55 | + super(); |
| 56 | + |
| 57 | + this._register(this._config.onDidChangeConfiguration(() => { |
| 58 | + this._logLevel = LogFunctionLogger.readLogLevel(this._config); |
| 59 | + })); |
| 60 | + |
| 61 | + this._logLevel = LogFunctionLogger.readLogLevel(this._config); |
| 62 | + } |
| 63 | + |
| 64 | + private static readLogLevel(config: ConfigurationManager): LogLevel { |
| 65 | + switch (config.getSettings().markdown.server.log) { |
| 66 | + case 'trace': return LogLevel.Trace; |
| 67 | + case 'debug': return LogLevel.Debug; |
| 68 | + case 'off': |
| 69 | + default: |
| 70 | + return LogLevel.Off; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + get level(): LogLevel { return this._logLevel; } |
| 75 | + |
| 76 | + public log(level: LogLevel, message: string, data?: unknown): void { |
| 77 | + if (this.level < level) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + this.appendLine(`[${this.toLevelLabel(level)} ${LogFunctionLogger.now()}] ${message}`); |
| 82 | + if (data) { |
| 83 | + this.appendLine(LogFunctionLogger.data2String(data)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private toLevelLabel(level: LogLevel): string { |
| 88 | + switch (level) { |
| 89 | + case LogLevel.Off: return 'Off'; |
| 90 | + case LogLevel.Debug: return 'Debug'; |
| 91 | + case LogLevel.Trace: return 'Trace'; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private appendLine(value: string): void { |
| 96 | + this._logFn(value); |
| 97 | + } |
| 98 | +} |
0 commit comments