Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Fix attach() and enable_attach() with log_dir=, and add tests for var…
Browse files Browse the repository at this point in the history
…ious ways to enable logging.
  • Loading branch information
int19h committed Mar 5, 2019
1 parent 848e534 commit 5e8861f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
24 changes: 14 additions & 10 deletions src/ptvsd/attach_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def wait_for_attach(timeout=None):
debugger_attached.wait(timeout)


def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True, log=None):
def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True, log_dir=None):
"""Enables a client to attach to this process remotely to debug Python code.
Parameters
Expand All @@ -53,8 +53,9 @@ def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True, lo
redirect_output : bool, optional
Specifies whether any output (on both `stdout` and `stderr`) produced
by this program should be sent to the debugger. Default is ``True``.
log : str, optional
Name of the file that debugger will use as a log.
log_dir : str, optional
Name of the directory that debugger will create its log files in.
If not specified, logging is disabled.
Notes
-----
Expand All @@ -69,8 +70,9 @@ def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True, lo
called will not be visible.
"""

if log:
ptvsd.log.to_file(log)
if log_dir:
ptvsd.options.log_dir = log_dir
ptvsd.log.to_file()
ptvsd.log.info('enable_attach{0!r}', (address, redirect_output))

if is_attached():
Expand All @@ -89,7 +91,7 @@ def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True, lo
)


def attach(address, redirect_output=True, log=None):
def attach(address, redirect_output=True, log_dir=None):
"""Attaches this process to the debugger listening on a given address.
Parameters
Expand All @@ -102,12 +104,14 @@ def attach(address, redirect_output=True, log=None):
redirect_output : bool, optional
Specifies whether any output (on both `stdout` and `stderr`) produced
by this program should be sent to the debugger. Default is ``True``.
log : str, optional
Name of the file that debugger will use as a log.
log_dir : str, optional
Name of the directory that debugger will create its log files in.
If not specified, logging is disabled.
"""

if log:
ptvsd.log.to_file(log)
if log_dir:
ptvsd.options.log_dir = log_dir
ptvsd.log.to_file()
ptvsd.log.info('attach{0!r}', (address, redirect_output))

if is_attached():
Expand Down
55 changes: 55 additions & 0 deletions tests/func/test_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in the project root
# for license information.

from __future__ import print_function, with_statement, absolute_import

import contextlib
import pytest

from tests.helpers.session import DebugSession


@contextlib.contextmanager
def check_logs(tmpdir, session):
assert not tmpdir.listdir('ptvsd-*.log')
yield
assert len(tmpdir.listdir('ptvsd-*.log')) == 1
log_name = 'ptvsd-{}.log'.format(session.pid)
assert tmpdir.join(log_name).size() > 0


@pytest.mark.parametrize('cli', ['arg', 'env'])
def test_log_cli(pyfile, tmpdir, run_as, start_method, cli):
if cli == 'arg' and start_method == 'attach_socket_import':
pytest.skip()

@pyfile
def code_to_debug():
from dbgimporter import import_and_enable_debugger
import_and_enable_debugger()

with DebugSession() as session:
with check_logs(tmpdir, session):
if cli == 'arg':
session.log_dir = str(tmpdir)
else:
session.env['PTVSD_LOG_DIR'] = str(tmpdir)
session.initialize(target=(run_as, code_to_debug), start_method=start_method)
session.start_debugging()
session.wait_for_exit()


def test_log_api(pyfile, tmpdir, run_as):
@pyfile
def code_to_debug():
import sys
from dbgimporter import import_and_enable_debugger
import_and_enable_debugger(log_dir=str(sys.argv[1]))

with DebugSession() as session:
with check_logs(tmpdir, session):
session.program_args += [str(tmpdir)]
session.initialize(target=(run_as, code_to_debug), start_method='attach_socket_import')
session.start_debugging()
session.wait_for_exit()
4 changes: 2 additions & 2 deletions tests/helpers/debuggee/dbgimporter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os

def import_and_enable_debugger():
def import_and_enable_debugger(**kwargs):
if os.getenv('PTVSD_ENABLE_ATTACH', False):
import ptvsd
host = os.getenv('PTVSD_TEST_HOST', 'localhost')
port = os.getenv('PTVSD_TEST_PORT', '5678')
ptvsd.enable_attach((host, port))
ptvsd.enable_attach((host, port), **kwargs)
ptvsd.wait_for_attach()
4 changes: 4 additions & 0 deletions tests/helpers/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, start_method='launch', ptvsd_port=None, pid=None):
self.cwd = None
self.expected_returncode = 0
self.program_args = []
self.log_dir = None

self.is_running = False
self.process = None
Expand Down Expand Up @@ -257,6 +258,9 @@ def initialize(self, **kwargs):
else:
pytest.fail()

if self.log_dir:
dbg_argv += ['--log-dir', self.log_dir]

if self.no_debug:
dbg_argv += ['--nodebug']

Expand Down

0 comments on commit 5e8861f

Please sign in to comment.