Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .vscode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
7 changes: 1 addition & 6 deletions src/logging/RollingLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
45 changes: 19 additions & 26 deletions src/logging/SwiftLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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") ||
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}
6 changes: 0 additions & 6 deletions src/utilities/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading