about summary refs log tree commit diff
path: root/editors/code/src/util.ts
diff options
context:
space:
mode:
authorVeetaha <veetaha2@gmail.com>2020-07-05 17:42:52 +0300
committerVeetaha <veetaha2@gmail.com>2020-07-05 17:50:29 +0300
commit3602f07bbee5b13dcd799cbc79381e9428808048 (patch)
tree6ab6a643f8ae0d4486b4078c757f8a31a93d0111 /editors/code/src/util.ts
parent8b0983e89ad9a28b142eccf3755a8c9aaeb37852 (diff)
downloadrust-3602f07bbee5b13dcd799cbc79381e9428808048.tar.gz
rust-3602f07bbee5b13dcd799cbc79381e9428808048.zip
Improve client logging (use output channel and more log levels)
Diffstat (limited to 'editors/code/src/util.ts')
-rw-r--r--editors/code/src/util.ts51
1 files changed, 41 insertions, 10 deletions
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts
index fec4c3295e0..78fe6f5dab9 100644
--- a/editors/code/src/util.ts
+++ b/editors/code/src/util.ts
@@ -1,7 +1,9 @@
 import * as lc from "vscode-languageclient";
+import * as fs from "fs";
 import * as vscode from "vscode";
 import { strict as nativeAssert } from "assert";
 import { spawnSync } from "child_process";
+import { inspect } from "util";
 
 export function assert(condition: boolean, explanation: string): asserts condition {
     try {
@@ -14,21 +16,46 @@ export function assert(condition: boolean, explanation: string): asserts conditi
 
 export const log = new class {
     private enabled = true;
+    private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client");
 
     setEnabled(yes: boolean): void {
         log.enabled = yes;
     }
 
-    debug(message?: any, ...optionalParams: any[]): void {
+    // Hint: the type [T, ...T[]] means a non-empty array
+    debug(...msg: [unknown, ...unknown[]]): void {
         if (!log.enabled) return;
-        // eslint-disable-next-line no-console
-        console.log(message, ...optionalParams);
+        log.write("DEBUG", ...msg);
+        log.output.toString();
     }
 
-    error(message?: any, ...optionalParams: any[]): void {
+    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;
-        // eslint-disable-next-line no-console
-        console.error(message, ...optionalParams);
+        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
+        });
     }
 };
 
@@ -46,7 +73,7 @@ export async function sendRequestWithRetry<TParam, TRet>(
             );
         } catch (error) {
             if (delay === null) {
-                log.error("LSP request timed out", { method: reqType.method, param, error });
+                log.warn("LSP request timed out", { method: reqType.method, param, error });
                 throw error;
             }
 
@@ -55,7 +82,7 @@ export async function sendRequestWithRetry<TParam, TRet>(
             }
 
             if (error.code !== lc.ErrorCodes.ContentModified) {
-                log.error("LSP request failed", { method: reqType.method, param, error });
+                log.warn("LSP request failed", { method: reqType.method, param, error });
                 throw error;
             }
 
@@ -87,11 +114,15 @@ export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor {
 export function isValidExecutable(path: string): boolean {
     log.debug("Checking availability of a binary at", path);
 
+    if (!fs.existsSync(path)) return false;
+
     const res = spawnSync(path, ["--version"], { encoding: 'utf8' });
 
-    log.debug(res, "--version output:", res.output);
+    const isSuccess = res.status === 0;
+    const printOutput = isSuccess ? log.debug : log.warn;
+    printOutput(path, "--version:", res);
 
-    return res.status === 0;
+    return isSuccess;
 }
 
 /** Sets ['when'](https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts) clause contexts */