Skip to content

Commit

Permalink
Fix #79882 Recurring web logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed Sep 3, 2019
1 parent ef7a57a commit 02eb784
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/vs/platform/log/common/fileLogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import { URI } from 'vs/base/common/uri';
import { IFileService } from 'vs/platform/files/common/files';
import { Queue } from 'vs/base/common/async';
import { VSBuffer } from 'vs/base/common/buffer';
import { dirname, joinPath, basename } from 'vs/base/common/resources';

const MAX_FILE_SIZE = 1024 * 1024 * 5;

export class FileLogService extends AbstractLogService implements ILogService {

_serviceBrand: undefined;

private readonly queue: Queue<void>;
private backupIndex: number = 1;

constructor(
private readonly name: string,
Expand Down Expand Up @@ -81,6 +85,10 @@ export class FileLogService extends AbstractLogService implements ILogService {
private _log(level: LogLevel, message: string): void {
this.queue.queue(async () => {
let content = await this.loadContent();
if (content.length > MAX_FILE_SIZE) {
await this.fileService.writeFile(this.getBackupResource(), VSBuffer.fromString(content));
content = '';
}
content += `[${this.getCurrentTimestamp()}] [${this.name}] [${this.stringifyLogLevel(level)}] ${message}\n`;
await this.fileService.writeFile(this.resource, VSBuffer.fromString(content));
});
Expand All @@ -93,6 +101,11 @@ export class FileLogService extends AbstractLogService implements ILogService {
return `${currentTime.getFullYear()}-${toTwoDigits(currentTime.getMonth() + 1)}-${toTwoDigits(currentTime.getDate())} ${toTwoDigits(currentTime.getHours())}:${toTwoDigits(currentTime.getMinutes())}:${toTwoDigits(currentTime.getSeconds())}.${toThreeDigits(currentTime.getMilliseconds())}`;
}

private getBackupResource(): URI {
this.backupIndex = this.backupIndex > 5 ? 1 : this.backupIndex;
return joinPath(dirname(this.resource), `${basename(this.resource)}_${this.backupIndex++}`);
}

private async loadContent(): Promise<string> {
try {
const content = await this.fileService.readFile(this.resource);
Expand Down

0 comments on commit 02eb784

Please sign in to comment.