Extract client-side logging

This commit is contained in:
Aleksey Kladov 2020-02-21 15:59:46 +01:00
parent 2cbe8a4c4b
commit 49844ab717
10 changed files with 51 additions and 21 deletions

View file

@ -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"
]
}
};

View file

@ -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",

View file

@ -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) {

View file

@ -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<void> {
console.log({ enabled, prev: this.enabled });
log.debug({ enabled, prev: this.enabled });
if (this.enabled === enabled) return;
this.enabled = enabled;

View file

@ -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}`);
}

View file

@ -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) => {

View file

@ -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, {

View file

@ -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<null | string> {
if (!source) {
@ -40,7 +41,7 @@ export async function ensureServerBinary(source: null | BinarySource): Promise<n
const installedVersion: null | string = getServerVersion(source.storage);
const requiredVersion: string = source.version;
console.log("Installed version:", installedVersion, "required:", requiredVersion);
log.debug("Installed version:", installedVersion, "required:", requiredVersion);
if (isBinaryAvailable(prebuiltBinaryPath) && installedVersion === requiredVersion) {
return prebuiltBinaryPath;
@ -72,16 +73,16 @@ async function downloadServer(source: BinarySource.GithubRelease): Promise<boole
`GitHub repository: ${err.message}`
);
console.error(err);
log.error(err);
dns.resolve('example.com').then(
addrs => 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<null | string>("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<void> {
console.log("Set server-version:", version);
log.debug("Set server-version:", version);
await storage.update("server-version", version.toString());
}

View file

@ -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);

18
editors/code/src/util.ts Normal file
View file

@ -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;
}
};