From 6e4e8428ee14137d82fb9aa4d5c03ab18ea49039 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 28 Oct 2019 01:49:34 -0700 Subject: [PATCH 1/8] Add debug session logging via tracker --- .../debugger/extension/adapter/activator.ts | 10 +-- .../debugger/extension/adapter/logging.ts | 67 +++++++++++++++++++ .../debugger/extension/serviceRegistry.ts | 4 +- src/client/debugger/extension/types.ts | 6 +- .../extension/adapter/activator.unit.test.ts | 20 ++++-- 5 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 src/client/debugger/extension/adapter/logging.ts diff --git a/src/client/debugger/extension/adapter/activator.ts b/src/client/debugger/extension/adapter/activator.ts index 2416d3bb2892..60dc77b7f796 100644 --- a/src/client/debugger/extension/adapter/activator.ts +++ b/src/client/debugger/extension/adapter/activator.ts @@ -9,19 +9,21 @@ import { IDebugService } from '../../../common/application/types'; import { DebugAdapterDescriptorFactory } from '../../../common/experimentGroups'; import { IDisposableRegistry, IExperimentsManager } from '../../../common/types'; import { DebuggerTypeName } from '../../constants'; -import { IDebugAdapterDescriptorFactory } from '../types'; +import { IDebugAdapterDescriptorFactory, IDebugSessionLoggingFactory } from '../types'; @injectable() export class DebugAdapterActivator implements IExtensionSingleActivationService { constructor( @inject(IDebugService) private readonly debugService: IDebugService, - @inject(IDebugAdapterDescriptorFactory) private factory: IDebugAdapterDescriptorFactory, + @inject(IDebugAdapterDescriptorFactory) private descriptorFactory: IDebugAdapterDescriptorFactory, + @inject(IDebugSessionLoggingFactory) private debugSessionLoggingFactory: IDebugSessionLoggingFactory, @inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry, @inject(IExperimentsManager) private readonly experimentsManager: IExperimentsManager - ) {} + ) { } public async activate(): Promise { if (this.experimentsManager.inExperiment(DebugAdapterDescriptorFactory.experiment)) { - this.disposables.push(this.debugService.registerDebugAdapterDescriptorFactory(DebuggerTypeName, this.factory)); + this.disposables.push(this.debugService.registerDebugAdapterTrackerFactory(DebuggerTypeName, this.debugSessionLoggingFactory)); + this.disposables.push(this.debugService.registerDebugAdapterDescriptorFactory(DebuggerTypeName, this.descriptorFactory)); } else { this.experimentsManager.sendTelemetryIfInExperiment(DebugAdapterDescriptorFactory.control); } diff --git a/src/client/debugger/extension/adapter/logging.ts b/src/client/debugger/extension/adapter/logging.ts new file mode 100644 index 000000000000..3b151b0a272b --- /dev/null +++ b/src/client/debugger/extension/adapter/logging.ts @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +'use strict'; +import * as fs from 'fs'; +import { inject, injectable } from 'inversify'; +import * as path from 'path'; +import { DebugAdapterTracker, DebugAdapterTrackerFactory, DebugSession, ProviderResult } from 'vscode'; +import { DebugProtocol } from 'vscode-debugprotocol'; + +import { IFileSystem } from '../../../common/platform/types'; +import { EXTENSION_ROOT_DIR } from '../../../constants'; + +class DebugSessionLoggingTracker implements DebugAdapterTracker { + private readonly session: DebugSession; + private readonly enabled: boolean = false; + private stream: fs.WriteStream; + constructor(session: DebugSession, fileSystem: IFileSystem) { + this.session = session; + this.enabled = this.session.configuration.logToFile as boolean; + const fileName = `debug_ext_${this.session.id}.log`; + this.stream = fileSystem.createWriteStream(path.join(EXTENSION_ROOT_DIR, fileName)); + } + + public onWillStartSession() { + if (this.enabled) { + this.stream.write(`Starting debug session with configuration: ${this.session.configuration}`); + // write start + } + } + public onWillReceiveMessage(_message: DebugProtocol.Message) { + if (this.enabled) { + // write received message with time stamp + } + } + public onDidSendMessage(_message: DebugProtocol.Message) { + if (this.enabled) { + // write sent message with time stamp + } + } + public onWillStopSession() { + if (this.enabled) { + // write stopped session details with message + } + } + public onError(_error: Error) { + if (this.enabled) { + // write any error details + } + } + public onExit(_code: number | undefined, _signal: string | undefined) { + if (this.enabled) { + // write any exit details + } + } +} + +@injectable() +export class DebugSessionLoggingFactory implements DebugAdapterTrackerFactory { + constructor( + @inject(IFileSystem) private readonly fileSystem: IFileSystem + ) { + } + + public createDebugAdapterTracker(session: DebugSession): ProviderResult { + return new DebugSessionLoggingTracker(session, this.fileSystem); + } +} diff --git a/src/client/debugger/extension/serviceRegistry.ts b/src/client/debugger/extension/serviceRegistry.ts index b3facb549d05..418e4bb405a6 100644 --- a/src/client/debugger/extension/serviceRegistry.ts +++ b/src/client/debugger/extension/serviceRegistry.ts @@ -8,6 +8,7 @@ import { IServiceManager } from '../../ioc/types'; import { AttachRequestArguments, LaunchRequestArguments } from '../types'; import { DebugAdapterActivator } from './adapter/activator'; import { DebugAdapterDescriptorFactory } from './adapter/factory'; +import { DebugSessionLoggingFactory } from './adapter/logging'; import { DebuggerBanner } from './banner'; import { PythonDebugConfigurationService } from './configuration/debugConfigurationService'; import { LaunchJsonCompletionProvider } from './configuration/launch.json/completionProvider'; @@ -26,7 +27,7 @@ import { IDebugConfigurationProviderFactory, IDebugConfigurationResolver } from import { ChildProcessAttachEventHandler } from './hooks/childProcessAttachHandler'; import { ChildProcessAttachService } from './hooks/childProcessAttachService'; import { IChildProcessAttachService, IDebugSessionEventHandlers } from './hooks/types'; -import { DebugConfigurationType, IDebugAdapterDescriptorFactory, IDebugConfigurationProvider, IDebugConfigurationService, IDebuggerBanner } from './types'; +import { DebugConfigurationType, IDebugAdapterDescriptorFactory, IDebugConfigurationProvider, IDebugConfigurationService, IDebuggerBanner, IDebugSessionLoggingFactory } from './types'; export function registerTypes(serviceManager: IServiceManager) { serviceManager.addSingleton(IExtensionSingleActivationService, LaunchJsonCompletionProvider); @@ -47,4 +48,5 @@ export function registerTypes(serviceManager: IServiceManager) { serviceManager.addSingleton(IDebugEnvironmentVariablesService, DebugEnvironmentVariablesHelper); serviceManager.addSingleton(IExtensionSingleActivationService, DebugAdapterActivator); serviceManager.addSingleton(IDebugAdapterDescriptorFactory, DebugAdapterDescriptorFactory); + serviceManager.addSingleton(IDebugSessionLoggingFactory, DebugSessionLoggingFactory); } diff --git a/src/client/debugger/extension/types.ts b/src/client/debugger/extension/types.ts index 1accd67965a6..f2f239d70ad1 100644 --- a/src/client/debugger/extension/types.ts +++ b/src/client/debugger/extension/types.ts @@ -3,7 +3,7 @@ 'use strict'; -import { CancellationToken, DebugAdapterDescriptorFactory, DebugConfigurationProvider, WorkspaceFolder } from 'vscode'; +import { CancellationToken, DebugAdapterDescriptorFactory, DebugAdapterTrackerFactory, DebugConfigurationProvider, WorkspaceFolder } from 'vscode'; import { InputStep, MultiStepInput } from '../../common/utils/multiStepInput'; import { RemoteDebugOptions } from '../debugAdapter/types'; import { DebugConfigurationArguments } from '../types'; @@ -44,3 +44,7 @@ export interface IDebugAdapterDescriptorFactory extends DebugAdapterDescriptorFa } export type DebugAdapterPtvsdPathInfo = { extensionVersion: string; ptvsdPath: string }; + +export const IDebugSessionLoggingFactory = Symbol('IDebugSessionLoggingFactory'); + +export interface IDebugSessionLoggingFactory extends DebugAdapterTrackerFactory { } diff --git a/src/test/debugger/extension/adapter/activator.unit.test.ts b/src/test/debugger/extension/adapter/activator.unit.test.ts index 1f83e54ddc80..52e52fb661a2 100644 --- a/src/test/debugger/extension/adapter/activator.unit.test.ts +++ b/src/test/debugger/extension/adapter/activator.unit.test.ts @@ -22,7 +22,8 @@ import { FileSystem } from '../../../../client/common/platform/fileSystem'; import { IDisposableRegistry, IPythonSettings } from '../../../../client/common/types'; import { DebugAdapterActivator } from '../../../../client/debugger/extension/adapter/activator'; import { DebugAdapterDescriptorFactory } from '../../../../client/debugger/extension/adapter/factory'; -import { IDebugAdapterDescriptorFactory } from '../../../../client/debugger/extension/types'; +import { DebugSessionLoggingFactory } from '../../../../client/debugger/extension/adapter/logging'; +import { IDebugAdapterDescriptorFactory, IDebugSessionLoggingFactory } from '../../../../client/debugger/extension/types'; import { clearTelemetryReporter } from '../../../../client/telemetry'; import { EventName } from '../../../../client/telemetry/constants'; import { noop } from '../../../core'; @@ -34,7 +35,8 @@ suite('Debugging - Adapter Factory Registration', () => { const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST; let activator: IExtensionSingleActivationService; let debugService: IDebugService; - let factory: IDebugAdapterDescriptorFactory; + let descriptorFactory: IDebugAdapterDescriptorFactory; + let loggingFactory: IDebugSessionLoggingFactory; let disposableRegistry: IDisposableRegistry; let experimentsManager: ExperimentsManager; let spiedInstance: ExperimentsManager; @@ -80,9 +82,10 @@ suite('Debugging - Adapter Factory Registration', () => { spiedInstance = spy(experimentsManager); debugService = mock(DebugService); - factory = mock(DebugAdapterDescriptorFactory); + descriptorFactory = mock(DebugAdapterDescriptorFactory); + loggingFactory = mock(DebugSessionLoggingFactory); disposableRegistry = []; - activator = new DebugAdapterActivator(instance(debugService), instance(factory), disposableRegistry, experimentsManager); + activator = new DebugAdapterActivator(instance(debugService), instance(descriptorFactory), instance(loggingFactory), disposableRegistry, experimentsManager); }); teardown(() => { @@ -100,17 +103,19 @@ suite('Debugging - Adapter Factory Registration', () => { await activator.activate(); - verify(debugService.registerDebugAdapterDescriptorFactory('python', instance(factory))).once(); + verify(debugService.registerDebugAdapterTrackerFactory('python', instance(loggingFactory))).once(); + verify(debugService.registerDebugAdapterDescriptorFactory('python', instance(descriptorFactory))).once(); }); test('Register a disposable item if inside the DA experiment', async () => { when(spiedInstance.inExperiment(DebugAdapterExperiment.experiment)).thenReturn(true); const disposable = { dispose: noop }; + when(debugService.registerDebugAdapterTrackerFactory(anything(), anything())).thenReturn(disposable); when(debugService.registerDebugAdapterDescriptorFactory(anything(), anything())).thenReturn(disposable); await activator.activate(); - assert.deepEqual(disposableRegistry, [disposable]); + assert.deepEqual(disposableRegistry, [disposable, disposable]); }); test('Send experiment group telemetry if inside the DA experiment', async () => { @@ -127,7 +132,8 @@ suite('Debugging - Adapter Factory Registration', () => { await activator.activate(); - verify(debugService.registerDebugAdapterDescriptorFactory('python', instance(factory))).never(); + verify(debugService.registerDebugAdapterTrackerFactory('python', instance(loggingFactory))).never(); + verify(debugService.registerDebugAdapterDescriptorFactory('python', instance(descriptorFactory))).never(); }); test('Don\'t register a disposable item if not inside the DA experiment', async () => { From a63c78be45b0ba3f491c439d7edc90b328a5b20d Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 28 Oct 2019 12:43:02 -0700 Subject: [PATCH 2/8] Add log messages --- .../debugger/extension/adapter/logging.ts | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/client/debugger/extension/adapter/logging.ts b/src/client/debugger/extension/adapter/logging.ts index 3b151b0a272b..0559533f8a37 100644 --- a/src/client/debugger/extension/adapter/logging.ts +++ b/src/client/debugger/extension/adapter/logging.ts @@ -1,10 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. 'use strict'; + import * as fs from 'fs'; import { inject, injectable } from 'inversify'; import * as path from 'path'; -import { DebugAdapterTracker, DebugAdapterTrackerFactory, DebugSession, ProviderResult } from 'vscode'; +import { DebugAdapterTracker, DebugAdapterTrackerFactory, DebugConfiguration, DebugSession, ProviderResult } from 'vscode'; import { DebugProtocol } from 'vscode-debugprotocol'; import { IFileSystem } from '../../../common/platform/types'; @@ -22,36 +23,38 @@ class DebugSessionLoggingTracker implements DebugAdapterTracker { } public onWillStartSession() { - if (this.enabled) { - this.stream.write(`Starting debug session with configuration: ${this.session.configuration}`); - // write start - } + this.log(`Stoping Session:\n${this.stringify(this.session.configuration)}\n`); } - public onWillReceiveMessage(_message: DebugProtocol.Message) { - if (this.enabled) { - // write received message with time stamp - } + + public onWillReceiveMessage(message: DebugProtocol.Message) { + this.log(`Client <-- Adapter:\n${this.stringify(message)}\n`); } - public onDidSendMessage(_message: DebugProtocol.Message) { - if (this.enabled) { - // write sent message with time stamp - } + + public onDidSendMessage(message: DebugProtocol.Message) { + this.log(`Client --> Adapter:\n${this.stringify(message)}\n`); } + public onWillStopSession() { - if (this.enabled) { - // write stopped session details with message - } + this.log(`Stopping Session`); } - public onError(_error: Error) { - if (this.enabled) { - // write any error details - } + + public onError(error: Error) { + this.log(`Error:\n${this.stringify(error)}\n`); } - public onExit(_code: number | undefined, _signal: string | undefined) { + + public onExit(code: number | undefined, signal: string | undefined) { + this.log(`Exit:\nExit-Code: ${code ? code : 0}\nSignal: ${signal ? signal : 'none'}`); + } + + private log(message: string) { if (this.enabled) { - // write any exit details + this.stream.write(`${Math.floor(Date.now())} ${message}`); } } + + private stringify(data: DebugProtocol.Message | Error | DebugConfiguration) { + return JSON.stringify(data, null, 4); + } } @injectable() From 3372ea6cbc8d5025ecfc3fa21893708881431003 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 28 Oct 2019 15:43:30 -0700 Subject: [PATCH 3/8] Ensure we only log as needed. --- .../debugger/extension/adapter/logging.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/client/debugger/extension/adapter/logging.ts b/src/client/debugger/extension/adapter/logging.ts index 0559533f8a37..f9d8ee41bbd5 100644 --- a/src/client/debugger/extension/adapter/logging.ts +++ b/src/client/debugger/extension/adapter/logging.ts @@ -9,20 +9,26 @@ import { DebugAdapterTracker, DebugAdapterTrackerFactory, DebugConfiguration, De import { DebugProtocol } from 'vscode-debugprotocol'; import { IFileSystem } from '../../../common/platform/types'; +import { StopWatch } from '../../../common/utils/stopWatch'; import { EXTENSION_ROOT_DIR } from '../../../constants'; class DebugSessionLoggingTracker implements DebugAdapterTracker { private readonly session: DebugSession; private readonly enabled: boolean = false; - private stream: fs.WriteStream; + private stream: fs.WriteStream | undefined; + private timer = new StopWatch(); + constructor(session: DebugSession, fileSystem: IFileSystem) { this.session = session; this.enabled = this.session.configuration.logToFile as boolean; - const fileName = `debug_ext_${this.session.id}.log`; - this.stream = fileSystem.createWriteStream(path.join(EXTENSION_ROOT_DIR, fileName)); + if (this.enabled) { + const fileName = `debug_ext_${this.session.id}.log`; + this.stream = fileSystem.createWriteStream(path.join(EXTENSION_ROOT_DIR, fileName)); + } } public onWillStartSession() { + this.timer.reset(); this.log(`Stoping Session:\n${this.stringify(this.session.configuration)}\n`); } @@ -35,7 +41,7 @@ class DebugSessionLoggingTracker implements DebugAdapterTracker { } public onWillStopSession() { - this.log(`Stopping Session`); + this.log(`Stopping Session\n`); } public onError(error: Error) { @@ -43,12 +49,12 @@ class DebugSessionLoggingTracker implements DebugAdapterTracker { } public onExit(code: number | undefined, signal: string | undefined) { - this.log(`Exit:\nExit-Code: ${code ? code : 0}\nSignal: ${signal ? signal : 'none'}`); + this.log(`Exit:\nExit-Code: ${code ? code : 0}\nSignal: ${signal ? signal : 'none'}\n`); } private log(message: string) { if (this.enabled) { - this.stream.write(`${Math.floor(Date.now())} ${message}`); + this.stream!.write(`${this.timer.elapsedTime} ${message}`); } } From 50a0ebbaa4e06b35a46453c84962ccbb728ca2b4 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 28 Oct 2019 18:46:43 -0700 Subject: [PATCH 4/8] Change log file name --- src/client/debugger/extension/adapter/logging.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/debugger/extension/adapter/logging.ts b/src/client/debugger/extension/adapter/logging.ts index f9d8ee41bbd5..5a453b25e4ed 100644 --- a/src/client/debugger/extension/adapter/logging.ts +++ b/src/client/debugger/extension/adapter/logging.ts @@ -22,14 +22,14 @@ class DebugSessionLoggingTracker implements DebugAdapterTracker { this.session = session; this.enabled = this.session.configuration.logToFile as boolean; if (this.enabled) { - const fileName = `debug_ext_${this.session.id}.log`; + const fileName = `debugger.vscode_${this.session.id}.log`; this.stream = fileSystem.createWriteStream(path.join(EXTENSION_ROOT_DIR, fileName)); } } public onWillStartSession() { this.timer.reset(); - this.log(`Stoping Session:\n${this.stringify(this.session.configuration)}\n`); + this.log(`Stopping Session:\n${this.stringify(this.session.configuration)}\n`); } public onWillReceiveMessage(message: DebugProtocol.Message) { From 7bb8308fff4a889643328e48f22b4524e6e3f9f7 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 28 Oct 2019 18:54:02 -0700 Subject: [PATCH 5/8] Add news item --- news/1 Enhancements/8106.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/1 Enhancements/8106.md diff --git a/news/1 Enhancements/8106.md b/news/1 Enhancements/8106.md new file mode 100644 index 000000000000..620185d45a9b --- /dev/null +++ b/news/1 Enhancements/8106.md @@ -0,0 +1 @@ +Add logging support for python debug adapter. From 273654ddf4a839be9a38c7bbf5c28f94509c07d6 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Tue, 29 Oct 2019 22:02:43 -0700 Subject: [PATCH 6/8] Adding unit test for debug session logger --- .../debugger/extension/adapter/logging.ts | 2 +- .../extension/adapter/activator.unit.test.ts | 2 +- .../extension/adapter/logging.unit.test.ts | 136 ++++++++++++++++++ 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 src/test/debugger/extension/adapter/logging.unit.test.ts diff --git a/src/client/debugger/extension/adapter/logging.ts b/src/client/debugger/extension/adapter/logging.ts index 5a453b25e4ed..3f2d1006b845 100644 --- a/src/client/debugger/extension/adapter/logging.ts +++ b/src/client/debugger/extension/adapter/logging.ts @@ -29,7 +29,7 @@ class DebugSessionLoggingTracker implements DebugAdapterTracker { public onWillStartSession() { this.timer.reset(); - this.log(`Stopping Session:\n${this.stringify(this.session.configuration)}\n`); + this.log(`Starting Session:\n${this.stringify(this.session.configuration)}\n`); } public onWillReceiveMessage(message: DebugProtocol.Message) { diff --git a/src/test/debugger/extension/adapter/activator.unit.test.ts b/src/test/debugger/extension/adapter/activator.unit.test.ts index 52e52fb661a2..07d4336ffaab 100644 --- a/src/test/debugger/extension/adapter/activator.unit.test.ts +++ b/src/test/debugger/extension/adapter/activator.unit.test.ts @@ -30,7 +30,7 @@ import { noop } from '../../../core'; import { MockOutputChannel } from '../../../mockClasses'; // tslint:disable-next-line: max-func-body-length -suite('Debugging - Adapter Factory Registration', () => { +suite('Debugging - Adapter Factory and logger Registration', () => { const oldValueOfVSC_PYTHON_UNIT_TEST = process.env.VSC_PYTHON_UNIT_TEST; const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST; let activator: IExtensionSingleActivationService; diff --git a/src/test/debugger/extension/adapter/logging.unit.test.ts b/src/test/debugger/extension/adapter/logging.unit.test.ts new file mode 100644 index 000000000000..d3b06de4f0dd --- /dev/null +++ b/src/test/debugger/extension/adapter/logging.unit.test.ts @@ -0,0 +1,136 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +'use strict'; + +import * as assert from 'assert'; +import * as fs from 'fs'; +import * as path from 'path'; +import { anything, instance, mock, verify, when } from 'ts-mockito'; +import { DebugSession, WorkspaceFolder } from 'vscode'; +import { DebugProtocol } from 'vscode-debugprotocol'; + +import { FileSystem } from '../../../../client/common/platform/fileSystem'; +import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; +import { DebugSessionLoggingFactory } from '../../../../client/debugger/extension/adapter/logging'; + +// tslint:disable-next-line: max-func-body-length +suite('Debugging - Session Logging', () => { + const oldValueOfVSC_PYTHON_UNIT_TEST = process.env.VSC_PYTHON_UNIT_TEST; + const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST; + let loggerFactory: DebugSessionLoggingFactory; + let fsService: FileSystem; + let writeStream: fs.WriteStream; + + setup(() => { + fsService = mock(FileSystem); + writeStream = mock(fs.WriteStream); + + process.env.VSC_PYTHON_UNIT_TEST = undefined; + process.env.VSC_PYTHON_CI_TEST = undefined; + + loggerFactory = new DebugSessionLoggingFactory(instance(fsService)); + }); + + teardown(() => { + process.env.VSC_PYTHON_UNIT_TEST = oldValueOfVSC_PYTHON_UNIT_TEST; + process.env.VSC_PYTHON_CI_TEST = oldValueOfVSC_PYTHON_CI_TEST; + }); + + function createSession(id: string, workspaceFolder?: WorkspaceFolder): DebugSession { + return { + configuration: { + name: '', + request: 'launch', + type: 'python' + }, + id: id, + name: 'python', + type: 'python', + workspaceFolder, + customRequest: () => Promise.resolve() + }; + } + + function createSessionWithLogging(id: string, logToFile: boolean, workspaceFolder?: WorkspaceFolder): DebugSession { + const session = createSession(id, workspaceFolder); + session.configuration.logToFile = logToFile; + return session; + } + + class TestMessage implements DebugProtocol.ProtocolMessage { + public seq: number; + public type: string; + public id: number; + public format: string; + public variables?: { [key: string]: string } | undefined; + public sendTelemetry?: boolean | undefined; + public showUser?: boolean | undefined; + public url?: string | undefined; + public urlLabel?: string | undefined; + constructor(id: number, seq: number, type: string) { + this.id = id; + this.format = 'json'; + this.seq = seq; + this.type = type; + } + } + + test('Create logger using session without logToFile', async () => { + const session = createSession('test1'); + const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`); + + loggerFactory.createDebugAdapterTracker(session); + + verify(fsService.createWriteStream(filePath)).never(); + }); + + test('Create logger using session with logToFile set to false', async () => { + const session = createSessionWithLogging('test2', false); + const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`); + + loggerFactory.createDebugAdapterTracker(session); + + verify(fsService.createWriteStream(filePath)).never(); + }); + + test('Create logger using session with logToFile set to true', async () => { + const session = createSessionWithLogging('test3', true); + const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`); + const logs: string[] = []; + + when(fsService.createWriteStream(filePath)).thenReturn(instance(writeStream)); + when(writeStream.write(anything())).thenCall((msg) => logs.push(msg)); + + const message = new TestMessage(1, 1, 'test-message'); + const logger = await loggerFactory.createDebugAdapterTracker(session); + + if (logger) { + logger.onWillStartSession!(); + assert.ok(logs.pop()!.includes('Starting Session')); + + logger.onDidSendMessage!(message); + const sentMessage = logs.pop(); + assert.ok(sentMessage!.includes('Client --> Adapter')); + assert.ok(sentMessage!.includes('test-message')); + + logger.onWillReceiveMessage!(message); + const receivedMessage = logs.pop(); + assert.ok(receivedMessage!.includes('Client <-- Adapter')); + assert.ok(receivedMessage!.includes('test-message')); + + logger.onWillStopSession!(); + assert.ok(logs.pop()!.includes('Stopping Session')); + + logger.onError!(new Error('test error message')); + assert.ok(logs.pop()!.includes('Error')); + + logger.onExit!(0, undefined); + assert.ok(logs.pop()!.includes('Exit')); + } + + verify(fsService.createWriteStream(filePath)).once(); + verify(writeStream.write(anything())).times(6); + assert.deepEqual(logs, []); + }); +}); From e66aaf00c31aac3d0ce6c43ec0889b20afa2993a Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Wed, 30 Oct 2019 13:55:44 -0700 Subject: [PATCH 7/8] Improve coverage. --- .../extension/adapter/logging.unit.test.ts | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/test/debugger/extension/adapter/logging.unit.test.ts b/src/test/debugger/extension/adapter/logging.unit.test.ts index d3b06de4f0dd..b765fa72ced8 100644 --- a/src/test/debugger/extension/adapter/logging.unit.test.ts +++ b/src/test/debugger/extension/adapter/logging.unit.test.ts @@ -80,7 +80,7 @@ suite('Debugging - Session Logging', () => { const session = createSession('test1'); const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`); - loggerFactory.createDebugAdapterTracker(session); + await loggerFactory.createDebugAdapterTracker(session); verify(fsService.createWriteStream(filePath)).never(); }); @@ -89,9 +89,15 @@ suite('Debugging - Session Logging', () => { const session = createSessionWithLogging('test2', false); const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`); - loggerFactory.createDebugAdapterTracker(session); + when(fsService.createWriteStream(filePath)).thenReturn(instance(writeStream)); + when(writeStream.write(anything())).thenReturn(true); + const logger = await loggerFactory.createDebugAdapterTracker(session); + if (logger) { + logger.onWillStartSession!(); + } verify(fsService.createWriteStream(filePath)).never(); + verify(writeStream.write(anything())).never(); }); test('Create logger using session with logToFile set to true', async () => { @@ -110,14 +116,14 @@ suite('Debugging - Session Logging', () => { assert.ok(logs.pop()!.includes('Starting Session')); logger.onDidSendMessage!(message); - const sentMessage = logs.pop(); - assert.ok(sentMessage!.includes('Client --> Adapter')); - assert.ok(sentMessage!.includes('test-message')); + const sentLog = logs.pop(); + assert.ok(sentLog!.includes('Client --> Adapter')); + assert.ok(sentLog!.includes('test-message')); logger.onWillReceiveMessage!(message); - const receivedMessage = logs.pop(); - assert.ok(receivedMessage!.includes('Client <-- Adapter')); - assert.ok(receivedMessage!.includes('test-message')); + const receivedLog = logs.pop(); + assert.ok(receivedLog!.includes('Client <-- Adapter')); + assert.ok(receivedLog!.includes('test-message')); logger.onWillStopSession!(); assert.ok(logs.pop()!.includes('Stopping Session')); @@ -125,12 +131,19 @@ suite('Debugging - Session Logging', () => { logger.onError!(new Error('test error message')); assert.ok(logs.pop()!.includes('Error')); - logger.onExit!(0, undefined); - assert.ok(logs.pop()!.includes('Exit')); + logger.onExit!(111, '222'); + const exitLog1 = logs.pop(); + assert.ok(exitLog1!.includes('Exit-Code: 111')); + assert.ok(exitLog1!.includes('Signal: 222')); + + logger.onExit!(undefined, undefined); + const exitLog2 = logs.pop(); + assert.ok(exitLog2!.includes('Exit-Code: 0')); + assert.ok(exitLog2!.includes('Signal: none')); } verify(fsService.createWriteStream(filePath)).once(); - verify(writeStream.write(anything())).times(6); + verify(writeStream.write(anything())).times(7); assert.deepEqual(logs, []); }); }); From 75e4b23c75bbdcbdfe85829ff324c18f431b6631 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Thu, 31 Oct 2019 11:36:02 -0700 Subject: [PATCH 8/8] Address review comments. --- src/client/debugger/extension/adapter/logging.ts | 15 +++++---------- .../extension/adapter/logging.unit.test.ts | 14 +++++++------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/client/debugger/extension/adapter/logging.ts b/src/client/debugger/extension/adapter/logging.ts index 3f2d1006b845..9c21831f71cf 100644 --- a/src/client/debugger/extension/adapter/logging.ts +++ b/src/client/debugger/extension/adapter/logging.ts @@ -13,13 +13,11 @@ import { StopWatch } from '../../../common/utils/stopWatch'; import { EXTENSION_ROOT_DIR } from '../../../constants'; class DebugSessionLoggingTracker implements DebugAdapterTracker { - private readonly session: DebugSession; private readonly enabled: boolean = false; private stream: fs.WriteStream | undefined; private timer = new StopWatch(); - constructor(session: DebugSession, fileSystem: IFileSystem) { - this.session = session; + constructor(private readonly session: DebugSession, fileSystem: IFileSystem) { this.enabled = this.session.configuration.logToFile as boolean; if (this.enabled) { const fileName = `debugger.vscode_${this.session.id}.log`; @@ -33,15 +31,15 @@ class DebugSessionLoggingTracker implements DebugAdapterTracker { } public onWillReceiveMessage(message: DebugProtocol.Message) { - this.log(`Client <-- Adapter:\n${this.stringify(message)}\n`); + this.log(`Client --> Adapter:\n${this.stringify(message)}\n`); } public onDidSendMessage(message: DebugProtocol.Message) { - this.log(`Client --> Adapter:\n${this.stringify(message)}\n`); + this.log(`Client <-- Adapter:\n${this.stringify(message)}\n`); } public onWillStopSession() { - this.log(`Stopping Session\n`); + this.log('Stopping Session\n'); } public onError(error: Error) { @@ -65,10 +63,7 @@ class DebugSessionLoggingTracker implements DebugAdapterTracker { @injectable() export class DebugSessionLoggingFactory implements DebugAdapterTrackerFactory { - constructor( - @inject(IFileSystem) private readonly fileSystem: IFileSystem - ) { - } + constructor(@inject(IFileSystem) private readonly fileSystem: IFileSystem) { } public createDebugAdapterTracker(session: DebugSession): ProviderResult { return new DebugSessionLoggingTracker(session, this.fileSystem); diff --git a/src/test/debugger/extension/adapter/logging.unit.test.ts b/src/test/debugger/extension/adapter/logging.unit.test.ts index b765fa72ced8..cae4d7b19328 100644 --- a/src/test/debugger/extension/adapter/logging.unit.test.ts +++ b/src/test/debugger/extension/adapter/logging.unit.test.ts @@ -63,11 +63,11 @@ suite('Debugging - Session Logging', () => { public type: string; public id: number; public format: string; - public variables?: { [key: string]: string } | undefined; - public sendTelemetry?: boolean | undefined; - public showUser?: boolean | undefined; - public url?: string | undefined; - public urlLabel?: string | undefined; + public variables?: { [key: string]: string }; + public sendTelemetry?: boolean; + public showUser?: boolean; + public url?: string; + public urlLabel?: string; constructor(id: number, seq: number, type: string) { this.id = id; this.format = 'json'; @@ -117,12 +117,12 @@ suite('Debugging - Session Logging', () => { logger.onDidSendMessage!(message); const sentLog = logs.pop(); - assert.ok(sentLog!.includes('Client --> Adapter')); + assert.ok(sentLog!.includes('Client <-- Adapter')); assert.ok(sentLog!.includes('test-message')); logger.onWillReceiveMessage!(message); const receivedLog = logs.pop(); - assert.ok(receivedLog!.includes('Client <-- Adapter')); + assert.ok(receivedLog!.includes('Client --> Adapter')); assert.ok(receivedLog!.includes('test-message')); logger.onWillStopSession!();