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.
This commit is contained in:
Josh McKinney 2024-07-27 21:43:35 -07:00
parent a46788318c
commit 45a881313a
No known key found for this signature in database
GPG key ID: 722287396A903BC5
6 changed files with 44 additions and 46 deletions

12
.vscode/launch.json vendored
View file

@ -18,7 +18,8 @@
"args": [ "args": [
// "--user-data-dir=${workspaceFolder}/target/code", // "--user-data-dir=${workspaceFolder}/target/code",
"--disable-extensions", "--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}/editors/code" "--extensionDevelopmentPath=${workspaceFolder}/editors/code",
"--log rust-lang.rust-analyzer:debug"
], ],
"outFiles": [ "outFiles": [
"${workspaceFolder}/editors/code/out/**/*.js" "${workspaceFolder}/editors/code/out/**/*.js"
@ -36,7 +37,8 @@
"runtimeExecutable": "${execPath}", "runtimeExecutable": "${execPath}",
"args": [ "args": [
"--disable-extensions", "--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}/editors/code" "--extensionDevelopmentPath=${workspaceFolder}/editors/code",
"--log rust-lang.rust-analyzer:debug"
], ],
"outFiles": [ "outFiles": [
"${workspaceFolder}/editors/code/out/**/*.js" "${workspaceFolder}/editors/code/out/**/*.js"
@ -57,7 +59,8 @@
"runtimeExecutable": "${execPath}", "runtimeExecutable": "${execPath}",
"args": [ "args": [
"--disable-extensions", "--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}/editors/code" "--extensionDevelopmentPath=${workspaceFolder}/editors/code",
"--log rust-lang.rust-analyzer:debug"
], ],
"outFiles": [ "outFiles": [
"${workspaceFolder}/editors/code/out/**/*.js" "${workspaceFolder}/editors/code/out/**/*.js"
@ -79,7 +82,8 @@
"runtimeExecutable": "${execPath}", "runtimeExecutable": "${execPath}",
"args": [ "args": [
"--disable-extension", "rust-lang.rust-analyzer", "--disable-extension", "rust-lang.rust-analyzer",
"--extensionDevelopmentPath=${workspaceFolder}/editors/code" "--extensionDevelopmentPath=${workspaceFolder}/editors/code",
"--log rust-lang.rust-analyzer:debug"
], ],
"outFiles": [ "outFiles": [
"${workspaceFolder}/editors/code/out/**/*.js" "${workspaceFolder}/editors/code/out/**/*.js"

View file

@ -483,6 +483,7 @@
}, },
"rust-analyzer.trace.extension": { "rust-analyzer.trace.extension": {
"description": "Enable logging of VS Code extensions itself.", "description": "Enable logging of VS Code extensions itself.",
"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.",
"type": "boolean", "type": "boolean",
"default": false "default": false
} }

View file

@ -117,9 +117,11 @@ export function isValidExecutable(path: string, extraEnv: Env): boolean {
env: { ...process.env, ...extraEnv }, env: { ...process.env, ...extraEnv },
}); });
const printOutput = res.error ? log.warn : log.info; if (res.error) {
printOutput(path, "--version:", res); log.warn(path, "--version:", res);
} else {
log.info(path, "--version:", res);
}
return res.status === 0; return res.status === 0;
} }

View file

@ -41,7 +41,6 @@ export class Config {
} }
private refreshLogging() { private refreshLogging() {
log.setEnabled(this.traceExtension ?? false);
log.info( log.info(
"Extension version:", "Extension version:",
vscode.extensions.getExtension(this.extensionId)!.packageJSON.version, vscode.extensions.getExtension(this.extensionId)!.packageJSON.version,
@ -253,10 +252,6 @@ export class Config {
await this.cfg.update("checkOnSave", !(value || false), target || null, overrideInLanguage); await this.cfg.update("checkOnSave", !(value || false), target || null, overrideInLanguage);
} }
get traceExtension() {
return this.get<boolean>("trace.extension");
}
get discoverProjectRunner(): string | undefined { get discoverProjectRunner(): string | undefined {
return this.get<string | undefined>("discoverProjectRunner"); return this.get<string | undefined>("discoverProjectRunner");
} }

View file

@ -249,7 +249,8 @@ export class Ctx implements RustAnalyzerExtensionApi {
message += message +=
'See the logs in "OUTPUT > Rust Analyzer Client" (should open automatically). '; 'See the logs in "OUTPUT > Rust Analyzer Client" (should open automatically). ';
message += 'To enable verbose logs use { "rust-analyzer.trace.extension": true }'; message +=
'To enable verbose logs, click the gear icon in the "OUTPUT" tab and select "Debug".';
log.error("Bootstrap error", err); log.error("Bootstrap error", err);
throw new Error(message); throw new Error(message);

View file

@ -17,49 +17,44 @@ export type Env = {
[name: string]: string; [name: string]: string;
}; };
export const log = new (class { class Log {
private enabled = true; private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client", {
private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client"); log: true,
setEnabled(yes: boolean): void {
log.enabled = yes;
}
// Hint: the type [T, ...T[]] means a non-empty array
debug(...msg: [unknown, ...unknown[]]): void {
if (!log.enabled) return;
log.write("DEBUG", ...msg);
}
info(...msg: [unknown, ...unknown[]]): void {
log.write("INFO", ...msg);
}
warn(...msg: [unknown, ...unknown[]]): void {
debugger;
log.write("WARN", ...msg);
}
error(...msg: [unknown, ...unknown[]]): void {
debugger;
log.write("ERROR", ...msg);
log.output.show(true);
}
private write(label: string, ...messageParts: unknown[]): void {
const message = messageParts.map(log.stringify).join(" ");
const dateTime = new Date().toLocaleString();
log.output.appendLine(`${label} [${dateTime}]: ${message}`);
}
private stringify(val: unknown): string {
if (typeof val === "string") return val;
return inspect(val, {
colors: false,
depth: 6, // heuristic
}); });
debug(...messages: [unknown, ...unknown[]]): void {
this.output.debug(this.stringify(messages));
} }
})();
info(...messages: [unknown, ...unknown[]]): void {
this.output.info(this.stringify(messages));
}
warn(...messages: [unknown, ...unknown[]]): void {
this.output.warn(this.stringify(messages));
}
error(...messages: [unknown, ...unknown[]]): void {
this.output.error(this.stringify(messages));
this.output.show(true);
}
private stringify(messages: unknown[]): string {
return messages
.map((message) => {
if (typeof message === "string") {
return message;
}
if (message instanceof Error) {
return message.stack || message.message;
}
return inspect(message, { depth: 6, colors: false });
})
.join(" ");
}
}
export const log = new Log();
export function sleep(ms: number) { export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms)); return new Promise((resolve) => setTimeout(resolve, ms));
@ -135,7 +130,7 @@ export function execute(command: string, options: ExecOptions): Promise<string>
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(command, options, (err, stdout, stderr) => { exec(command, options, (err, stdout, stderr) => {
if (err) { if (err) {
log.error(err); log.error("error:", err);
reject(err); reject(err);
return; return;
} }