From 9ec76f9c7002bd32d22707b90e4c35ce9b1c00da Mon Sep 17 00:00:00 2001 From: Sandeep Somavarapu Date: Thu, 22 Aug 2019 00:01:49 +0200 Subject: [PATCH] logs data clean up --- .../contrib/logs/common/logs.contribution.ts | 6 ++- .../contrib/logs/common/logsDataCleaner.ts | 44 +++++++++++++++++++ .../environment/browser/environmentService.ts | 2 +- .../log/browser/indexedDBLogProvider.ts | 13 +++--- 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 src/vs/workbench/contrib/logs/common/logsDataCleaner.ts diff --git a/src/vs/workbench/contrib/logs/common/logs.contribution.ts b/src/vs/workbench/contrib/logs/common/logs.contribution.ts index 7030e38f8ee..582f1b4efda 100644 --- a/src/vs/workbench/contrib/logs/common/logs.contribution.ts +++ b/src/vs/workbench/contrib/logs/common/logs.contribution.ts @@ -20,6 +20,8 @@ import { ILogService, LogLevel } from 'vs/platform/log/common/log'; import { dirname } from 'vs/base/common/resources'; import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle'; import { isWeb } from 'vs/base/common/platform'; +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { LogsDataCleaner } from 'vs/workbench/contrib/logs/common/logsDataCleaner'; const workbenchActionsRegistry = Registry.as(WorkbenchActionExtensions.WorkbenchActions); const devCategory = nls.localize('developer', "Developer"); @@ -30,7 +32,8 @@ class LogOutputChannels extends Disposable implements IWorkbenchContribution { constructor( @IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService, @ILogService private readonly logService: ILogService, - @IFileService private readonly fileService: IFileService + @IFileService private readonly fileService: IFileService, + @IInstantiationService private readonly instantiationService: IInstantiationService ) { super(); if (isWeb) { @@ -42,6 +45,7 @@ class LogOutputChannels extends Disposable implements IWorkbenchContribution { private registerWebContributions(): void { Registry.as(OutputExt.OutputChannels).registerChannel({ id: Constants.rendererLogChannelId, label: nls.localize('rendererLog', "Window"), file: this.environmentService.logFile, log: true }); + this.instantiationService.createInstance(LogsDataCleaner); } private registerNativeContributions(): void { diff --git a/src/vs/workbench/contrib/logs/common/logsDataCleaner.ts b/src/vs/workbench/contrib/logs/common/logsDataCleaner.ts new file mode 100644 index 00000000000..90d328690ed --- /dev/null +++ b/src/vs/workbench/contrib/logs/common/logsDataCleaner.ts @@ -0,0 +1,44 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { Disposable } from 'vs/base/common/lifecycle'; +import { IFileService } from 'vs/platform/files/common/files'; +import { basename, dirname } from 'vs/base/common/resources'; +import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService'; +import { URI } from 'vs/base/common/uri'; +import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle'; + +export class LogsDataCleaner extends Disposable { + + constructor( + @IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService, + @IFileService private readonly fileService: IFileService, + @ILifecycleService private readonly lifecycleService: ILifecycleService, + ) { + super(); + this.cleanUpOldLogsSoon(); + } + + private cleanUpOldLogsSoon(): void { + let handle: NodeJS.Timeout | undefined = setTimeout(async () => { + handle = undefined; + const logsPath = URI.file(this.environmentService.logsPath).with({ scheme: this.environmentService.logFile.scheme }); + const stat = await this.fileService.resolve(dirname(logsPath)); + if (stat.children) { + const currentLog = basename(logsPath); + const allSessions = stat.children.filter(stat => stat.isDirectory && /^\d{8}T\d{6}$/.test(stat.name)); + const oldSessions = allSessions.sort().filter((d, i) => d.name !== currentLog); + const toDelete = oldSessions.slice(0, Math.max(0, oldSessions.length - 49)); + Promise.all(toDelete.map(stat => this.fileService.del(stat.resource, { recursive: true }))); + } + }, 10 * 1000); + this.lifecycleService.onWillShutdown(() => { + if (handle) { + clearTimeout(handle); + handle = undefined; + } + }); + } +} diff --git a/src/vs/workbench/services/environment/browser/environmentService.ts b/src/vs/workbench/services/environment/browser/environmentService.ts index 9ee69bc4198..821ba14cb26 100644 --- a/src/vs/workbench/services/environment/browser/environmentService.ts +++ b/src/vs/workbench/services/environment/browser/environmentService.ts @@ -76,7 +76,7 @@ export class BrowserWorkbenchEnvironmentService implements IWorkbenchEnvironment constructor(readonly options: IBrowserWorkbenchEnvironemntConstructionOptions) { this.args = { _: [] }; this.logsPath = options.logsPath.path; - this.logFile = joinPath(options.logsPath, `window-${generateUuid()}`); + this.logFile = joinPath(options.logsPath, `window-${generateUuid()}.log`); this.appRoot = '/web/'; this.appNameLong = 'Visual Studio Code - Web'; diff --git a/src/vs/workbench/services/log/browser/indexedDBLogProvider.ts b/src/vs/workbench/services/log/browser/indexedDBLogProvider.ts index 064e6bd6773..da2859fc206 100644 --- a/src/vs/workbench/services/log/browser/indexedDBLogProvider.ts +++ b/src/vs/workbench/services/log/browser/indexedDBLogProvider.ts @@ -9,7 +9,7 @@ import { Disposable, IDisposable } from 'vs/base/common/lifecycle'; import { Event, Emitter } from 'vs/base/common/event'; import { VSBuffer } from 'vs/base/common/buffer'; import { FileSystemError } from 'vs/workbench/api/common/extHostTypes'; -import { isEqualOrParent, joinPath } from 'vs/base/common/resources'; +import { isEqualOrParent, joinPath, relativePath } from 'vs/base/common/resources'; const LOGS_OBJECT_STORE = 'logs'; export const INDEXEDDB_LOG_SCHEME = 'vscode-logs-indexedbd'; @@ -95,11 +95,14 @@ export class IndexedDBLogProvider extends Disposable implements IFileSystemProvi request.onerror = () => e(request.error); request.onsuccess = () => { const files: [string, FileType][] = []; - const resourceSegments = resource.path.split('/'); for (const key of request.result) { - if (isEqualOrParent(URI.file(key).with({ scheme: INDEXEDDB_LOG_SCHEME }), resource, false)) { - const keySegments = key.split('/'); - files.push([keySegments[resourceSegments.length], resourceSegments.length + 1 === keySegments.length ? FileType.File : FileType.Directory]); + const keyResource = this.toResource(key); + if (isEqualOrParent(keyResource, resource, false)) { + const path = relativePath(resource, keyResource, false); + if (path) { + const keySegments = path.split('/'); + files.push([keySegments[0], keySegments.length === 1 ? FileType.File : FileType.Directory]); + } } } c(files); -- GitLab