Skip to content

Commit c0df26b

Browse files
committed
feat: use vscode log format for client logs
This change updates the log format to use the vscode log format instead of the custom log format, by replacing the `OutputChannel` with a `LogOutputChannel` and using the `debug`, `info`, `warn`, and `error` methods on it. This has the following benefits: - Each log level now has its own color and the timestamp is in a more standard format - Inspect output (e.g. the log of the config object) is now colored - Error stack traces are now shown in the output - The log level is now controlled on the output tab by clicking the gear icon and selecting "Debug" or by passing the `--log` parameter to vscode. The `trace.extension` setting has been marked as deprecated.
1 parent b2719c4 commit c0df26b

File tree

6 files changed

+44
-46
lines changed

6 files changed

+44
-46
lines changed

src/tools/rust-analyzer/.vscode/launch.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"args": [
1919
// "--user-data-dir=${workspaceFolder}/target/code",
2020
"--disable-extensions",
21-
"--extensionDevelopmentPath=${workspaceFolder}/editors/code"
21+
"--extensionDevelopmentPath=${workspaceFolder}/editors/code",
22+
"--log rust-lang.rust-analyzer:debug"
2223
],
2324
"outFiles": [
2425
"${workspaceFolder}/editors/code/out/**/*.js"
@@ -36,7 +37,8 @@
3637
"runtimeExecutable": "${execPath}",
3738
"args": [
3839
"--disable-extensions",
39-
"--extensionDevelopmentPath=${workspaceFolder}/editors/code"
40+
"--extensionDevelopmentPath=${workspaceFolder}/editors/code",
41+
"--log rust-lang.rust-analyzer:debug"
4042
],
4143
"outFiles": [
4244
"${workspaceFolder}/editors/code/out/**/*.js"
@@ -57,7 +59,8 @@
5759
"runtimeExecutable": "${execPath}",
5860
"args": [
5961
"--disable-extensions",
60-
"--extensionDevelopmentPath=${workspaceFolder}/editors/code"
62+
"--extensionDevelopmentPath=${workspaceFolder}/editors/code",
63+
"--log rust-lang.rust-analyzer:debug"
6164
],
6265
"outFiles": [
6366
"${workspaceFolder}/editors/code/out/**/*.js"
@@ -79,7 +82,8 @@
7982
"runtimeExecutable": "${execPath}",
8083
"args": [
8184
"--disable-extension", "rust-lang.rust-analyzer",
82-
"--extensionDevelopmentPath=${workspaceFolder}/editors/code"
85+
"--extensionDevelopmentPath=${workspaceFolder}/editors/code",
86+
"--log rust-lang.rust-analyzer:debug"
8387
],
8488
"outFiles": [
8589
"${workspaceFolder}/editors/code/out/**/*.js"

src/tools/rust-analyzer/editors/code/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@
483483
},
484484
"rust-analyzer.trace.extension": {
485485
"description": "Enable logging of VS Code extensions itself.",
486+
"markdownDeprecationMessage": "Log level is now controlled by the [Developer: Set Log Level...](command:workbench.action.setLogLevel) command.You can set the log level for the current session and also the default log level from there. This is also available by clicking the gear icon on the OUTPUT tab when Rust Analyzer Client is visible or by passing the --log rust-lang.rust-analyzer:debug parameter to VS Code.",
486487
"type": "boolean",
487488
"default": false
488489
}

src/tools/rust-analyzer/editors/code/src/bootstrap.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ export function isValidExecutable(path: string, extraEnv: Env): boolean {
117117
env: { ...process.env, ...extraEnv },
118118
});
119119

120-
const printOutput = res.error ? log.warn : log.info;
121-
printOutput(path, "--version:", res);
122-
120+
if (res.error) {
121+
log.warn(path, "--version:", res);
122+
} else {
123+
log.info(path, "--version:", res);
124+
}
123125
return res.status === 0;
124126
}
125127

src/tools/rust-analyzer/editors/code/src/config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export class Config {
4141
}
4242

4343
private refreshLogging() {
44-
log.setEnabled(this.traceExtension ?? false);
4544
log.info(
4645
"Extension version:",
4746
vscode.extensions.getExtension(this.extensionId)!.packageJSON.version,
@@ -253,10 +252,6 @@ export class Config {
253252
await this.cfg.update("checkOnSave", !(value || false), target || null, overrideInLanguage);
254253
}
255254

256-
get traceExtension() {
257-
return this.get<boolean>("trace.extension");
258-
}
259-
260255
get discoverProjectRunner(): string | undefined {
261256
return this.get<string | undefined>("discoverProjectRunner");
262257
}

src/tools/rust-analyzer/editors/code/src/ctx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ export class Ctx implements RustAnalyzerExtensionApi {
249249

250250
message +=
251251
'See the logs in "OUTPUT > Rust Analyzer Client" (should open automatically). ';
252-
message += 'To enable verbose logs use { "rust-analyzer.trace.extension": true }';
252+
message +=
253+
'To enable verbose logs, click the gear icon in the "OUTPUT" tab and select "Debug".';
253254

254255
log.error("Bootstrap error", err);
255256
throw new Error(message);

src/tools/rust-analyzer/editors/code/src/util.ts

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,44 @@ export type Env = {
1717
[name: string]: string;
1818
};
1919

20-
export const log = new (class {
21-
private enabled = true;
22-
private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client");
23-
24-
setEnabled(yes: boolean): void {
25-
log.enabled = yes;
26-
}
20+
class Log {
21+
private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client", {
22+
log: true,
23+
});
2724

28-
// Hint: the type [T, ...T[]] means a non-empty array
29-
debug(...msg: [unknown, ...unknown[]]): void {
30-
if (!log.enabled) return;
31-
log.write("DEBUG", ...msg);
25+
debug(...messages: [unknown, ...unknown[]]): void {
26+
this.output.debug(this.stringify(messages));
3227
}
3328

34-
info(...msg: [unknown, ...unknown[]]): void {
35-
log.write("INFO", ...msg);
29+
info(...messages: [unknown, ...unknown[]]): void {
30+
this.output.info(this.stringify(messages));
3631
}
3732

38-
warn(...msg: [unknown, ...unknown[]]): void {
39-
debugger;
40-
log.write("WARN", ...msg);
33+
warn(...messages: [unknown, ...unknown[]]): void {
34+
this.output.warn(this.stringify(messages));
4135
}
4236

43-
error(...msg: [unknown, ...unknown[]]): void {
44-
debugger;
45-
log.write("ERROR", ...msg);
46-
log.output.show(true);
37+
error(...messages: [unknown, ...unknown[]]): void {
38+
this.output.error(this.stringify(messages));
39+
this.output.show(true);
4740
}
4841

49-
private write(label: string, ...messageParts: unknown[]): void {
50-
const message = messageParts.map(log.stringify).join(" ");
51-
const dateTime = new Date().toLocaleString();
52-
log.output.appendLine(`${label} [${dateTime}]: ${message}`);
42+
private stringify(messages: unknown[]): string {
43+
return messages
44+
.map((message) => {
45+
if (typeof message === "string") {
46+
return message;
47+
}
48+
if (message instanceof Error) {
49+
return message.stack || message.message;
50+
}
51+
return inspect(message, { depth: 6, colors: false });
52+
})
53+
.join(" ");
5354
}
55+
}
5456

55-
private stringify(val: unknown): string {
56-
if (typeof val === "string") return val;
57-
return inspect(val, {
58-
colors: false,
59-
depth: 6, // heuristic
60-
});
61-
}
62-
})();
57+
export const log = new Log();
6358

6459
export function sleep(ms: number) {
6560
return new Promise((resolve) => setTimeout(resolve, ms));
@@ -135,7 +130,7 @@ export function execute(command: string, options: ExecOptions): Promise<string>
135130
return new Promise((resolve, reject) => {
136131
exec(command, options, (err, stdout, stderr) => {
137132
if (err) {
138-
log.error(err);
133+
log.error("error:", err);
139134
reject(err);
140135
return;
141136
}

0 commit comments

Comments
 (0)