Skip to content

Commit c42d534

Browse files
committed
Extract logger as an interface
1 parent 0387965 commit c42d534

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as child_process from "child_process";
55
import * as fs from "node:fs/promises";
66
import { ConfigureButton, OpenSettingsButton } from "./ui/show-error-message";
77
import { ErrorWithNotification } from "./ui/error-with-notification";
8-
import { Logger } from "./logger";
8+
import { LogFilePathProvider, Logger } from "./logger";
99

1010
const exec = util.promisify(child_process.execFile);
1111

@@ -178,13 +178,15 @@ function formatDate(date: Date): string {
178178
* debug configuration. Assumes that the given debug configuration is for a local launch of lldb-dap.
179179
*
180180
* @param logger The {@link Logger} to get default session log location
181+
* @param logFilePath The {@link LogFilePathProvider} for determining where to put session logs
181182
* @param workspaceFolder The {@link vscode.WorkspaceFolder} that the debug session will be launched within
182183
* @param configuration The {@link vscode.DebugConfiguration} that will be launched
183184
* @throws An {@link ErrorWithNotification} if something went wrong
184185
* @returns The {@link vscode.DebugAdapterExecutable} that can be used to launch lldb-dap
185186
*/
186187
export async function createDebugAdapterExecutable(
187188
logger: Logger,
189+
logFilePath: LogFilePathProvider,
188190
workspaceFolder: vscode.WorkspaceFolder | undefined,
189191
configuration: vscode.DebugConfiguration,
190192
): Promise<vscode.DebugAdapterExecutable> {
@@ -194,7 +196,7 @@ export async function createDebugAdapterExecutable(
194196
if (log_path) {
195197
env["LLDBDAP_LOG"] = log_path;
196198
} else if (vscode.workspace.getConfiguration("lldb-dap").get("verboseLogging", false)) {
197-
env["LLDBDAP_LOG"] = logger.logFilePath(`lldb-dap-session-${formatDate(new Date())}.log`);
199+
env["LLDBDAP_LOG"] = logFilePath(`lldb-dap-session-${formatDate(new Date())}.log`);
198200
}
199201
const configEnvironment =
200202
config.get<{ [key: string]: string }>("environment") || {};
@@ -224,7 +226,7 @@ export async function createDebugAdapterExecutable(
224226
export class LLDBDapDescriptorFactory
225227
implements vscode.DebugAdapterDescriptorFactory
226228
{
227-
constructor(private readonly logger: Logger) {}
229+
constructor(private readonly logger: Logger, private logFilePath: LogFilePathProvider) {}
228230

229231
async createDebugAdapterDescriptor(
230232
session: vscode.DebugSession,
@@ -251,6 +253,7 @@ export class LLDBDapDescriptorFactory
251253

252254
return createDebugAdapterExecutable(
253255
this.logger,
256+
this.logFilePath,
254257
session.workspaceFolder,
255258
session.configuration,
256259
);

lldb/tools/lldb-dap/src-ts/debug-configuration-provider.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { LLDBDapServer } from "./lldb-dap-server";
55
import { createDebugAdapterExecutable } from "./debug-adapter-factory";
66
import { ConfigureButton, showErrorMessage } from "./ui/show-error-message";
77
import { ErrorWithNotification } from "./ui/error-with-notification";
8-
import { Logger } from "./logger";
8+
import { LogFilePathProvider, Logger } from "./logger";
99

1010
const exec = util.promisify(child_process.execFile);
1111

@@ -72,7 +72,7 @@ const configurations: Record<string, DefaultConfig> = {
7272
export class LLDBDapConfigurationProvider
7373
implements vscode.DebugConfigurationProvider
7474
{
75-
constructor(private readonly server: LLDBDapServer, private readonly logger: Logger) {}
75+
constructor(private readonly server: LLDBDapServer, private readonly logger: Logger, private readonly logFilePath: LogFilePathProvider) {}
7676

7777
async resolveDebugConfiguration(
7878
folder: vscode.WorkspaceFolder | undefined,
@@ -156,6 +156,7 @@ export class LLDBDapConfigurationProvider
156156
// if there are any.
157157
const executable = await createDebugAdapterExecutable(
158158
this.logger,
159+
this.logFilePath,
159160
folder,
160161
debugConfiguration,
161162
);
@@ -192,7 +193,7 @@ export class LLDBDapConfigurationProvider
192193

193194
return debugConfiguration;
194195
} catch (error) {
195-
this.logger.error(error);
196+
this.logger.error(error as Error);
196197
// Show a better error message to the user if possible
197198
if (!(error instanceof ErrorWithNotification)) {
198199
throw error;

lldb/tools/lldb-dap/src-ts/extension.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import {
1111
ModulesDataProvider,
1212
ModuleProperty,
1313
} from "./ui/modules-data-provider";
14-
import { Logger } from "./logger";
14+
import { LLDBDAPLogger, LogFilePathProvider } from "./logger";
1515

1616
/**
1717
* This class represents the extension and manages its life cycle. Other extensions
1818
* using it as as library should use this class as the main entry point.
1919
*/
2020
export class LLDBDapExtension extends DisposableContext {
21-
constructor(logger: Logger, outputChannel: vscode.OutputChannel) {
21+
constructor(logger: LLDBDAPLogger, logFilePath: LogFilePathProvider, outputChannel: vscode.OutputChannel) {
2222
super();
2323

2424
const lldbDapServer = new LLDBDapServer();
@@ -31,11 +31,11 @@ export class LLDBDapExtension extends DisposableContext {
3131
sessionTracker,
3232
vscode.debug.registerDebugConfigurationProvider(
3333
"lldb-dap",
34-
new LLDBDapConfigurationProvider(lldbDapServer, logger),
34+
new LLDBDapConfigurationProvider(lldbDapServer, logger, logFilePath),
3535
),
3636
vscode.debug.registerDebugAdapterDescriptorFactory(
3737
"lldb-dap",
38-
new LLDBDapDescriptorFactory(logger),
38+
new LLDBDapDescriptorFactory(logger, logFilePath),
3939
),
4040
vscode.debug.registerDebugAdapterTrackerFactory(
4141
"lldb-dap",
@@ -61,8 +61,9 @@ export class LLDBDapExtension extends DisposableContext {
6161
export async function activate(context: vscode.ExtensionContext) {
6262
await vscode.workspace.fs.createDirectory(context.logUri);
6363
const outputChannel = vscode.window.createOutputChannel("LLDB-DAP");
64-
const logger = new Logger((name) => path.join(context.logUri.fsPath, name), outputChannel);
64+
const logFilePath: LogFilePathProvider = (name) => path.join(context.logUri.fsPath, name);
65+
const logger = new LLDBDAPLogger(logFilePath("lldb-dap-extension.log"), outputChannel);
6566
logger.info("LLDB-Dap extension activating...");
66-
context.subscriptions.push(new LLDBDapExtension(logger, outputChannel));
67+
context.subscriptions.push(new LLDBDapExtension(logger, logFilePath, outputChannel));
6768
logger.info("LLDB-Dap extension activated");
6869
}

lldb/tools/lldb-dap/src-ts/logger.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@ class OutputChannelTransport extends Transport {
1313
}
1414
}
1515

16-
export class Logger implements vscode.Disposable {
16+
export type LogFilePathProvider = (name: string) => string;
17+
18+
export interface Logger {
19+
debug(message: string, ...args: any[]): void
20+
error(error: string | Error, ...args: any[]): void
21+
info(message: string, ...args: any[]): void
22+
warn(message: string, ...args: any[]): void
23+
}
24+
25+
export class LLDBDAPLogger implements vscode.Disposable {
1726
private disposables: vscode.Disposable[] = [];
1827
private logger: winston.Logger;
1928

20-
constructor(public readonly logFilePath: (name: string) => string, ouptutChannel: vscode.OutputChannel) {
29+
constructor(public readonly logFilePath: string, ouptutChannel: vscode.OutputChannel) {
2130
const ouptutChannelTransport = new OutputChannelTransport(ouptutChannel);
2231
ouptutChannelTransport.level = this.outputChannelLevel();
2332
this.logger = winston.createLogger({
2433
transports: [
25-
new winston.transports.File({ filename: logFilePath("lldb-dap-extension.log"), level: "debug" }), // File logging at the 'debug' level
34+
new winston.transports.File({ filename: logFilePath, level: "debug" }), // File logging at the 'debug' level
2635
ouptutChannelTransport
2736
],
2837
format: winston.format.combine(
@@ -48,24 +57,25 @@ export class Logger implements vscode.Disposable {
4857
);
4958
}
5059

51-
debug(message: any) {
52-
this.logger.debug(this.normalizeMessage(message));
60+
debug(message: string, ...args: any[]) {
61+
this.logger.debug([message, ...args].map(m => this.normalizeMessage(m)).join(" "));
5362
}
5463

55-
info(message: any) {
56-
this.logger.info(this.normalizeMessage(message));
64+
info(message: string, ...args: any[]) {
65+
this.logger.info([message, ...args].map(m => this.normalizeMessage(m)).join(" "));
5766
}
5867

59-
warn(message: any) {
60-
this.logger.warn(this.normalizeMessage(message));
68+
warn(message: string, ...args: any[]) {
69+
this.logger.warn([message, ...args].map(m => this.normalizeMessage(m)).join(" "));
6170
}
6271

63-
error(message: any) {
72+
error(message: Error | string, ...args: any[]) {
6473
if (message instanceof Error) {
6574
this.logger.error(message);
75+
this.logger.error([...args].map(m => this.normalizeMessage(m)).join(" "));
6676
return;
6777
}
68-
this.logger.error(this.normalizeMessage(message));
78+
this.logger.error([message, ...args].map(m => this.normalizeMessage(m)).join(" "));
6979
}
7080

7181
private normalizeMessage(message: any) {

0 commit comments

Comments
 (0)