Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion vscode-wpilib/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

import * as path from 'path';
import * as vscode from 'vscode';
import * as winston from 'winston';
import * as TransportStream from 'winston-transport';

export interface ILogger {
error(message: string, ...meta: unknown[]): void;
Expand All @@ -10,6 +12,32 @@ export interface ILogger {
log(message: string, ...meta: unknown[]): void;
}

class VsCodeOutputTransport extends TransportStream {
private channel: vscode.LogOutputChannel;

constructor(opts?: TransportStream.TransportStreamOptions) {
super(opts);
this.channel = vscode.window.createOutputChannel('WPILib', { log: true });
}

public log(info: winston.LogEntry, next: () => void) {
setImmediate(() => this.emit('logged', info));
switch (info.level) {
case 'error':
this.channel.error(info.message, info.meta);
break;
case 'warn':
this.channel.warn(info.message, info.meta);
break;
// Log everything info and below as info
default:
this.channel.info(info.message, info.meta);
break;
}
next();
}
}

const myFormat = winston.format.printf((info) => {
return `${info.timestamp} ${info.level}: ${info.message}`;
});
Expand All @@ -18,7 +46,7 @@ const winstonLogger = winston.createLogger({
exitOnError: false,
format: winston.format.combine(winston.format.timestamp(), winston.format.simple(), myFormat),
level: 'verbose',
transports: [new winston.transports.Console()],
transports: [new VsCodeOutputTransport()],
});

export function closeLogger() {
Expand Down
Loading