summaryrefslogtreecommitdiffstats
path: root/src/osmo_gsm_tester/event_loop.py
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-06-10 01:18:27 +0200
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-06-13 13:32:01 +0200
commit1a7a3f0e4318bccbd3ed177d5b07fa3618d06868 (patch)
tree7ff22b29e37be6cdd8ad2b2737cfe2fee2aa01dc /src/osmo_gsm_tester/event_loop.py
parent0e57aadd280ec40bd3b8754f6169fc507b1b25bf (diff)
fix and refactor logging: drop 'with', simplify
With the recent fix of the junit report related issues, another issue arose: the 'with log.Origin' was changed to disallow __enter__ing an object twice to fix problems, now still code would fail because it tries to do 'with' on the same object twice. The only reason is to ensure that logging is associated with a given object. Instead of complicating even more, implement differently. Refactor logging to simplify use: drop the 'with Origin' style completely, and instead use the python stack to determine which objects are created by which, and which object to associate a log statement with. The new way: we rely on the convention that each class instance has a local 'self' referencing the object instance. If we need to find an origin as a new object's parent, or to associate a log message with, we traverse each stack frame, fetching the first local 'self' object that is a log.Origin class instance. How to use: Simply call log.log() anywhere, and it finds an Origin object to log for, from the stack. Alternatively call self.log() for any Origin() object to skip the lookup. Create classes as child class of log.Origin and make sure to call super().__init__(category, name). This constructor will magically find a parent Origin on the stack. When an exception happens, we first escalate the exception up through call scopes to where ever it is handled by log.log_exn(). This then finds an Origin object in the traceback's stack frames, no need to nest in 'with' scopes. Hence the 'with log.Origin' now "happens implicitly", we can write pure natural python code, no more hassles with scope ordering. Furthermore, any frame can place additional logging information in a frame by calling log.ctx(). This is automatically inserted in the ancestry associated with a log statement / exception. Change-Id: I5f9b53150f2bb6fa9d63ce27f0806f0ca6a45e90
Diffstat (limited to 'src/osmo_gsm_tester/event_loop.py')
-rw-r--r--src/osmo_gsm_tester/event_loop.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/osmo_gsm_tester/event_loop.py b/src/osmo_gsm_tester/event_loop.py
index c3c679b..6f64ef9 100644
--- a/src/osmo_gsm_tester/event_loop.py
+++ b/src/osmo_gsm_tester/event_loop.py
@@ -21,6 +21,8 @@
# A test script can thus establish its context by doing:
# from osmo_gsm_tester.test import *
import time
+from . import log
+
poll_funcs = []
def register_poll_func(func):
@@ -38,7 +40,8 @@ def poll():
def wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep):
if not timeout or timeout < 0:
- log_obj.raise_exn('wait() *must* time out at some point. timeout=%r' % timeout)
+ self = log_obj
+ raise log.Error('wait() *must* time out at some point.', timeout=timeout)
if timestep < 0.1:
timestep = 0.1
@@ -54,7 +57,8 @@ def wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout,
def wait(log_obj, condition, *condition_args, timeout=300, timestep=1, **condition_kwargs):
if not wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep):
- log_obj.raise_exn('Wait timeout')
+ log.ctx(log_obj)
+ raise log.Error('Wait timeout')
def sleep(log_obj, seconds):
assert seconds > 0.