Skip to content

Commit ee72122

Browse files
committed
Auto merge of rust-lang#17722 - joshka:jm/logs, r=Veykril
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. Motivation: The large uncolored unformatted log output with a large config object logged whenever it changes has always dominated the logs. This subjectively has made it that looking to see what the client is doing has always been a bit disappointing. That said, there's only 17 log messages total in the client. Hopefully by making the logs more visually useful this will encourage adding more appropriate debug level messages in future. Incidentally, it might be worth only logging the config change message at a debug level instead of an info level to reduce the noise.
2 parents 48fb66b + 2f0451d commit ee72122

File tree

6 files changed

+46
-44
lines changed

6 files changed

+46
-44
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: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,48 @@ 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");
20+
class Log {
21+
private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client", {
22+
log: true,
23+
});
2324

24-
setEnabled(yes: boolean): void {
25-
log.enabled = yes;
25+
trace(...messages: [unknown, ...unknown[]]): void {
26+
this.output.trace(this.stringify(messages));
2627
}
2728

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);
29+
debug(...messages: [unknown, ...unknown[]]): void {
30+
this.output.debug(this.stringify(messages));
3231
}
3332

34-
info(...msg: [unknown, ...unknown[]]): void {
35-
log.write("INFO", ...msg);
33+
info(...messages: [unknown, ...unknown[]]): void {
34+
this.output.info(this.stringify(messages));
3635
}
3736

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

43-
error(...msg: [unknown, ...unknown[]]): void {
44-
debugger;
45-
log.write("ERROR", ...msg);
46-
log.output.show(true);
41+
error(...messages: [unknown, ...unknown[]]): void {
42+
this.output.error(this.stringify(messages));
43+
this.output.show(true);
4744
}
4845

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}`);
46+
private stringify(messages: unknown[]): string {
47+
return messages
48+
.map((message) => {
49+
if (typeof message === "string") {
50+
return message;
51+
}
52+
if (message instanceof Error) {
53+
return message.stack || message.message;
54+
}
55+
return inspect(message, { depth: 6, colors: false });
56+
})
57+
.join(" ");
5358
}
59+
}
5460

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-
})();
61+
export const log = new Log();
6362

6463
export function sleep(ms: number) {
6564
return new Promise((resolve) => setTimeout(resolve, ms));
@@ -135,7 +134,7 @@ export function execute(command: string, options: ExecOptions): Promise<string>
135134
return new Promise((resolve, reject) => {
136135
exec(command, options, (err, stdout, stderr) => {
137136
if (err) {
138-
log.error(err);
137+
log.error("error:", err);
139138
reject(err);
140139
return;
141140
}

0 commit comments

Comments
 (0)