Skip to content

Commit

Permalink
src/goDebugFactory: add GoDebugAdapterTrackerFactory
Browse files Browse the repository at this point in the history
And create a `Go Debug` output channel that can be used
to output all logging and traces from debug extension
activity.

Logging level of individual debug session is controlled
by launch configuration's `trace` attribute.

Manually tested.

Update #1410

Change-Id: Ib87d014ec6114581af78bdcdd74b190b14552ce6
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/310750
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
  • Loading branch information
hyangah committed Apr 19, 2021
1 parent 8755d84 commit cfc787e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/goDebugFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import path = require('path');
import * as fs from 'fs';
import * as net from 'net';
import { getTool } from './goTools';
import { TimestampedLogger } from './goLogging';

export class GoDebugAdapterDescriptorFactory implements vscode.DebugAdapterDescriptorFactory {
public createDebugAdapterDescriptor(
Expand Down Expand Up @@ -41,8 +42,27 @@ export class GoDebugAdapterDescriptorFactory implements vscode.DebugAdapterDescr
}
}

// TODO(hyangah): Code below needs refactoring to avoid using vscode API
// so we can use from a separate debug adapter executable in testing.
export class GoDebugAdapterTrackerFactory implements vscode.DebugAdapterTrackerFactory {
constructor(private outputChannel: vscode.OutputChannel) {}

createDebugAdapterTracker(session: vscode.DebugSession) {
const level = session.configuration?.trace;
if (!level || level === 'off') {
return null;
}
const logger = new TimestampedLogger(session.configuration?.trace || 'off', this.outputChannel);
return {
onWillStartSession: () =>
logger.debug(`session ${session.id} will start with ${JSON.stringify(session.configuration)}\n`),
onWillReceiveMessage: (message: any) => logger.trace(`client -> ${JSON.stringify(message)}\n`),
onDidSendMessage: (message: any) => logger.trace(`client <- ${JSON.stringify(message)}\n`),
onError: (error: Error) => logger.error(`error: ${error}\n`),
onWillStopSession: () => logger.debug(`session ${session.id} will stop\n`),
onExit: (code: number | undefined, signal: string | undefined) =>
logger.info(`debug adapter exited: (code: ${code}, signal: ${signal})\n`)
};
}
}

const TWO_CRLF = '\r\n\r\n';

Expand Down
10 changes: 9 additions & 1 deletion src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
updateCodeCoverageDecorators
} from './goCover';
import { GoDebugConfigurationProvider } from './goDebugConfiguration';
import { GoDebugAdapterDescriptorFactory } from './goDebugFactory';
import { GoDebugAdapterDescriptorFactory, GoDebugAdapterTrackerFactory } from './goDebugFactory';
import { extractFunction, extractVariable } from './goDoctor';
import { toolExecutionEnvironment } from './goEnv';
import {
Expand Down Expand Up @@ -240,6 +240,14 @@ If you would like additional configuration for diagnostics from gopls, please se
ctx.subscriptions.push(factory);
}

const debugOutputChannel = vscode.window.createOutputChannel('Go Debug');
ctx.subscriptions.push(debugOutputChannel);
const tracker = new GoDebugAdapterTrackerFactory(debugOutputChannel);
ctx.subscriptions.push(vscode.debug.registerDebugAdapterTrackerFactory('go', tracker));
if ('dispose' in tracker) {
ctx.subscriptions.push(tracker);
}

buildDiagnosticCollection = vscode.languages.createDiagnosticCollection('go');
ctx.subscriptions.push(buildDiagnosticCollection);
lintDiagnosticCollection = vscode.languages.createDiagnosticCollection(
Expand Down

0 comments on commit cfc787e

Please sign in to comment.