- Use logger service to create output appender

- Unify output channels and services in ext host
- remove unused main output service methods
This commit is contained in:
Sandeep Somavarapu
2021-11-08 21:15:15 +01:00
parent 472b3e5447
commit ecf994ecfa
5 changed files with 102 additions and 238 deletions

View File

@@ -17,7 +17,6 @@ import { isNumber } from 'vs/base/common/types';
@extHostNamedCustomer(MainContext.MainThreadOutputService)
export class MainThreadOutputService extends Disposable implements MainThreadOutputServiceShape {
private static _idPool = 1;
private static _extensionIdPool = new Map<string, number>();
private readonly _proxy: ExtHostOutputServiceShape;
@@ -43,30 +42,17 @@ export class MainThreadOutputService extends Disposable implements MainThreadOut
setVisibleChannel();
}
public $register(label: string, log: boolean, file?: UriComponents, extensionId?: string): Promise<string> {
let id: string;
if (extensionId) {
const idCounter = (MainThreadOutputService._extensionIdPool.get(extensionId) || 0) + 1;
MainThreadOutputService._extensionIdPool.set(extensionId, idCounter);
id = `extension-output-${extensionId}-#${idCounter}`;
} else {
id = `extension-output-#${(MainThreadOutputService._idPool++)}`;
}
public async $register(label: string, log: boolean, file: UriComponents, extensionId: string): Promise<string> {
const idCounter = (MainThreadOutputService._extensionIdPool.get(extensionId) || 0) + 1;
MainThreadOutputService._extensionIdPool.set(extensionId, idCounter);
const id = `extension-output-${extensionId}-#${idCounter}`;
Registry.as<IOutputChannelRegistry>(Extensions.OutputChannels).registerChannel({ id, label, file: file ? URI.revive(file) : undefined, log });
Registry.as<IOutputChannelRegistry>(Extensions.OutputChannels).registerChannel({ id, label, file: URI.revive(file), log });
this._register(toDisposable(() => this.$dispose(id)));
return Promise.resolve(id);
return id;
}
public $append(channelId: string, value: string): Promise<void> | undefined {
const channel = this._getChannel(channelId);
if (channel) {
channel.append(value);
}
return undefined;
}
public $update(channelId: string, mode: OutputChannelUpdateMode, till?: number): Promise<void> | undefined {
public async $update(channelId: string, mode: OutputChannelUpdateMode, till?: number): Promise<void> {
const channel = this._getChannel(channelId);
if (channel) {
if (mode === OutputChannelUpdateMode.Append) {
@@ -75,50 +61,29 @@ export class MainThreadOutputService extends Disposable implements MainThreadOut
channel.update(mode, till);
}
}
return undefined;
}
public $clear(channelId: string): Promise<void> | undefined {
const channel = this._getChannel(channelId);
if (channel) {
channel.clear();
}
return undefined;
}
public $replace(channelId: string, value: string): Promise<void> | undefined {
const channel = this._getChannel(channelId);
if (channel) {
channel.replace(value);
}
return undefined;
}
public $reveal(channelId: string, preserveFocus: boolean): Promise<void> | undefined {
public async $reveal(channelId: string, preserveFocus: boolean): Promise<void> {
const channel = this._getChannel(channelId);
if (channel) {
this._outputService.showChannel(channel.id, preserveFocus);
}
return undefined;
}
public $close(channelId: string): Promise<void> | undefined {
public async $close(channelId: string): Promise<void> {
if (this._viewsService.isViewVisible(OUTPUT_VIEW_ID)) {
const activeChannel = this._outputService.getActiveChannel();
if (activeChannel && channelId === activeChannel.id) {
this._viewsService.closeView(OUTPUT_VIEW_ID);
}
}
return undefined;
}
public $dispose(channelId: string): Promise<void> | undefined {
public async $dispose(channelId: string): Promise<void> {
const channel = this._getChannel(channelId);
if (channel) {
channel.dispose();
}
return undefined;
}
private _getChannel(channelId: string): IOutputChannel | undefined {

View File

@@ -444,15 +444,12 @@ export interface MainThreadMessageServiceShape extends IDisposable {
}
export interface MainThreadOutputServiceShape extends IDisposable {
$register(label: string, log: boolean, file?: UriComponents, extensionId?: string): Promise<string>;
$append(channelId: string, value: string): Promise<void> | undefined;
$clear(channelId: string): Promise<void> | undefined;
$replace(channelId: string, value: string): Promise<void> | undefined;
$update(channelId: string, mode: OutputChannelUpdateMode.Append): Promise<void> | undefined;
$update(channelId: string, mode: OutputChannelUpdateMode, till: number): Promise<void> | undefined;
$reveal(channelId: string, preserveFocus: boolean): Promise<void> | undefined;
$close(channelId: string): Promise<void> | undefined;
$dispose(channelId: string): Promise<void> | undefined;
$register(label: string, log: boolean, file: UriComponents, extensionId: string): Promise<string>;
$update(channelId: string, mode: OutputChannelUpdateMode.Append): Promise<void>;
$update(channelId: string, mode: OutputChannelUpdateMode, till: number): Promise<void>;
$reveal(channelId: string, preserveFocus: boolean): Promise<void>;
$close(channelId: string): Promise<void>;
$dispose(channelId: string): Promise<void>;
}
export interface MainThreadProgressShape extends IDisposable {

View File

@@ -11,88 +11,104 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'
import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { checkProposedApiEnabled } from 'vs/workbench/services/extensions/common/extensions';
import { ILogger, ILoggerService } from 'vs/platform/log/common/log';
import { OutputChannelUpdateMode } from 'vs/workbench/contrib/output/common/output';
import { IExtHostConsumerFileSystem } from 'vs/workbench/api/common/extHostFileSystemConsumer';
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
import { IExtHostFileSystemInfo } from 'vs/workbench/api/common/extHostFileSystemInfo';
import { toLocalISOString } from 'vs/base/common/date';
import { VSBuffer } from 'vs/base/common/buffer';
export abstract class AbstractExtHostOutputChannel extends Disposable implements vscode.OutputChannel {
readonly _id: Promise<string>;
private readonly _name: string;
protected readonly _proxy: MainThreadOutputServiceShape;
private _disposed: boolean;
get disposed(): boolean { return this._disposed; }
export class ExtHostOutputChannel extends Disposable implements vscode.OutputChannel {
private _offset: number = 0;
public visible: boolean = false;
constructor(name: string, log: boolean, file: URI | undefined, extensionId: string, proxy: MainThreadOutputServiceShape) {
private _disposed: boolean = false;
get disposed(): boolean { return this._disposed; }
constructor(
readonly id: string, readonly name: string,
private readonly logger: ILogger,
private readonly proxy: MainThreadOutputServiceShape
) {
super();
this._name = name;
this._proxy = proxy;
this._id = proxy.$register(this.name, log, file, extensionId);
this._disposed = false;
}
get name(): string {
return this._name;
}
appendLine(value: string): void {
this.append(value + '\n');
}
append(value: string): void {
this.write(value);
if (this.visible) {
this.logger.flush();
this.proxy.$update(this.id, OutputChannelUpdateMode.Append);
}
}
clear(): void {
const till = this._offset;
this.logger.flush();
this.proxy.$update(this.id, OutputChannelUpdateMode.Clear, till);
}
replace(value: string): void {
const till = this._offset;
this.write(value);
this.proxy.$update(this.id, OutputChannelUpdateMode.Replace, till);
if (this.visible) {
this.logger.flush();
}
}
show(columnOrPreserveFocus?: vscode.ViewColumn | boolean, preserveFocus?: boolean): void {
this._id.then(id => this._proxy.$reveal(id, !!(typeof columnOrPreserveFocus === 'boolean' ? columnOrPreserveFocus : preserveFocus)));
this.logger.flush();
this.proxy.$reveal(this.id, !!(typeof columnOrPreserveFocus === 'boolean' ? columnOrPreserveFocus : preserveFocus));
}
hide(): void {
this._id.then(id => this._proxy.$close(id));
this.proxy.$close(this.id);
}
private write(value: string): void {
this._offset += VSBuffer.fromString(value).byteLength;
this.logger.info(value);
}
override dispose(): void {
super.dispose();
if (!this._disposed) {
this._id
.then(id => this._proxy.$dispose(id))
.then(() => this._disposed = true);
this.proxy.$dispose(this.id);
this._disposed = true;
}
}
abstract append(value: string): void;
abstract clear(): void;
abstract replace(value: string): void;
}
export class ExtHostPushOutputChannel extends AbstractExtHostOutputChannel {
constructor(name: string, extensionId: string, proxy: MainThreadOutputServiceShape) {
super(name, false, undefined, extensionId, proxy);
}
append(value: string): void {
this._id.then(id => this._proxy.$append(id, value));
}
clear(): void {
this._id.then(id => this._proxy.$clear(id));
}
replace(value: string): void {
this._id.then(id => this._proxy.$replace(id, value));
}
}
export class ExtHostOutputService implements ExtHostOutputServiceShape {
readonly _serviceBrand: undefined;
protected readonly _proxy: MainThreadOutputServiceShape;
private readonly _channels: Map<string, AbstractExtHostOutputChannel> = new Map<string, AbstractExtHostOutputChannel>();
private readonly proxy: MainThreadOutputServiceShape;
private readonly _outputsLocation: URI;
private outputDirectoryPromise: Thenable<URI> | undefined;
private _namePool: number = 1;
private readonly _channels: Map<string, ExtHostOutputChannel> = new Map<string, ExtHostOutputChannel>();
private visibleChannelId: string | null = null;
constructor(@IExtHostRpcService extHostRpc: IExtHostRpcService) {
this._proxy = extHostRpc.getProxy(MainContext.MainThreadOutputService);
constructor(
@IExtHostRpcService extHostRpc: IExtHostRpcService,
@IExtHostInitDataService initData: IExtHostInitDataService,
@IExtHostConsumerFileSystem private readonly extHostFileSystem: IExtHostConsumerFileSystem,
@IExtHostFileSystemInfo private readonly extHostFileSystemInfo: IExtHostFileSystemInfo,
@ILoggerService private readonly loggerService: ILoggerService,
) {
this.proxy = extHostRpc.getProxy(MainContext.MainThreadOutputService);
this._outputsLocation = this.extHostFileSystemInfo.extUri.joinPath(initData.logsLocation, `output_logging_${toLocalISOString(new Date()).replace(/-|:|\.\d+Z$/g, '')}`);
}
$setVisibleChannel(visibleChannelId: string | null): void {
@@ -107,20 +123,31 @@ export class ExtHostOutputService implements ExtHostOutputServiceShape {
if (!name) {
throw new Error('illegal argument `name`. must not be falsy');
}
const extHostOutputChannel = this.doCreateOutChannel(name, extension);
extHostOutputChannel.then(channel => channel._id.then(id => {
this._channels.set(id, channel);
channel.visible = id === this.visibleChannelId;
}));
const extHostOutputChannel = this.doCreateOutputChannel(name, extension);
extHostOutputChannel.then(channel => {
this._channels.set(channel.id, channel);
channel.visible = channel.id === this.visibleChannelId;
});
return this.createExtHostOutputChannel(name, extHostOutputChannel, extension);
}
protected async doCreateOutChannel(name: string, extension: IExtensionDescription): Promise<AbstractExtHostOutputChannel> {
return new ExtHostPushOutputChannel(name, extension.identifier.value, this._proxy);
private async doCreateOutputChannel(name: string, extension: IExtensionDescription): Promise<ExtHostOutputChannel> {
const outputDir = await this.createOutputDirectory();
const file = this.extHostFileSystemInfo.extUri.joinPath(outputDir, `${this._namePool++}-${name.replace(/[\\/:\*\?"<>\|]/g, '')}.log`);
const logger = this.loggerService.createLogger(file, { always: true, donotRotate: true, donotUseFormatters: true });
const id = await this.proxy.$register(name, false, file, extension.identifier.value);
return new ExtHostOutputChannel(id, name, logger, this.proxy);
}
private createExtHostOutputChannel(name: string, channelPromise: Promise<AbstractExtHostOutputChannel>, extensionDescription: IExtensionDescription): vscode.OutputChannel {
const validate = (channel: AbstractExtHostOutputChannel, checkProposedApi?: boolean) => {
private createOutputDirectory(): Thenable<URI> {
if (!this.outputDirectoryPromise) {
this.outputDirectoryPromise = this.extHostFileSystem.value.createDirectory(this._outputsLocation).then(() => this._outputsLocation);
}
return this.outputDirectoryPromise;
}
private createExtHostOutputChannel(name: string, channelPromise: Promise<ExtHostOutputChannel>, extensionDescription: IExtensionDescription): vscode.OutputChannel {
const validate = (channel: ExtHostOutputChannel, checkProposedApi?: boolean) => {
if (checkProposedApi) {
checkProposedApiEnabled(extensionDescription);
}

View File

@@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { ExtHostOutputService } from 'vs/workbench/api/node/extHostOutputService';
import { ExtHostTerminalService } from 'vs/workbench/api/node/extHostTerminalService';
import { ExtHostTask } from 'vs/workbench/api/node/extHostTask';
import { ExtHostDebugService } from 'vs/workbench/api/node/extHostDebugService';
@@ -13,7 +12,6 @@ import { ExtHostExtensionService } from 'vs/workbench/api/node/extHostExtensionS
import { ExtHostTunnelService } from 'vs/workbench/api/node/extHostTunnelService';
import { IExtHostDebugService } from 'vs/workbench/api/common/extHostDebugService';
import { IExtHostExtensionService } from 'vs/workbench/api/common/extHostExtensionService';
import { IExtHostOutputService } from 'vs/workbench/api/common/extHostOutput';
import { IExtHostSearch } from 'vs/workbench/api/common/extHostSearch';
import { IExtHostTask } from 'vs/workbench/api/common/extHostTask';
import { IExtHostTerminalService } from 'vs/workbench/api/common/extHostTerminalService';
@@ -34,7 +32,6 @@ registerSingleton(ILoggerService, ExtHostLoggerService);
registerSingleton(IExtensionStoragePaths, ExtensionStoragePaths);
registerSingleton(IExtHostDebugService, ExtHostDebugService);
registerSingleton(IExtHostOutputService, ExtHostOutputService);
registerSingleton(IExtHostSearch, NativeExtHostSearch);
registerSingleton(IExtHostTask, ExtHostTask);
registerSingleton(IExtHostTerminalService, ExtHostTerminalService);

View File

@@ -1,122 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { MainThreadOutputServiceShape } from '../common/extHost.protocol';
import type * as vscode from 'vscode';
import { URI } from 'vs/base/common/uri';
import { join } from 'vs/base/common/path';
import { toLocalISOString } from 'vs/base/common/date';
import { Promises, SymlinkSupport } from 'vs/base/node/pfs';
import { AbstractExtHostOutputChannel, ExtHostOutputService as BaseExtHostOutputService } from 'vs/workbench/api/common/extHostOutput';
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
import { ILogService } from 'vs/platform/log/common/log';
import { createRotatingLogger } from 'vs/platform/log/node/spdlogLog';
import { Logger } from 'spdlog';
import { ByteSize } from 'vs/platform/files/common/files';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { OutputChannelUpdateMode } from 'vs/workbench/contrib/output/common/output';
import { VSBuffer } from 'vs/base/common/buffer';
class OutputAppender {
static async create(name: string, file: string): Promise<OutputAppender> {
const appender = await createRotatingLogger(name, file, 30 * ByteSize.MB, 1);
appender.clearFormatters();
return new OutputAppender(name, file, appender);
}
private constructor(readonly name: string, readonly file: string, private readonly appender: Logger) { }
append(content: string): void {
this.appender.critical(content);
}
flush(): void {
this.appender.flush();
}
}
class ExtHostOutputChannelBackedByFile extends AbstractExtHostOutputChannel {
private _offset: number;
private readonly _appender: OutputAppender;
constructor(name: string, appender: OutputAppender, extensionId: string, proxy: MainThreadOutputServiceShape) {
super(name, false, URI.file(appender.file), extensionId, proxy);
this._offset = 0;
this._appender = appender;
}
append(value: string): void {
this.incrementOffset(value);
this._appender.append(value);
if (this.visible) {
this._appender.flush();
this._id.then(id => this._proxy.$update(id, OutputChannelUpdateMode.Append));
}
}
clear(): void {
const till = this._offset;
this._appender.flush();
this._id.then(id => this._proxy.$update(id, OutputChannelUpdateMode.Clear, till));
}
replace(value: string): void {
const till = this._offset;
this.incrementOffset(value);
this._id.then(id => this._proxy.$update(id, OutputChannelUpdateMode.Replace, till));
this._appender.append(value);
if (this.visible) {
this._appender.flush();
}
}
override show(columnOrPreserveFocus?: vscode.ViewColumn | boolean, preserveFocus?: boolean): void {
this._appender.flush();
super.show(columnOrPreserveFocus, preserveFocus);
}
private incrementOffset(value: string) {
this._offset += VSBuffer.fromString(value).byteLength;
}
}
export class ExtHostOutputService extends BaseExtHostOutputService {
private _logsLocation: URI;
private _namePool: number = 1;
constructor(
@IExtHostRpcService extHostRpc: IExtHostRpcService,
@ILogService private readonly logService: ILogService,
@IExtHostInitDataService initData: IExtHostInitDataService,
) {
super(extHostRpc);
this._logsLocation = initData.logsLocation;
}
protected override async doCreateOutChannel(name: string, extension: IExtensionDescription): Promise<AbstractExtHostOutputChannel> {
try {
const outputDirPath = join(this._logsLocation.fsPath, `output_logging_${toLocalISOString(new Date()).replace(/-|:|\.\d+Z$/g, '')}`);
const exists = await SymlinkSupport.existsDirectory(outputDirPath);
if (!exists) {
await Promises.mkdir(outputDirPath, { recursive: true });
}
const fileName = `${this._namePool++}-${name.replace(/[\\/:\*\?"<>\|]/g, '')}`;
const file = URI.file(join(outputDirPath, `${fileName}.log`));
const appender = await OutputAppender.create(fileName, file.fsPath);
return new ExtHostOutputChannelBackedByFile(name, appender, extension.identifier.value, this._proxy);
} catch (error) {
// Do not crash if logger cannot be created
this.logService.error(error);
}
return super.doCreateOutChannel(name, extension);
}
}