From 49844ab717d8d1790dbdf7f44d160936ece0e80f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 21 Feb 2020 15:59:46 +0100 Subject: Extract client-side logging --- editors/code/.eslintrc.js | 7 ++++--- editors/code/package.json | 5 +++++ editors/code/src/config.ts | 5 ++++- editors/code/src/inlay_hints.ts | 3 ++- editors/code/src/installation/download_artifact.ts | 2 -- editors/code/src/installation/download_file.ts | 7 ++++--- .../src/installation/fetch_artifact_release_info.ts | 3 ++- editors/code/src/installation/server.ts | 19 ++++++++++--------- editors/code/src/main.ts | 3 ++- editors/code/src/util.ts | 18 ++++++++++++++++++ 10 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 editors/code/src/util.ts (limited to 'editors') diff --git a/editors/code/.eslintrc.js b/editors/code/.eslintrc.js index d494ebce8..16f18ab2c 100644 --- a/editors/code/.eslintrc.js +++ b/editors/code/.eslintrc.js @@ -12,8 +12,10 @@ module.exports = { "@typescript-eslint" ], "rules": { - "eqeqeq": ["error", "always", { "null": "ignore" }], "camelcase": ["error"], + "eqeqeq": ["error", "always", { "null": "ignore" }], + "no-console": ["error"], + "prefer-const": "error", "@typescript-eslint/member-delimiter-style": [ "error", { @@ -30,7 +32,6 @@ module.exports = { "@typescript-eslint/semi": [ "error", "always" - ], - "prefer-const": "error" + ] } }; diff --git a/editors/code/package.json b/editors/code/package.json index 72befe2b6..9ef6c6983 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -252,6 +252,11 @@ "default": "off", "description": "Trace requests to the rust-analyzer" }, + "rust-analyzer.trace.extension": { + "description": "Enable logging of VS Code extensions itself", + "type": "boolean", + "default": false + }, "rust-analyzer.lruCapacity": { "type": [ "null", diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 347c989c4..47e8cd45d 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -1,6 +1,7 @@ import * as os from "os"; import * as vscode from 'vscode'; import { BinarySource } from "./installation/interfaces"; +import { log } from "./util"; const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; @@ -46,7 +47,9 @@ export class Config { private refreshConfig() { this.cfg = vscode.workspace.getConfiguration(Config.rootSection); - console.log("Using configuration:", this.cfg); + const enableLogging = this.cfg.get("trace.extension") as boolean; + log.setEnabled(enableLogging); + log.debug("Using configuration:", this.cfg); } private async onConfigChange(event: vscode.ConfigurationChangeEvent) { diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index 641ec15c6..7e6c310a9 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; import { Ctx, sendRequestWithRetry } from './ctx'; +import { log } from './util'; export function activateInlayHints(ctx: Ctx) { const hintsUpdater = new HintsUpdater(ctx); @@ -71,7 +72,7 @@ class HintsUpdater { } async setEnabled(enabled: boolean): Promise { - console.log({ enabled, prev: this.enabled }); + log.debug({ enabled, prev: this.enabled }); if (this.enabled === enabled) return; this.enabled = enabled; diff --git a/editors/code/src/installation/download_artifact.ts b/editors/code/src/installation/download_artifact.ts index 9996c556f..356723aba 100644 --- a/editors/code/src/installation/download_artifact.ts +++ b/editors/code/src/installation/download_artifact.ts @@ -29,7 +29,6 @@ export async function downloadArtifact( const installationPath = path.join(installationDir, artifactFileName); - console.time(`Downloading ${artifactFileName}`); await vscode.window.withProgress( { location: vscode.ProgressLocation.Notification, @@ -54,5 +53,4 @@ export async function downloadArtifact( ); } ); - console.timeEnd(`Downloading ${artifactFileName}`); } diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index d154f4816..319cb995c 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts @@ -3,6 +3,7 @@ import * as fs from "fs"; import * as stream from "stream"; import * as util from "util"; import { strict as assert } from "assert"; +import { log } from "../util"; const pipeline = util.promisify(stream.pipeline); @@ -21,8 +22,8 @@ export async function downloadFile( const res = await fetch(url); if (!res.ok) { - console.log("Error", res.status, "while downloading file from", url); - console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 }); + log.error("Error", res.status, "while downloading file from", url); + log.error({ body: await res.text(), headers: res.headers }); throw new Error(`Got response ${res.status} when trying to download a file.`); } @@ -30,7 +31,7 @@ export async function downloadFile( const totalBytes = Number(res.headers.get('content-length')); assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); - console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); + log.debug("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); let readBytes = 0; res.body.on("data", (chunk: Buffer) => { diff --git a/editors/code/src/installation/fetch_artifact_release_info.ts b/editors/code/src/installation/fetch_artifact_release_info.ts index 1e764718c..1b6fc8d48 100644 --- a/editors/code/src/installation/fetch_artifact_release_info.ts +++ b/editors/code/src/installation/fetch_artifact_release_info.ts @@ -1,5 +1,6 @@ import fetch from "node-fetch"; import { GithubRepo, ArtifactReleaseInfo } from "./interfaces"; +import { log } from "../util"; const GITHUB_API_ENDPOINT_URL = "https://api.github.com"; @@ -24,7 +25,7 @@ export async function fetchArtifactReleaseInfo( // We skip runtime type checks for simplicity (here we cast from `any` to `GithubRelease`) - console.log("Issuing request for released artifacts metadata to", requestUrl); + log.debug("Issuing request for released artifacts metadata to", requestUrl); // FIXME: handle non-ok response const response: GithubRelease = await fetch(requestUrl, { diff --git a/editors/code/src/installation/server.ts b/editors/code/src/installation/server.ts index 750852921..685abfdc6 100644 --- a/editors/code/src/installation/server.ts +++ b/editors/code/src/installation/server.ts @@ -7,6 +7,7 @@ import { spawnSync } from "child_process"; import { BinarySource } from "./interfaces"; import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info"; import { downloadArtifact } from "./download_artifact"; +import { log } from "../util"; export async function ensureServerBinary(source: null | BinarySource): Promise { if (!source) { @@ -40,7 +41,7 @@ export async function ensureServerBinary(source: null | BinarySource): Promise console.log("DNS resolution for example.com was successful", addrs), + addrs => log.debug("DNS resolution for example.com was successful", addrs), err => { - console.error( + log.error( "DNS resolution for example.com failed, " + "there might be an issue with Internet availability" ); - console.error(err); + log.error(err); } ); return false; @@ -105,19 +106,19 @@ function isBinaryAvailable(binaryPath: string): boolean { // ACHTUNG! `res` type declaration is inherently wrong, see // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/42221 - console.log("Checked binary availablity via --version", res); - console.log(binaryPath, "--version output:", res.output?.map(String)); + log.debug("Checked binary availablity via --version", res); + log.debug(binaryPath, "--version output:", res.output?.map(String)); return res.status === 0; } function getServerVersion(storage: vscode.Memento): null | string { const version = storage.get("server-version", null); - console.log("Get server-version:", version); + log.debug("Get server-version:", version); return version; } async function setServerVersion(storage: vscode.Memento, version: string): Promise { - console.log("Set server-version:", version); + log.debug("Set server-version:", version); await storage.update("server-version", version.toString()); } diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index de19a44e5..7b3bb6302 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -7,6 +7,7 @@ import { Ctx } from './ctx'; import { activateHighlighting } from './highlighting'; import { ensureServerBinary } from './installation/server'; import { Config } from './config'; +import { log } from './util'; let ctx: Ctx | undefined; @@ -38,7 +39,7 @@ export async function activate(context: vscode.ExtensionContext) { try { sub.dispose(); } catch (e) { - console.error(e); + log.error(e); } } await activate(context); diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts new file mode 100644 index 000000000..7a6657753 --- /dev/null +++ b/editors/code/src/util.ts @@ -0,0 +1,18 @@ +let enabled: boolean = false; + +export const log = { + debug(message?: any, ...optionalParams: any[]): void { + if (!enabled) return; + // eslint-disable-next-line no-console + console.log(message, ...optionalParams); + }, + error(message?: any, ...optionalParams: any[]): void { + if (!enabled) return; + debugger; + // eslint-disable-next-line no-console + console.error(message, ...optionalParams); + }, + setEnabled(yes: boolean): void { + enabled = yes; + } +}; -- cgit v1.2.3