Skip to content

Commit 4e5baae

Browse files
committed
Bring back logging to VS Code's Output panel
1 parent db770e8 commit 4e5baae

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

vscode-wpilib/src/logger.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

33
import * as path from 'path';
4+
import * as vscode from 'vscode';
45
import * as winston from 'winston';
6+
import * as TransportStream from 'winston-transport';
57

68
export interface ILogger {
79
error(message: string, ...meta: unknown[]): void;
@@ -10,6 +12,32 @@ export interface ILogger {
1012
log(message: string, ...meta: unknown[]): void;
1113
}
1214

15+
class VsCodeOutputTransport extends TransportStream {
16+
private channel: vscode.LogOutputChannel;
17+
18+
constructor(opts?: TransportStream.TransportStreamOptions) {
19+
super(opts);
20+
this.channel = vscode.window.createOutputChannel('WPILib', { log: true });
21+
}
22+
23+
public log(info: winston.LogEntry, next: () => void) {
24+
setImmediate(() => this.emit('logged', info));
25+
switch (info.level) {
26+
case 'error':
27+
this.channel.error(info.message, info.meta);
28+
break;
29+
case 'warn':
30+
this.channel.warn(info.message, info.meta);
31+
break;
32+
// Log everything info and below as info
33+
default:
34+
this.channel.info(info.message, info.meta);
35+
break;
36+
}
37+
next();
38+
}
39+
}
40+
1341
const myFormat = winston.format.printf((info) => {
1442
return `${info.timestamp} ${info.level}: ${info.message}`;
1543
});
@@ -18,7 +46,7 @@ const winstonLogger = winston.createLogger({
1846
exitOnError: false,
1947
format: winston.format.combine(winston.format.timestamp(), winston.format.simple(), myFormat),
2048
level: 'verbose',
21-
transports: [new winston.transports.Console()],
49+
transports: [new VsCodeOutputTransport()],
2250
});
2351

2452
export function closeLogger() {

0 commit comments

Comments
 (0)