utils.debug: Allow passing logger to log_time.
This commit is contained in:
parent
80d3bb712d
commit
545d82a04d
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import inspect
|
import inspect
|
||||||
|
import logging
|
||||||
import functools
|
import functools
|
||||||
import datetime
|
import datetime
|
||||||
import contextlib
|
import contextlib
|
||||||
@ -229,9 +230,11 @@ def log_time(logger, action='operation'):
|
|||||||
"""Log the time the operation in the with-block takes.
|
"""Log the time the operation in the with-block takes.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
logger: The logging.Logger to use for logging.
|
logger: The logging.Logger to use for logging, or a logger name.
|
||||||
action: A description of what's being done.
|
action: A description of what's being done.
|
||||||
"""
|
"""
|
||||||
|
if isinstance(logger, str):
|
||||||
|
logger = logging.getLogger(logger)
|
||||||
started = datetime.datetime.now()
|
started = datetime.datetime.now()
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
|
@ -81,11 +81,13 @@ def test_log_signals(caplog, signal_obj):
|
|||||||
assert records[1].msg == "Signal in <repr>: signal2('foo', 'bar')"
|
assert records[1].msg == "Signal in <repr>: signal2('foo', 'bar')"
|
||||||
|
|
||||||
|
|
||||||
def test_log_time(caplog):
|
class TestLogTime:
|
||||||
|
|
||||||
|
def test_duration(self, caplog):
|
||||||
logger_name = 'qt-tests'
|
logger_name = 'qt-tests'
|
||||||
|
|
||||||
with caplog.atLevel(logging.DEBUG, logger_name):
|
with caplog.atLevel(logging.DEBUG, logger_name):
|
||||||
with debug.log_time(logging.getLogger(logger_name), action='foobar'):
|
with debug.log_time(logger_name, action='foobar'):
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
records = caplog.records()
|
records = caplog.records()
|
||||||
@ -98,6 +100,16 @@ def test_log_time(caplog):
|
|||||||
duration = float(match.group(1))
|
duration = float(match.group(1))
|
||||||
assert 0 < duration < 5
|
assert 0 < duration < 5
|
||||||
|
|
||||||
|
def test_logger(self, caplog):
|
||||||
|
"""Test with an explicit logger instead of a name."""
|
||||||
|
logger_name = 'qt-tests'
|
||||||
|
|
||||||
|
with caplog.atLevel(logging.DEBUG, logger_name):
|
||||||
|
with debug.log_time(logging.getLogger(logger_name)):
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert len(caplog.records()) == 1
|
||||||
|
|
||||||
|
|
||||||
class TestQEnumKey:
|
class TestQEnumKey:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user