Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging in web #177275

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
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
43 changes: 25 additions & 18 deletions src/vs/platform/log/common/fileLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Queue } from 'vs/base/common/async';
import { ThrottledDelayer } from 'vs/base/common/async';
import { VSBuffer } from 'vs/base/common/buffer';
import { basename, dirname, joinPath } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
Expand All @@ -16,8 +16,9 @@ const MAX_FILE_SIZE = 5 * ByteSize.MB;
class FileLogger extends AbstractMessageLogger implements ILogger {

private readonly initializePromise: Promise<void>;
private readonly queue: Queue<void>;
private readonly flushDelayer: ThrottledDelayer<void>;
private backupIndex: number = 1;
private buffer: string = '';

constructor(
private readonly resource: URI,
Expand All @@ -27,11 +28,25 @@ class FileLogger extends AbstractMessageLogger implements ILogger {
) {
super();
this.setLevel(level);
this.queue = this._register(new Queue<void>());
this.flushDelayer = new ThrottledDelayer<void>(100 /* buffer saves over a short time */);
this.initializePromise = this.initialize();
}

override flush(): void {
override async flush(): Promise<void> {
if (!this.buffer) {
return;
}
await this.initializePromise;
let content = await this.loadContent();
if (content.length > MAX_FILE_SIZE) {
await this.fileService.writeFile(this.getBackupResource(), VSBuffer.fromString(content));
content = '';
}
if (this.buffer) {
content += this.buffer;
this.buffer = '';
await this.fileService.writeFile(this.resource, VSBuffer.fromString(content));
}
}

private async initialize(): Promise<void> {
Expand All @@ -45,20 +60,12 @@ class FileLogger extends AbstractMessageLogger implements ILogger {
}

protected log(level: LogLevel, message: string): void {
this.queue.queue(async () => {
await this.initializePromise;
let content = await this.loadContent();
if (content.length > MAX_FILE_SIZE) {
await this.fileService.writeFile(this.getBackupResource(), VSBuffer.fromString(content));
content = '';
}
if (this.donotUseFormatters) {
content += message;
} else {
content += `${this.getCurrentTimestamp()} [${this.stringifyLogLevel(level)}] ${message}\n`;
}
await this.fileService.writeFile(this.resource, VSBuffer.fromString(content));
});
if (this.donotUseFormatters) {
this.buffer += message;
} else {
this.buffer += `${this.getCurrentTimestamp()} [${this.stringifyLogLevel(level)}] ${message}\n`;
}
this.flushDelayer.trigger(() => this.flush());
}

private getCurrentTimestamp(): string {
Expand Down
8 changes: 8 additions & 0 deletions src/vs/workbench/api/browser/mainThreadLogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export class MainThreadLoggerService implements MainThreadLoggerShape {
this.loggerService.setVisibility(URI.revive(resource), visible);
}

$flush(file: UriComponents): void {
const logger = this.loggerService.getLogger(URI.revive(file));
if (!logger) {
throw new Error('Create the logger before flushing');
}
logger.flush();
}

dispose(): void {
this.disposables.dispose();
}
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,7 @@ export interface ExtHostLogLevelServiceShape {

export interface MainThreadLoggerShape {
$log(file: UriComponents, messages: [LogLevel, string][]): void;
$flush(file: UriComponents): void;
$createLogger(file: UriComponents, options?: ILoggerOptions): Promise<void>;
$registerLogger(logger: UriDto<ILoggerResource>): Promise<void>;
$deregisterLogger(resource: UriComponents): Promise<void>;
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/api/common/extHostLoggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ class Logger extends AbstractMessageLogger {
private doLog(messages: [LogLevel, string][]) {
this.proxy.$log(this.file, messages);
}

override flush(): void {
this.proxy.$flush(this.file);
}
}