Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Write copilot debug log to file #13077

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export function logToDebugConsole(logLevel: LogLevel, message: string): void {
} catch (e) {}
}

export async function writeCopilotLogToFile(log: string, filePath: string): Promise<void> {
export function writeExecutionDetailsToFile(logFilePath: string, logString: string): void {
const fs = require("fs");
await fs.appendFile(filePath, log + "\n");
fs.appendFileSync(logFilePath, logString + "\n");
}

export class CopilotDebugLog {
Expand Down Expand Up @@ -131,12 +131,6 @@ export class CopilotDebugLog {
}
}

private logNoMatchedFunctions(debugConsole: vscode.DebugConsole): void {
debugConsole.appendLine(
`${ANSIColors.RED} (×) Error: ${ANSIColors.WHITE}Matched functions: None`
);
}

private logNoPlugins(debugConsole: vscode.DebugConsole): void {
debugConsole.appendLine(`${ANSIColors.RED}(×) Error: ${ANSIColors.WHITE}Enabled plugin: None`);
}
Expand All @@ -161,9 +155,11 @@ export class CopilotDebugLog {
debugConsole: vscode.DebugConsole,
matchedFunction: FunctionDescriptor
): void {
const logFileName = `Copilot log ${new Date().toISOString().replace(/-|:|\.\d+Z$/g, "")}.txt`;
// E.g "Copilot-debug-20250113T070957.txt"
const logFileName = `Copilot-debug-${new Date().toISOString().replace(/-|:|\.\d+Z$/g, "")}.txt`;
const logFilePath = `${defaultExtensionLogPath}/${logFileName}`;
if (this.functionExecutions && this.functionExecutions.length > 0) {
writeExecutionDetailsToFile(logFilePath, JSON.stringify(this.functionExecutions, null, 2));
this.functionExecutions.forEach((functionExecution) => {
if (
functionExecution.function.functionDisplayName === matchedFunction.functionDisplayName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import * as sinon from "sinon";
import * as vscode from "vscode";
import { ANSIColors } from "../../src/debug/common/debugConstants";
import * as globalVariables from "../../src/globalVariables";
import { CopilotDebugLog, logToDebugConsole } from "../../src/pluginDebugger/copilotDebugLogOutput";
import {
CopilotDebugLog,
logToDebugConsole,
writeExecutionDetailsToFile,
} from "../../src/pluginDebugger/copilotDebugLogOutput";

describe("copilotDebugLogOutput", () => {
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -71,14 +75,14 @@ describe("copilotDebugLogOutput", () => {
});
});

// describe("writeCopilotLogToFile", () => {
// it("should write log to file", async () => {
// const fs = require("fs");
// const appendFileStub = sandbox.stub(fs, "appendFile").resolves();
// await writeCopilotLogToFile("log message", "path/to/log.txt");
// assert.isTrue(appendFileStub.calledWith("path/to/log.txt", "log message\n"));
// });
// });
describe("writeExecutionDetailsToFile", () => {
it("should write function execution details to file", async () => {
const fs = require("fs");
const appendFileStub = sandbox.stub(fs, "appendFileSync").resolves();
writeExecutionDetailsToFile("path/to/log.txt", "log message");
chai.assert.isTrue(appendFileStub.calledWith("path/to/log.txt", "log message\n"));
});
});

describe("CopilotDebugLog", () => {
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -173,6 +177,34 @@ describe("copilotDebugLogOutput", () => {
);
});

it("write with 0 enabled plugin(s)", () => {
const logJson = JSON.stringify({
enabledPlugins: [],
matchedFunctionCandidates: [],
functionsSelectedForInvocation: [],
functionExecutions: [],
});

const copilotDebugLog = new CopilotDebugLog(logJson);
const appendLineStub = sandbox.stub(vscode.debug.activeDebugConsole, "appendLine");
copilotDebugLog.write();

chai.assert.isTrue(appendLineStub.calledWith(""));
chai.assert.isTrue(
appendLineStub.calledWith(
`${ANSIColors.WHITE}[${new Date().toJSON()}] - ${ANSIColors.BLUE}0 enabled plugin(s).`
)
);
chai.assert.isTrue(
appendLineStub.calledWith(`${ANSIColors.WHITE}Copilot plugin developer info:`)
);
chai.assert.isTrue(
appendLineStub.calledWith(
`${ANSIColors.RED}(×) Error: ${ANSIColors.WHITE}Enabled plugin: None`
)
);
});

it("write with plugins enabled", () => {
const logJson = JSON.stringify({
enabledPlugins: [{ name: "plugin1", id: "1", version: "1.0" }],
Expand Down Expand Up @@ -204,48 +236,38 @@ describe("copilotDebugLogOutput", () => {
},
],
});
const logFilePath = `/path/to/log/Copilot log ${"test".replace(/-|:|\.\d+Z$/g, "")}.txt`;
const logFilePath = `/path/to/log/Copilot-debug-test.txt`;
const responseStatus = 200;
const fs = require("fs");
const appendFileSyncStub = sandbox.stub(fs, "appendFileSync").resolves();
sandbox.stub(globalVariables, "defaultExtensionLogPath").value("/path/to/log");
sandbox.stub(Date.prototype, "toISOString").returns("test");
const copilotDebugLog = new CopilotDebugLog(logJson);
const appendLineStub = sandbox.stub(vscode.debug.activeDebugConsole, "appendLine");
copilotDebugLog.write();
chai.assert.isTrue(
appendLineStub.calledWith(
`${ANSIColors.GREEN} (√) ${ANSIColors.WHITE}Function execution details: ${ANSIColors.GREEN}Status 200, ${ANSIColors.WHITE}refer to ${ANSIColors.BLUE}${logFilePath}${ANSIColors.WHITE} for all details.`
`${ANSIColors.GREEN}(√) ${ANSIColors.WHITE}Enabled plugin: ${ANSIColors.MAGENTA}plugin1 ${ANSIColors.GRAY}• version 1.0 • 1`
)
);
chai.assert.isTrue(
appendLineStub.calledWith(
`${ANSIColors.RED} (×) Error: ${ANSIColors.WHITE}Sample error`
`${ANSIColors.GREEN} (√) ${ANSIColors.WHITE}Matched functions: ${ANSIColors.MAGENTA}function1`
)
);
});

it("0 enabled plugin(s)", () => {
const logJson = JSON.stringify({
enabledPlugins: [],
matchedFunctionCandidates: [],
functionsSelectedForInvocation: [],
functionExecutions: [],
});

const copilotDebugLog = new CopilotDebugLog(logJson);
const appendLineStub = sandbox.stub(vscode.debug.activeDebugConsole, "appendLine");
copilotDebugLog.write();

chai.assert.isTrue(appendLineStub.calledWith(""));
chai.assert.isTrue(
appendLineStub.calledWith(
`${ANSIColors.WHITE}[${new Date().toJSON()}] - ${ANSIColors.BLUE}0 enabled plugin(s).`
`${ANSIColors.GREEN} (√) ${ANSIColors.WHITE}Selected functions for execution: ${ANSIColors.MAGENTA}function1`
)
);
chai.assert.isTrue(
appendLineStub.calledWith(`${ANSIColors.WHITE}Copilot plugin developer info:`)
appendLineStub.calledWith(
`${ANSIColors.GREEN} (√) ${ANSIColors.WHITE}Function execution details: ${ANSIColors.GREEN}Status ${responseStatus}, ${ANSIColors.WHITE}refer to ${ANSIColors.BLUE}${logFilePath}${ANSIColors.WHITE} for all details.`
)
);
chai.assert.isTrue(
appendLineStub.calledWith(
`${ANSIColors.RED}(×) Error: ${ANSIColors.WHITE}Enabled plugin: None`
`${ANSIColors.RED} (×) Error: ${ANSIColors.WHITE}Sample error`
)
);
});
Expand Down
Loading