diff --git a/.vscode-test.js b/.vscode-test.js index de3bd589f..9d6ceeeef 100644 --- a/.vscode-test.js +++ b/.vscode-test.js @@ -80,7 +80,7 @@ if (vsixPath) { extensionDependencies.push("vadimcn.vscode-lldb", "llvm-vs-code-extensions.lldb-dap"); } -const vscodeVersion = process.env["VSCODE_VERSION"] ?? "1.106.3"; +const vscodeVersion = process.env["VSCODE_VERSION"] ?? "stable"; log("Running tests against VS Code version " + vscodeVersion); const installConfigs = []; diff --git a/src/logging/RollingLog.ts b/src/logging/RollingLog.ts index f168137c4..36d11c3be 100644 --- a/src/logging/RollingLog.ts +++ b/src/logging/RollingLog.ts @@ -11,9 +11,8 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -import * as vscode from "vscode"; -export class RollingLog implements vscode.Disposable { +export class RollingLog { private _logs: (string | null)[]; private startIndex: number = 0; private endIndex: number = 0; @@ -35,10 +34,6 @@ export class RollingLog implements vscode.Disposable { return (index + 1) % this.maxLogs; } - dispose() { - this.clear(); - } - clear() { this._logs = new Array(this.maxLogs).fill(null); this.startIndex = 0; diff --git a/src/logging/SwiftLogger.ts b/src/logging/SwiftLogger.ts index ee29ee7d5..bef8d78c9 100644 --- a/src/logging/SwiftLogger.ts +++ b/src/logging/SwiftLogger.ts @@ -13,9 +13,10 @@ //===----------------------------------------------------------------------===// import * as vscode from "vscode"; import * as winston from "winston"; +import type * as Transport from "winston-transport"; import configuration from "../configuration"; -import { IS_RUNNING_IN_DEVELOPMENT_MODE, IS_RUNNING_UNDER_TEST } from "../utilities/utilities"; +import { IS_RUNNING_UNDER_TEST } from "../utilities/utilities"; import { FileTransport } from "./FileTransport"; import { OutputChannelTransport } from "./OutputChannelTransport"; import { RollingLog } from "./RollingLog"; @@ -28,12 +29,13 @@ type LoggerMeta = any; type LogMessageOptions = { append: boolean }; export class SwiftLogger implements vscode.Disposable { - private disposables: vscode.Disposable[] = []; + private subscriptions: vscode.Disposable[] = []; private logger: winston.Logger; protected rollingLog: RollingLog; protected outputChannel: vscode.OutputChannel; private fileTransport: FileTransport; private cachedOutputChannelLevel: string | undefined; + private isDisposed: boolean = false; constructor( public readonly name: string, @@ -44,22 +46,18 @@ export class SwiftLogger implements vscode.Disposable { this.outputChannel = vscode.window.createOutputChannel(name); const ouptutChannelTransport = new OutputChannelTransport(this.outputChannel); ouptutChannelTransport.level = this.outputChannelLevel; - const rollingLogTransport = new RollingLogTransport(this.rollingLog); // Create file transport this.fileTransport = new FileTransport(this.logFilePath); this.fileTransport.level = "debug"; // File logging at the 'debug' level always // Create logger with all transports - const transports = [ - ouptutChannelTransport, - this.fileTransport, - // Only want to capture the rolling log in memory when testing - ...(IS_RUNNING_UNDER_TEST ? [rollingLogTransport] : []), - ...(IS_RUNNING_IN_DEVELOPMENT_MODE - ? [new winston.transports.Console({ level: "debug" })] - : []), - ]; + const transports: Transport[] = [ouptutChannelTransport, this.fileTransport]; + if (IS_RUNNING_UNDER_TEST) { + // We only want to capture the rolling log in memory when testing + const rollingLogTransport = new RollingLogTransport(this.rollingLog); + transports.push(rollingLogTransport); + } this.logger = winston.createLogger({ transports: transports, @@ -72,19 +70,7 @@ export class SwiftLogger implements vscode.Disposable { winston.format.colorize() ), }); - this.disposables.push( - { - dispose: () => { - this.logger.close(); - if (ouptutChannelTransport.close) { - ouptutChannelTransport.close(); - } - if (rollingLogTransport.close) { - rollingLogTransport.close(); - } - this.fileTransport.close(); - }, - }, + this.subscriptions.push( vscode.workspace.onDidChangeConfiguration(e => { if ( e.affectsConfiguration("swift.outputChannelLogLevel") || @@ -123,6 +109,10 @@ export class SwiftLogger implements vscode.Disposable { } private logWithBuffer(level: string, message: string | Error, meta?: LoggerMeta) { + if (this.isDisposed) { + return; + } + // Log to all transports (output channel, file, console, etc.) if (message instanceof Error) { this.logger.log(level, message); @@ -177,6 +167,9 @@ export class SwiftLogger implements vscode.Disposable { } dispose() { - this.disposables.forEach(d => d.dispose()); + this.isDisposed = true; + this.logger.close(); + this.rollingLog.clear(); + this.subscriptions.forEach(d => d.dispose()); } } diff --git a/src/utilities/utilities.ts b/src/utilities/utilities.ts index c30f446bb..77d05c419 100644 --- a/src/utilities/utilities.ts +++ b/src/utilities/utilities.ts @@ -50,12 +50,6 @@ export const IS_RUNNING_UNDER_DOCKER = IS_RUNNING_UNDER_ACT || IS_RUNNING_UNDER_ */ export const IS_RUNNING_UNDER_TEST = process.env.RUNNING_UNDER_VSCODE_TEST_CLI === "1"; -/** - * Determined by the presence of the `VSCODE_DEBUG` environment variable, set by the - * launch.json when starting the extension in development. - */ -export const IS_RUNNING_IN_DEVELOPMENT_MODE = process.env["VSCODE_DEBUG"] === "1"; - /** Determines whether the provided object has any properties set to non-null values. */ export function isEmptyObject(obj: { [key: string]: unknown }): boolean { const properties = Object.getOwnPropertyNames(obj).filter(