Skip to content

Commit

Permalink
src/debugAdapter: accept additional trace levels
Browse files Browse the repository at this point in the history
The purpose of this PR is to match the allowed 'trace' attribute values
acceptable by the legacy and the new dlv-dap modes. The old debug
adapter simply maps 'trace' to 'verbose', and 'info' and 'warn' to
'log'.

Add add 'warn' level support to `src/goLogging.ts`.

Updated dlv-dap.md

Change-Id: I0a5836e255a6b576258879e84d63b862b397f1e3
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/311809
Trust: Hyang-Ah Hana Kim <[email protected]>
Run-TryBot: Hyang-Ah Hana Kim <[email protected]>
TryBot-Result: kokoro <[email protected]>
Reviewed-by: Suzy Mueller <[email protected]>
  • Loading branch information
hyangah committed Apr 21, 2021
1 parent 5639184 commit ec08530
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
15 changes: 14 additions & 1 deletion docs/dlv-dap.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ For simple launch cases, build the delve binary, and configure `"go.alternateToo
}
```

Set `logOutput` and `showLog` attributes in `launch.json` to enable logging and DAP message tracing.
Set `logOutput` and `showLog` attributes in `launch.json` to enable `dlv'-side logging and DAP message tracing.
```json5
{
"name": "Launch file",
Expand All @@ -118,6 +118,19 @@ Set `logOutput` and `showLog` attributes in `launch.json` to enable logging and
}
```

Set `trace` attribute to control the verbosity of debug extension's logging.
The logging will appear in the `Go Debug` output channel (Command Palette -> "View: Toggle Output" -> Select "Go Debug" from the dropdown menu).

```json5
{
"name": "Launch file",
"type": "go",
"debugAdapter": "dlv-dap",
"trace": "verbose",
...
}
```

If you are having issues with seeing logs and or suspect problems in extension's integration, you can start Delve DAP server from a separate terminal and configure the extension to directly connect to it.

```
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,15 @@
"trace": {
"type": "string",
"enum": [
"log",
"verbose",
"trace",
"log",
"info",
"warn",
"error"
],
"default": "error",
"description": "Various levels of logging shown in the debug console. When set to 'log' or 'verbose', the logs will also be written to a file."
"description": "Various levels of the debug console & 'Go Debug' output channel. When using the `legacy` debug adapter, the logs will also be written to a file if it is set to a value other than `error`."
},
"envFile": {
"type": [
Expand Down
13 changes: 9 additions & 4 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,12 @@ interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArguments {
host?: string;
buildFlags?: string;
init?: string;
trace?: 'verbose' | 'log' | 'error';
// trace, info, warn are to match goLogging.
// In practice, this adapter handles only verbose, log, and error
// verbose === trace,
// log === info === warn,
// error
trace?: 'verbose' | 'trace' | 'info' | 'log' | 'warn' | 'error';
backend?: string;
output?: string;
substitutePath?: { from: string; to: string }[];
Expand Down Expand Up @@ -315,7 +320,7 @@ interface AttachRequestArguments extends DebugProtocol.AttachRequestArguments {
remotePath?: string;
port?: number;
host?: string;
trace?: 'verbose' | 'log' | 'error';
trace?: 'verbose' | 'trace' | 'info' | 'log' | 'warn' | 'error';
backend?: string;
substitutePath?: { from: string; to: string }[];
/** Delve LoadConfig parameters */
Expand Down Expand Up @@ -1954,9 +1959,9 @@ export class GoDebugSession extends LoggingDebugSession {
args: LaunchRequestArguments | AttachRequestArguments
) {
this.logLevel =
args.trace === 'verbose'
args.trace === 'verbose' || args.trace === 'trace'
? Logger.LogLevel.Verbose
: args.trace === 'log'
: args.trace === 'log' || args.trace === 'info' || args.trace === 'warn'
? Logger.LogLevel.Log
: Logger.LogLevel.Error;
const logPath =
Expand Down
31 changes: 22 additions & 9 deletions src/goLogging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@

'use strict';

type LogLevel = 'off' | 'error' | 'info' | 'trace' | 'verbose';
type LogLevel = 'off' | 'error' | 'warn' | 'info' | 'trace' | 'verbose';

const levels: { [key in LogLevel]: number } = {
off: -1,
error: 0,
info: 1,
trace: 2,
verbose: 3
warn: 1,
info: 2,
trace: 3,
verbose: 4
};
// TODO: consider 'warning' level.

function levelToString(level: number) {
switch (level) {
case levels.error:
return 'Error';
case levels.warn:
return 'Warn';
case levels.info:
return 'Info';
case levels.trace:
Expand Down Expand Up @@ -55,6 +57,9 @@ export class Logger {
error(msg: string) {
this.log(levels.error, msg);
}
warn(msg: string) {
this.log(levels.warn, msg);
}
info(msg: string) {
this.log(levels.info, msg);
}
Expand Down Expand Up @@ -91,14 +96,22 @@ export function setLogConfig(cfg: LogConfig) {
defaultLogger = new Logger(cfg.level);
}

export function logVerbose(msg: string) {
defaultLogger?.debug(msg);
}

export function logError(msg: string) {
defaultLogger?.error(msg);
}

export function logWarn(msg: string) {
defaultLogger?.warn(msg);
}

export function logInfo(msg: string) {
defaultLogger?.info(msg);
}

export function logTrace(msg: string) {
defaultLogger?.trace(msg);
}

export function logVerbose(msg: string) {
defaultLogger?.debug(msg);
}
8 changes: 5 additions & 3 deletions test/unit/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ suite('Logger Tests', () => {
const appendLine = sandbox.fake();
const logger = new Logger(level, { appendLine });
logger.error('error');
logger.warn('warn');
logger.info('info');
logger.debug('debug');
logger.trace('trace');
assert.strictEqual(appendLine.callCount, want, `called ${appendLine.callCount} times, want ${want}`);
}
test('logger level = off', () => runTest('off', 0));
test('logger level = error', () => runTest('error', 1));
test('logger level = info', () => runTest('info', 2));
test('logger level = trace', () => runTest('trace', 3));
test('logger level = verbose', () => runTest('verbose', 4));
test('logger level = warning', () => runTest('warn', 2));
test('logger level = info', () => runTest('info', 3));
test('logger level = trace', () => runTest('trace', 4));
test('logger level = verbose', () => runTest('verbose', 5));
test('logger level = undefined', () => runTest(undefined, 1));
test('logger level = ""', () => runTest('', 1));
test('logger level = object', () => runTest({}, 1));
Expand Down

0 comments on commit ec08530

Please sign in to comment.