11import * as lc from "vscode-languageclient" ;
2+ import * as fs from "fs" ;
23import * as vscode from "vscode" ;
34import { strict as nativeAssert } from "assert" ;
45import { spawnSync } from "child_process" ;
6+ import { inspect } from "util" ;
57
68export function assert ( condition : boolean , explanation : string ) : asserts condition {
79 try {
@@ -14,21 +16,46 @@ export function assert(condition: boolean, explanation: string): asserts conditi
1416
1517export const log = new class {
1618 private enabled = true ;
19+ private readonly output = vscode . window . createOutputChannel ( "Rust Analyzer Client" ) ;
1720
1821 setEnabled ( yes : boolean ) : void {
1922 log . enabled = yes ;
2023 }
2124
22- debug ( message ?: any , ...optionalParams : any [ ] ) : void {
25+ // Hint: the type [T, ...T[]] means a non-empty array
26+ debug ( ...msg : [ unknown , ...unknown [ ] ] ) : void {
2327 if ( ! log . enabled ) return ;
24- // eslint-disable-next-line no-console
25- console . log ( message , ... optionalParams ) ;
28+ log . write ( "DEBUG" , ... msg ) ;
29+ log . output . toString ( ) ;
2630 }
2731
28- error ( message ?: any , ...optionalParams : any [ ] ) : void {
32+ info ( ...msg : [ unknown , ...unknown [ ] ] ) : void {
33+ log . write ( "INFO" , ...msg ) ;
34+ }
35+
36+ warn ( ...msg : [ unknown , ...unknown [ ] ] ) : void {
37+ debugger ;
38+ log . write ( "WARN" , ...msg ) ;
39+ }
40+
41+ error ( ...msg : [ unknown , ...unknown [ ] ] ) : void {
2942 debugger ;
30- // eslint-disable-next-line no-console
31- console . error ( message , ...optionalParams ) ;
43+ log . write ( "ERROR" , ...msg ) ;
44+ log . output . show ( true ) ;
45+ }
46+
47+ private write ( label : string , ...messageParts : unknown [ ] ) : void {
48+ const message = messageParts . map ( log . stringify ) . join ( " " ) ;
49+ const dateTime = new Date ( ) . toLocaleString ( ) ;
50+ log . output . appendLine ( `${ label } [${ dateTime } ]: ${ message } ` ) ;
51+ }
52+
53+ private stringify ( val : unknown ) : string {
54+ if ( typeof val === "string" ) return val ;
55+ return inspect ( val , {
56+ colors : false ,
57+ depth : 6 , // heuristic
58+ } ) ;
3259 }
3360} ;
3461
@@ -46,7 +73,7 @@ export async function sendRequestWithRetry<TParam, TRet>(
4673 ) ;
4774 } catch ( error ) {
4875 if ( delay === null ) {
49- log . error ( "LSP request timed out" , { method : reqType . method , param, error } ) ;
76+ log . warn ( "LSP request timed out" , { method : reqType . method , param, error } ) ;
5077 throw error ;
5178 }
5279
@@ -55,7 +82,7 @@ export async function sendRequestWithRetry<TParam, TRet>(
5582 }
5683
5784 if ( error . code !== lc . ErrorCodes . ContentModified ) {
58- log . error ( "LSP request failed" , { method : reqType . method , param, error } ) ;
85+ log . warn ( "LSP request failed" , { method : reqType . method , param, error } ) ;
5986 throw error ;
6087 }
6188
@@ -87,11 +114,15 @@ export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor {
87114export function isValidExecutable ( path : string ) : boolean {
88115 log . debug ( "Checking availability of a binary at" , path ) ;
89116
117+ if ( ! fs . existsSync ( path ) ) return false ;
118+
90119 const res = spawnSync ( path , [ "--version" ] , { encoding : 'utf8' } ) ;
91120
92- log . debug ( res , "--version output:" , res . output ) ;
121+ const isSuccess = res . status === 0 ;
122+ const printOutput = isSuccess ? log . debug : log . warn ;
123+ printOutput ( path , "--version:" , res ) ;
93124
94- return res . status === 0 ;
125+ return isSuccess ;
95126}
96127
97128/** Sets ['when'](https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts) clause contexts */
0 commit comments