forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Adds logging support for python debug adapter #8272
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6e4e842
Add debug session logging via tracker
karthiknadig a63c78b
Add log messages
karthiknadig 3372ea6
Ensure we only log as needed.
karthiknadig 50a0ebb
Change log file name
karthiknadig 7bb8308
Add news item
karthiknadig 273654d
Adding unit test for debug session logger
karthiknadig e66aaf0
Improve coverage.
karthiknadig 75e4b23
Address review comments.
karthiknadig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add logging support for python debug adapter. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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, DebugConfiguration, DebugSession, ProviderResult } from 'vscode'; | ||
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 | undefined; | ||
private timer = new StopWatch(); | ||
|
||
constructor(session: DebugSession, fileSystem: IFileSystem) { | ||
this.session = session; | ||
karthiknadig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.enabled = this.session.configuration.logToFile as boolean; | ||
if (this.enabled) { | ||
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(`Starting Session:\n${this.stringify(this.session.configuration)}\n`); | ||
} | ||
|
||
public onWillReceiveMessage(message: DebugProtocol.Message) { | ||
this.log(`Client <-- Adapter:\n${this.stringify(message)}\n`); | ||
} | ||
|
||
public onDidSendMessage(message: DebugProtocol.Message) { | ||
this.log(`Client --> Adapter:\n${this.stringify(message)}\n`); | ||
} | ||
|
||
public onWillStopSession() { | ||
this.log(`Stopping Session\n`); | ||
karthiknadig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public onError(error: Error) { | ||
this.log(`Error:\n${this.stringify(error)}\n`); | ||
} | ||
|
||
public onExit(code: number | undefined, signal: string | undefined) { | ||
this.log(`Exit:\nExit-Code: ${code ? code : 0}\nSignal: ${signal ? signal : 'none'}\n`); | ||
} | ||
|
||
private log(message: string) { | ||
if (this.enabled) { | ||
this.stream!.write(`${this.timer.elapsedTime} ${message}`); | ||
} | ||
} | ||
|
||
private stringify(data: DebugProtocol.Message | Error | DebugConfiguration) { | ||
return JSON.stringify(data, null, 4); | ||
} | ||
} | ||
|
||
@injectable() | ||
export class DebugSessionLoggingFactory implements DebugAdapterTrackerFactory { | ||
constructor( | ||
@inject(IFileSystem) private readonly fileSystem: IFileSystem | ||
) { | ||
kimadeline marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public createDebugAdapterTracker(session: DebugSession): ProviderResult<DebugAdapterTracker> { | ||
return new DebugSessionLoggingTracker(session, this.fileSystem); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/test/debugger/extension/adapter/logging.unit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// 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; | ||
karthiknadig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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`); | ||
|
||
await 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`); | ||
|
||
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 () => { | ||
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 sentLog = logs.pop(); | ||
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('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!(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(7); | ||
assert.deepEqual(logs, []); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.