path: root/src/osmo_gsm_tester/osmo_nitb.py
diff options
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/osmo_nitb.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/osmo_nitb.py')
1 files changed, 4 insertions, 8 deletions
diff --git a/src/osmo_gsm_tester/osmo_nitb.py b/src/osmo_gsm_tester/osmo_nitb.py
index b4bf247..3104842 100644
--- a/src/osmo_gsm_tester/osmo_nitb.py
+++ b/src/osmo_gsm_tester/osmo_nitb.py
@@ -32,10 +32,9 @@ class OsmoNitb(log.Origin):
bts = None
def __init__(self, suite_run, ip_address):
+ super().__init__(log.C_RUN, 'osmo-nitb_%s' % ip_address.get('addr'))
self.suite_run = suite_run
self.ip_address = ip_address
- self.set_log_category(log.C_RUN)
- self.set_name('osmo-nitb_%s' % ip_address.get('addr'))
self.bts = []
def start(self):
@@ -106,8 +105,7 @@ class OsmoNitb(log.Origin):
msisdn = self.suite_run.resources_pool.next_msisdn(modem)
self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi())
- with self:
- OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki())
+ OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki())
def subscriber_attached(self, *modems):
return self.imsi_attached(*[m.imsi() for m in modems])
@@ -118,8 +116,7 @@ class OsmoNitb(log.Origin):
return all([(imsi in attached) for imsi in imsis])
def imsi_list_attached(self):
- with self:
- return OsmoNitbCtrl(self).subscriber_list_active()
+ return OsmoNitbCtrl(self).subscriber_list_active()
def running(self):
return not self.process.terminated()
@@ -133,8 +130,7 @@ class OsmoNitbCtrl(log.Origin):
def __init__(self, nitb):
self.nitb = nitb
- self.set_name('CTRL(%s:%d)' % (self.nitb.addr(), OsmoNitbCtrl.PORT))
- self.set_child_of(nitb)
+ super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.nitb.addr(), OsmoNitbCtrl.PORT))
def ctrl(self):
return osmo_ctrl.OsmoCtrl(self.nitb.addr(), OsmoNitbCtrl.PORT)