|
5 | 5 |
|
6 | 6 | import * as vscode from 'vscode' |
7 | 7 |
|
8 | | -export type LogTopic = 'crashReport' | 'notifications' | 'test' | 'unknown' |
| 8 | +export type LogTopic = 'crashReport' | 'dev/beta' | 'notifications' | 'test' | 'unknown' |
9 | 9 |
|
10 | 10 | class ErrorLog { |
11 | 11 | constructor( |
@@ -143,24 +143,18 @@ function prependTopic(topic: string, message: string | Error): string | ErrorLog |
143 | 143 | } |
144 | 144 |
|
145 | 145 | /** |
146 | | - * Gets the logger if it has been initialized |
147 | | - * the logger is of `'main'` or `undefined`: Main logger; default impl: logs to log file and log output channel |
| 146 | + * Gets the global default logger. |
| 147 | + * |
148 | 148 | * @param topic: topic to be appended in front of the message. |
149 | 149 | */ |
150 | 150 | export function getLogger(topic?: LogTopic): Logger { |
151 | | - const logger = toolkitLoggers['main'] |
152 | | - if (!logger) { |
153 | | - return new ConsoleLogger() |
154 | | - } |
155 | | - return new TopicLogger(topic ?? 'unknown', logger) |
| 151 | + // `TopicLogger` will lazy-load the "main" logger when it becomes available. |
| 152 | + return new TopicLogger(topic ?? 'unknown', 'main') |
156 | 153 | } |
157 | 154 |
|
158 | 155 | export function getDebugConsoleLogger(topic?: LogTopic): Logger { |
159 | | - const logger = toolkitLoggers['debugConsole'] |
160 | | - if (!logger) { |
161 | | - return new ConsoleLogger() |
162 | | - } |
163 | | - return new TopicLogger(topic ?? 'unknown', logger) |
| 156 | + // `TopicLogger` will lazy-load the "debugConsole" logger when it becomes available. |
| 157 | + return new TopicLogger(topic ?? 'unknown', 'debugConsole') |
164 | 158 | } |
165 | 159 |
|
166 | 160 | // jscpd:ignore-start |
@@ -215,15 +209,25 @@ export class ConsoleLogger extends BaseLogger { |
215 | 209 | } |
216 | 210 |
|
217 | 211 | /** |
218 | | - * Wraps a `ToolkitLogger` and defers to it for everything except `topic`. |
| 212 | + * Wraps the specified `ToolkitLogger` and defers to it for everything except `topic`. |
| 213 | + * |
| 214 | + * Falls back to `ConsoleLogger` when the logger isn't available yet (during startup). |
219 | 215 | */ |
220 | 216 | export class TopicLogger extends BaseLogger implements vscode.Disposable { |
| 217 | + // HACK: crude form of "lazy initialization", to support module-scope assignment of |
| 218 | + // `getLogger()` without being sensitive to module-load ordering. So even if logging isn't ready |
| 219 | + // at the time of the `getLogger` call, it will recover later. (This is a bit hacky, because it |
| 220 | + // arguably doesn't belong in `TopicLogger`.) |
| 221 | + public get logger() { |
| 222 | + return toolkitLoggers[this.loggerKey] ?? new ConsoleLogger() |
| 223 | + } |
| 224 | + |
221 | 225 | /** |
222 | 226 | * Wraps a `ToolkitLogger` and defers to it for everything except `topic`. |
223 | 227 | */ |
224 | 228 | public constructor( |
225 | 229 | public override topic: LogTopic, |
226 | | - public readonly logger: Logger |
| 230 | + public readonly loggerKey: keyof typeof toolkitLoggers |
227 | 231 | ) { |
228 | 232 | super() |
229 | 233 | } |
|
0 commit comments