From 87178d1e5ec5f241be67d210038ffadb2406d2f3 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Sat, 18 May 2024 15:12:32 +0800 Subject: [PATCH] chore: Update logger.trace calls to improve code readability --- src/context/contextCustomCommand.ts | 8 +- .../codecomplete/ast/treeSitter.ts | 4 +- .../codecomplete/astIndex/index.ts | 6 +- src/contributes/codecomplete/astTest.ts | 2 +- src/contributes/codecomplete/codecomplete.ts | 28 +--- src/contributes/codecomplete/llm.ts | 8 +- src/contributes/codecomplete/promptCreator.ts | 2 +- .../codecomplete/symbolindex/embedding.ts | 2 +- src/extension.ts | 1 - src/handler/messageHandler.ts | 11 +- src/handler/sendMessageBase.ts | 2 +- src/ide_services/services.ts | 4 +- src/panel/codeLens.ts | 2 +- src/toolwrapper/devchat.ts | 2 +- src/util/commonUtil.ts | 146 +++++++++--------- src/util/logger.ts | 1 + src/util/logger_vscode.ts | 4 + src/util/python_installer/conda_url.ts | 4 +- tools | 2 +- 19 files changed, 117 insertions(+), 122 deletions(-) diff --git a/src/context/contextCustomCommand.ts b/src/context/contextCustomCommand.ts index ae7de13..403c165 100644 --- a/src/context/contextCustomCommand.ts +++ b/src/context/contextCustomCommand.ts @@ -20,12 +20,12 @@ export const customCommandContext: ChatContext = { const tempDir = await createTempSubdirectory('devchat/context'); const diffFile = path.join(tempDir, 'custom.txt'); - logger.channel()?.info(`Your custom command is: ${customCommand}`); + logger.channel()?.trace(`Your custom command is: ${customCommand}`); const result = await runCommandStringAndWriteOutput(customCommand, diffFile); - logger.channel()?.info(` exit code:`, result.exitCode); + logger.channel()?.trace(` exit code:`, result.exitCode); - logger.channel()?.debug(` stdout:`, result.stdout); - logger.channel()?.debug(` stderr:`, result.stderr); + logger.channel()?.trace(` stdout:`, result.stdout); + logger.channel()?.trace(` stderr:`, result.stderr); return [`[context|${diffFile}]`]; } return []; diff --git a/src/contributes/codecomplete/ast/treeSitter.ts b/src/contributes/codecomplete/ast/treeSitter.ts index f9d5ed1..dd69b75 100644 --- a/src/contributes/codecomplete/ast/treeSitter.ts +++ b/src/contributes/codecomplete/ast/treeSitter.ts @@ -54,7 +54,7 @@ export async function getParserForFile(filepath: string) { parserCache.set(extension, parser); return parser; } catch (e) { - logger.channel()?.error("Unable to load language for file", filepath, e); + logger.channel()?.warn("Unable to load language for file", filepath, e); return undefined; } } @@ -89,7 +89,7 @@ export async function getLanguageForFile( langCache.set(extension, language); return language; } catch (e) { - logger.channel()?.error("Unable to load language for file:", filepath, e); + logger.channel()?.warn("Unable to load language for file:", filepath, e); return undefined; } } diff --git a/src/contributes/codecomplete/astIndex/index.ts b/src/contributes/codecomplete/astIndex/index.ts index 390960e..0d9bf02 100644 --- a/src/contributes/codecomplete/astIndex/index.ts +++ b/src/contributes/codecomplete/astIndex/index.ts @@ -83,14 +83,14 @@ export async function indexDir(dir: string) { } try { - logger.channel()?.info(`Indexing ${file}`); + logger.channel()?.trace(`Indexing ${file}`); const newFileBlockInfo = await createFileBlockInfo(file); if (newFileBlockInfo) { await indexStore!.add(newFileBlockInfo); indexUpdated = true; } } catch (err) { - logger.channel()?.error(`${err}`); + logger.channel()?.warn(`${err}`); } } @@ -98,7 +98,7 @@ export async function indexDir(dir: string) { try { await indexStore!.save(); } catch (err) { - logger.channel()?.error(`${err}`); + logger.channel()?.warn(`${err}`); } } }; diff --git a/src/contributes/codecomplete/astTest.ts b/src/contributes/codecomplete/astTest.ts index 4d2ce6a..9698002 100644 --- a/src/contributes/codecomplete/astTest.ts +++ b/src/contributes/codecomplete/astTest.ts @@ -30,5 +30,5 @@ export async function outputAst( // output ast const treeText = "\n" + printTree(ast.rootNode, 0); - logger.channel()?.info(treeText); + logger.channel()?.trace(treeText); } diff --git a/src/contributes/codecomplete/codecomplete.ts b/src/contributes/codecomplete/codecomplete.ts index 71adc69..7eec3a9 100644 --- a/src/contributes/codecomplete/codecomplete.ts +++ b/src/contributes/codecomplete/codecomplete.ts @@ -83,9 +83,7 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv try { const response = await fetch(apiUrl, requestOptions); if (!response.ok) { - if (this.devchatConfig.get("complete_debug")) { - logger.channel()?.info("log event to server failed:", response.status); - } + logger.channel()?.info("log event to server failed:", response.status); } } catch (error) { console.error('Error posting event to the server:', error); @@ -130,24 +128,18 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv const fileContent = document.getText(); const posOffset = document.offsetAt(position); - if (completeDebug) { - logger.channel()?.info(`cur position: ${position.line}: ${position.character}`); - } + logger.channel()?.debug(`cur position: ${position.line}: ${position.character}`); const prompt = await createPrompt(fsPath, fileContent, position.line, position.character, posOffset, this.recentEditors.getEdits()); if (!prompt) { return undefined; } - if (completeDebug) { - logger.channel()?.info("prompt:", prompt); - } + logger.channel()?.trace("prompt:", prompt); // check cache const result = await this.cache.get(prompt); if (result) { - if (completeDebug) { - logger.channel()?.info(`cache hited:\n${result.code}`); - } + logger.channel()?.debug(`cache hited:\n${result.code}`); return result; } @@ -240,9 +232,7 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv // TODO // 代码补全建议是否已经被用户看到,这个需要更加准确的方式来识别。 - if (completeDebug) { - logger.channel()?.info("code complete show."); - } + logger.channel()?.trace("code complete show."); this.logEventToServer( { completion_id: response.id, @@ -253,16 +243,12 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv // log to server const logRejectionTimeout: NodeJS.Timeout = setTimeout(() => { - if (completeDebug) { - logger.channel()?.info("code complete not accept."); - } + logger.channel()?.trace("code complete not accept."); }, 10_000); // 代码补全回调处理 const callback = () => { - if (completeDebug) { - logger.channel()?.info("accept:", response!.id); - } + logger.channel()?.trace("accept:", response!.id); // delete cache this.cache.delete(response!.prompt); // delete timer diff --git a/src/contributes/codecomplete/llm.ts b/src/contributes/codecomplete/llm.ts index ab5fa0e..9d18458 100644 --- a/src/contributes/codecomplete/llm.ts +++ b/src/contributes/codecomplete/llm.ts @@ -88,7 +88,7 @@ export async function * nvidiaStarcoderComplete(prompt: string) : AsyncGenerator // data: {"id": "cmpl-1713846153", "created": 1713846160.366049, "object": "completion.chunk", "model": "ollama/starcoder2:7b", "choices": [{"index": 0, "finish_reason": "stop", "text": ""}], "usage": {"prompt_tokens": 413, "completion_tokens": 16}} if (!chunkText.startsWith("data:")) { // log unexpected data - logger.channel()?.info("Unexpected data: " + chunkText); + logger.channel()?.warn("Unexpected data: " + chunkText); return; } @@ -105,7 +105,7 @@ export async function * nvidiaStarcoderComplete(prompt: string) : AsyncGenerator }; } catch (e: any) { logger.channel()?.info("receve:", chunkText); - logger.channel()?.error("JSON Parsing Error:", e.message); + logger.channel()?.warn("JSON Parsing Error:", e.message); } } } @@ -234,7 +234,7 @@ export async function * devchatComplete(prompt: string) : AsyncGenerator= 5000) { - logger.channel()?.error(`symbolMap size is too large, symbol: ${symbol}`); + logger.channel()?.warn(`symbolMap size is too large, symbol: ${symbol}`); continue; } symbolMapUpdated = true; diff --git a/src/extension.ts b/src/extension.ts index 48387ff..f782d0d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -171,7 +171,6 @@ async function activate(context: vscode.ExtensionContext) { registerQuickFixCommand(context); startRpcServer(); - logger.channel()?.info(`registerHandleUri:`); registerHandleUri(context); registerQuickFixProvider(); diff --git a/src/handler/messageHandler.ts b/src/handler/messageHandler.ts index 8effc21..e8332f1 100644 --- a/src/handler/messageHandler.ts +++ b/src/handler/messageHandler.ts @@ -22,11 +22,11 @@ export class MessageHandler { const handler = this.handlers[message.command]; if (handler) { - logger.channel()?.info(`Handling the command "${message.command}"`); + logger.channel()?.trace(`Handling the command "${message.command}"`); await handler(message, panel); - logger.channel()?.info(`Handling the command "${message.command}" done`); + logger.channel()?.trace(`Handling the command "${message.command}" done`); } else { - logger.channel()?.error(`No handler found for the command "${message.command}"`); + logger.channel()?.warn(`No handler found for the command "${message.command}"`); logger.channel()?.show(); } @@ -34,9 +34,8 @@ export class MessageHandler { MessageHandler.sendMessage(panel, { command: 'receiveMessage', text: 'finish', hash: '', user: '', date: 1, isError: false }); } } catch (e) { - logger.channel()?.error(`Error handling the message: "${JSON.stringify(message)}"`); + logger.channel()?.warn(`Error handling the message: "${JSON.stringify(message)}"`); logger.channel()?.show(); - } } @@ -57,7 +56,7 @@ export class MessageHandler { public static sendMessage(panel: vscode.WebviewPanel|vscode.WebviewView, message : any, log: boolean = true): void { if (log) { - logger.channel()?.info(`Sending message: "${JSON.stringify(message)}"`); + logger.channel()?.trace(`Message to GUI: "${JSON.stringify(message)}"`); } panel.webview.postMessage(message); diff --git a/src/handler/sendMessageBase.ts b/src/handler/sendMessageBase.ts index c51479c..d5c9826 100644 --- a/src/handler/sendMessageBase.ts +++ b/src/handler/sendMessageBase.ts @@ -166,7 +166,7 @@ export async function sendMessageBase(message: any, handlePartialData: (data: { // parse context and others from message const [parsedMessage, chatOptions] = await parseMessageAndSetOptions(message); - logger.channel()?.info(`parent hash: ${chatOptions.parent}`); + logger.channel()?.trace(`parent hash: ${chatOptions.parent}`); // call devchat chat const chatResponse = await devChat.chat( diff --git a/src/ide_services/services.ts b/src/ide_services/services.ts index cb39a8e..29cb144 100644 --- a/src/ide_services/services.ts +++ b/src/ide_services/services.ts @@ -91,7 +91,7 @@ let server: http.Server | null = null; export async function startRpcServer() { server = http.createServer((req, res) => { const parsedUrl = new URL(req.url!, `http://${req.headers.host}`); - logger.channel()?.info(`request: ${parsedUrl}`); + logger.channel()?.trace(`request: ${parsedUrl}`); if (parsedUrl.pathname === "/favicon.ico") { res.writeHead(204); res.end(); @@ -166,7 +166,7 @@ export async function startRpcServer() { responseResult["result"] = res; } } catch (error) { - logger.channel()?.warn(`Error: ${error}`); + logger.channel()?.warn(`warning: ${error}`); responseResult["result"] = res; } diff --git a/src/panel/codeLens.ts b/src/panel/codeLens.ts index 86b4076..0c8b7d3 100644 --- a/src/panel/codeLens.ts +++ b/src/panel/codeLens.ts @@ -249,7 +249,7 @@ class FunctionTestCodeLensProvider implements vscode.CodeLensProvider { // log info find how many functionDefinitions, innerFunctionDefinitions, lenses logger .channel() - ?.info( + ?.trace( `found ${functionDefinitions.length} functions, ${innerFunctionDefinitions.length} inner functions, ${lenses.length} registered codeLenses in document: ${document.fileName}` ); return lenses; diff --git a/src/toolwrapper/devchat.ts b/src/toolwrapper/devchat.ts index 84a9c62..6111b68 100644 --- a/src/toolwrapper/devchat.ts +++ b/src/toolwrapper/devchat.ts @@ -498,7 +498,7 @@ class DevChat { logger.channel()?.warn(`${stderr}`); } - logger.channel()?.info(`${stdout}`); + logger.channel()?.trace(`${stdout}`); return stdout; } catch (error: any) { logger.channel()?.error(`Error: ${error.message}`); diff --git a/src/util/commonUtil.ts b/src/util/commonUtil.ts index 31dd265..fb6238e 100644 --- a/src/util/commonUtil.ts +++ b/src/util/commonUtil.ts @@ -60,85 +60,91 @@ export class CommandRun { this.childProcess = null; } - public async spawnAsync(command: string, args: string[], options: object, onData: ((data: string) => void) | undefined, onError: ((data: string) => void) | undefined, onOutputFile: ((command: string, stdout: string, stderr: string) => string) | undefined, outputFile: string | undefined): Promise { - return new Promise((resolve, reject) => { - logger.channel()?.info(`Running command: ${command} ${args.join(' ')}`); - this._input = ""; - const argsNew: string[] = args.map((arg) => { - if (arg.trim()[0] === '$') { - // get rest string except '$' - const restStr = arg.trim().slice(1); - if (process.env[restStr]) { - return process.env[restStr]!; - } else { - return arg; - } - } else { - return arg; - } - }); + public async spawnAsync(command: string, args: string[], options: object, onData: ((data: string) => void) | undefined, onError: ((data: string) => void) | undefined, onOutputFile: ((command: string, stdout: string, stderr: string) => string) | undefined, outputFile: string | undefined): Promise { + return new Promise((resolve, reject) => { + logger.channel()?.debug(`Running command: ${command} ${args.join(' ')}`); + this._input = ""; + const argsNew: string[] = args.map((arg) => { + if (arg.trim()[0] === '$') { + const restStr = arg.trim().slice(1); + if (process.env[restStr]) { + return process.env[restStr]!; + } else { + return arg; + } + } else { + return arg; + } + }); - this.childProcess = spawn(command, argsNew, options); + this.childProcess = spawn(command, argsNew, options); - let stdout = ''; - let stderr = ''; + let stdout = ''; + let stderr = ''; - this.childProcess.stdout.on('data', (data: { toString: () => any; }) => { - const dataStr = this._input + data.toString(); - this._input = ""; - if (onData) { - onData(dataStr); - } - stdout += dataStr; - }); + // Record process start time + const startTime = process.hrtime(); - this.childProcess.stderr.on('data', (data: string) => { - const dataStr = data.toString(); - if (onError) { - onError(dataStr); - } - stderr += dataStr; - }); + this.childProcess.stdout.on('data', (data: { toString: () => any; }) => { + const dataStr = this._input + data.toString(); + this._input = ""; + if (onData) { + onData(dataStr); + } + stdout += dataStr; + }); - this.childProcess.on('close', (code: number) => { - let outputData = stdout; - if (onOutputFile) { - outputData = onOutputFile(command + " " + args.join(" "), stdout, stderr); - } + this.childProcess.stderr.on('data', (data: string) => { + const dataStr = data.toString(); + if (onError) { + onError(dataStr); + } + stderr += dataStr; + }); - if (outputFile) { - fs.writeFileSync(outputFile, outputData); - } + this.childProcess.on('close', (code: number) => { + let outputData = stdout; + if (onOutputFile) { + outputData = onOutputFile(command + " " + args.join(" "), stdout, stderr); + } - if (stderr && !onError) { - logger.channel()?.error(stderr); - logger.channel()?.show(); - } + if (outputFile) { + fs.writeFileSync(outputFile, outputData); + } - this.childProcess = null; - if (code === 0) { - resolve({ exitCode: code, stdout, stderr }); - } else { - resolve({ exitCode: code, stdout, stderr }); - } - }); + if (stderr && !onError) { + logger.channel()?.error(stderr); + logger.channel()?.show(); + } - // Add error event listener to handle command not found exception - this.childProcess.on('error', (error: any) => { - this.childProcess = null; - let errorMessage = error.message; - if (error.code === 'ENOENT') { - errorMessage = `Command not found: ${command}`; - logger.channel()?.error(`Command "${command}" not found`); - logger.channel()?.show(); - } else { - logger.channel()?.error(`Error: ${error.message}`); - logger.channel()?.show(); - } - resolve({ exitCode: error.code, stdout: "", stderr: errorMessage }); - }); - }); - }; + this.childProcess = null; + if (code === 0) { + resolve({ exitCode: code, stdout, stderr }); + } else { + resolve({ exitCode: code, stdout, stderr }); + } + + // Record process end time and calculate duration + const endTime = process.hrtime(startTime); + const duration = endTime[0] + endTime[1] / 1e9; + logger.channel()?.debug(`Process took ${duration} seconds`); + }); + + this.childProcess.on('error', (error: any) => { + this.childProcess = null; + let errorMessage = error.message; + if (error.code === 'ENOENT') { + errorMessage = `Command not found: ${command}`; + logger.channel()?.error(`Command "${command}" not found`); + logger.channel()?.show(); + } else { + logger.channel()?.error(`Error: ${error.message}`); + logger.channel()?.show(); + } + resolve({ exitCode: error.code, stdout: "", stderr: errorMessage }); + }); + }); + }; public write(input: string) { if (this.childProcess) { diff --git a/src/util/logger.ts b/src/util/logger.ts index 3ce444d..2fbb745 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -4,6 +4,7 @@ export interface LogChannel { warn(message: string, ...args: any[]): void; error(message: string | Error, ...args: any[]): void; debug(message: string, ...args: any[]): void; + trace(message: string, ...args: any[]): void; show(): void; } diff --git a/src/util/logger_vscode.ts b/src/util/logger_vscode.ts index caca8f9..53d49dc 100644 --- a/src/util/logger_vscode.ts +++ b/src/util/logger_vscode.ts @@ -33,6 +33,10 @@ export class LoggerChannelVscode implements LogChannel { this._channel.debug(message, ...args); } + trace(message: string, ...args: any[]): void { + this._channel.trace(message, ...args); + } + show(): void { this._channel.show(); } diff --git a/src/util/python_installer/conda_url.ts b/src/util/python_installer/conda_url.ts index e0f77bf..a3d3fa8 100644 --- a/src/util/python_installer/conda_url.ts +++ b/src/util/python_installer/conda_url.ts @@ -11,7 +11,7 @@ import path from 'path'; function getDownloadFileName(): string { const platform = os.platform(); const arch = os.arch(); - logger.channel()?.info(`Platform: ${platform}, Arch: ${arch}`); + logger.channel()?.debug(`Platform: ${platform}, Arch: ${arch}`); if (platform === "win32") { if (arch === "x64") { @@ -55,7 +55,7 @@ import path from 'path'; export function getMicromambaUrl(): string { const platform = os.platform(); const arch = os.arch(); - logger.channel()?.info(`Platform: ${platform}, Arch: ${arch}`); + logger.channel()?.debug(`Platform: ${platform}, Arch: ${arch}`); let micromambaUrl = ''; if (platform === "win32") { diff --git a/tools b/tools index 501b7ff..66da07c 160000 --- a/tools +++ b/tools @@ -1 +1 @@ -Subproject commit 501b7ff4aee1656e543f23a3e5f88210f1eaaa7c +Subproject commit 66da07cc2536a05ba3641122dfe62f1fa009cfb9