| 167 | * Observes log messages from an ObservableLogOutputChannel and collects them until disposed. |
| 168 | */ |
| 169 | export class LogObserver { |
| 170 | private readonly _messages: LogMessage[] = []; |
| 171 | private readonly _subscription: vscode.Disposable; |
| 172 | |
| 173 | constructor(channel: ObservableLogOutputChannel) { |
| 174 | this._subscription = channel.onMessageLogged((message) => { |
| 175 | this._messages.push(message); |
| 176 | }); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Returns the collected messages as a formatted string suitable for a log file. |
| 181 | */ |
| 182 | public getLog(): string { |
| 183 | return LogObserver.formatLogMessages(this._messages); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Disposes the subscription and stops observing log messages. |
| 188 | */ |
| 189 | public dispose(): void { |
| 190 | this._subscription.dispose(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Formats an array of log messages into a string suitable for a log file. |
| 195 | */ |
| 196 | public static formatLogMessages(messages: LogMessage[]): string { |
| 197 | return messages |
| 198 | .map((msg) => { |
| 199 | const timestamp = msg.timestamp.toISOString(); |
| 200 | const level = msg.level.toUpperCase().padEnd(5); |
| 201 | return `[${timestamp}] [${level}] ${msg.message}`; |
| 202 | }) |
| 203 | .join('\n'); |
| 204 | } |
| 205 | } |
nothing calls this directly
no outgoing calls
no test coverage detected