From c3c6fc63be16f88262428f4da56651ba2629ed0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20R=C3=B8rvik?= Date: Wed, 22 Jan 2025 22:38:19 +0100 Subject: [PATCH] Add debug console output monitoring (#1400) * feat: add debug console output monitoring * Fix formatting --------- Co-authored-by: minimali Co-authored-by: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> --- src/integrations/debug/DebugConsoleManager.ts | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/integrations/debug/DebugConsoleManager.ts diff --git a/src/integrations/debug/DebugConsoleManager.ts b/src/integrations/debug/DebugConsoleManager.ts new file mode 100644 index 0000000000..4ec7bdf65a --- /dev/null +++ b/src/integrations/debug/DebugConsoleManager.ts @@ -0,0 +1,73 @@ +import * as vscode from "vscode" + +interface DebugSession { + id: string + name: string + output: string[] + lastRetrievedIndex: number +} + +export class DebugConsoleManager { + private sessions: Map = new Map() + private disposables: vscode.Disposable[] = [] + + constructor() { + // Listen for debug session start events + this.disposables.push( + vscode.debug.onDidStartDebugSession((session) => { + this.sessions.set(session.id, { + id: session.id, + name: session.name, + output: [], + lastRetrievedIndex: -1, + }) + }), + ) + + // Listen for debug session end events + this.disposables.push( + vscode.debug.onDidTerminateDebugSession((session) => { + this.sessions.delete(session.id) + }), + ) + + // Listen for debug console output + this.disposables.push( + vscode.debug.onDidReceiveDebugSessionCustomEvent((e: vscode.DebugSessionCustomEvent) => { + if (e.event === "output" && e.body?.output) { + const session = this.sessions.get(e.session.id) + if (session) { + session.output.push(e.body.output) + } + } + }), + ) + } + + /** + * Get all active debug sessions + */ + getActiveSessions(): { id: string; name: string }[] { + return Array.from(this.sessions.values()).map(({ id, name }) => ({ id, name })) + } + + /** + * Get any new output since the last retrieval for a specific debug session + */ + getUnretrievedOutput(sessionId: string): string | undefined { + const session = this.sessions.get(sessionId) + if (!session) return undefined + + const newOutput = session.output.slice(session.lastRetrievedIndex + 1).join("") + session.lastRetrievedIndex = session.output.length - 1 + return newOutput || undefined + } + + /** + * Clean up resources + */ + dispose() { + this.disposables.forEach((d) => d.dispose()) + this.sessions.clear() + } +}