#57618: ExtHost: Implement writable output channels using spdlog

This commit is contained in:
Sandeep Somavarapu
2018-09-04 15:26:19 +02:00
parent a030947fd6
commit 5dbd093581
12 changed files with 187 additions and 116 deletions

View File

@@ -6,19 +6,24 @@
import { MainContext, MainThreadOutputServiceShape, IMainContext } from './extHost.protocol';
import * as vscode from 'vscode';
import { URI } from 'vs/base/common/uri';
import { posix } from 'path';
import { OutputAppender } from 'vs/platform/output/node/outputAppender';
import { TPromise } from 'vs/base/common/winjs.base';
export class ExtHostOutputChannel implements vscode.OutputChannel {
export abstract class AbstractExtHostOutputChannel implements vscode.OutputChannel {
private static _idPool = 1;
private _proxy: MainThreadOutputServiceShape;
private _name: string;
private _id: string;
protected readonly _id: string;
private readonly _name: string;
protected readonly _proxy: MainThreadOutputServiceShape;
protected _registerationPromise: TPromise<void> = TPromise.as(null);
private _disposed: boolean;
constructor(name: string, proxy: MainThreadOutputServiceShape) {
this._id = 'extension-output-#' + (AbstractExtHostOutputChannel._idPool++);
this._name = name;
this._id = 'extension-output-#' + (ExtHostOutputChannel._idPool++);
this._proxy = proxy;
}
@@ -26,18 +31,7 @@ export class ExtHostOutputChannel implements vscode.OutputChannel {
return this._name;
}
dispose(): void {
if (!this._disposed) {
this._proxy.$dispose(this._id, this._name).then(() => {
this._disposed = true;
});
}
}
append(value: string): void {
this.validate();
this._proxy.$append(this._id, this._name, value);
}
abstract append(value: string): void;
appendLine(value: string): void {
this.validate();
@@ -46,44 +40,81 @@ export class ExtHostOutputChannel implements vscode.OutputChannel {
clear(): void {
this.validate();
this._proxy.$clear(this._id, this._name);
this._registerationPromise.then(() => this._proxy.$clear(this._id));
}
show(columnOrPreserveFocus?: vscode.ViewColumn | boolean, preserveFocus?: boolean): void {
this.validate();
if (typeof columnOrPreserveFocus === 'boolean') {
preserveFocus = columnOrPreserveFocus;
}
this._proxy.$reveal(this._id, this._name, preserveFocus);
this._registerationPromise.then(() => this._proxy.$reveal(this._id, typeof columnOrPreserveFocus === 'boolean' ? columnOrPreserveFocus : preserveFocus));
}
hide(): void {
this.validate();
this._proxy.$close(this._id);
this._registerationPromise.then(() => this._proxy.$close(this._id));
}
private validate(): void {
protected validate(): void {
if (this._disposed) {
throw new Error('Channel has been closed');
}
}
dispose(): void {
if (!this._disposed) {
this._registerationPromise
.then(() => this._proxy.$dispose(this._id))
.then(() => this._disposed = true);
}
}
}
export class ExtHostOutputChannel extends AbstractExtHostOutputChannel {
constructor(name: string, proxy: MainThreadOutputServiceShape) {
super(name, proxy);
this._registerationPromise = proxy.$register(this._id, name);
}
append(value: string): void {
this.validate();
this._registerationPromise.then(() => this._proxy.$append(this._id, value));
}
}
export class ExtHostLoggingOutputChannel extends AbstractExtHostOutputChannel {
private _appender: OutputAppender;
constructor(name: string, outputDir: string, proxy: MainThreadOutputServiceShape) {
super(name, proxy);
const file = URI.file(posix.join(outputDir, `${this._id}.log`));
this._appender = new OutputAppender(this._id, file.fsPath);
this._registerationPromise = proxy.$register(this._id, this.name, file);
}
append(value: string): void {
this.validate();
this._appender.append(value);
}
}
export class ExtHostOutputService {
private _proxy: MainThreadOutputServiceShape;
private _outputDir: string;
constructor(mainContext: IMainContext) {
constructor(outputDir: string, mainContext: IMainContext) {
this._outputDir = outputDir;
this._proxy = mainContext.getProxy(MainContext.MainThreadOutputService);
}
createOutputChannel(name: string): vscode.OutputChannel {
createOutputChannel(name: string, logging?: boolean): vscode.OutputChannel {
name = name.trim();
if (!name) {
throw new Error('illegal argument `name`. must not be falsy');
} else {
return new ExtHostOutputChannel(name, this._proxy);
return logging ? new ExtHostLoggingOutputChannel(name, this._outputDir, this._proxy) : new ExtHostOutputChannel(name, this._proxy);
}
}
}