path: root/src/osmo_gsm_tester/osmo_hlr.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_hlr.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_hlr.py')
1 files changed, 11 insertions, 12 deletions
diff --git a/src/osmo_gsm_tester/osmo_hlr.py b/src/osmo_gsm_tester/osmo_hlr.py
index 3492f06..110faa5 100644
--- a/src/osmo_gsm_tester/osmo_hlr.py
+++ b/src/osmo_gsm_tester/osmo_hlr.py
@@ -32,10 +32,9 @@ class OsmoHlr(log.Origin):
next_subscriber_id = 1
def __init__(self, suite_run, ip_address):
+ super().__init__(log.C_RUN, 'osmo-hlr_%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-hlr_%s' % ip_address.get('addr'))
self.bts = []
def start(self):
@@ -47,16 +46,16 @@ class OsmoHlr(log.Origin):
binary = inst.child('bin', 'osmo-hlr')
if not os.path.isfile(binary):
- self.raise_exn('Binary missing:', binary)
+ raise log.Error('Binary missing:', binary)
lib = inst.child('lib')
if not os.path.isdir(lib):
- self.raise_exn('No lib/ in', inst)
+ raise log.Error('No lib/ in', inst)
# bootstrap an empty hlr.db
self.db_file = self.run_dir.new_file('hlr.db')
sql_input = inst.child('share/doc/osmo-hlr/hlr.sql')
if not os.path.isfile(sql_input):
- self.raise_exn('hlr.sql missing:', sql_input)
+ raise log.Error('hlr.sql missing:', sql_input)
self.run_local('create_hlr_db', ('/bin/sh', '-c', 'sqlite3 %r < %r' % (self.db_file, sql_input)))
iface = util.ip_to_iface(self.addr())
@@ -96,13 +95,13 @@ class OsmoHlr(log.Origin):
return not self.process.terminated()
def run_local(self, name, popen_args):
- with self:
- run_dir = self.run_dir.new_dir(name)
- proc = process.Process(name, run_dir, popen_args)
- proc.launch()
- proc.wait()
- if proc.result != 0:
- proc.raise_exn('Exited in error')
+ run_dir = self.run_dir.new_dir(name)
+ proc = process.Process(name, run_dir, popen_args)
+ proc.launch()
+ proc.wait()
+ if proc.result != 0:
+ log.ctx(proc)
+ raise log.Error('Exited in error')
def run_sql_file(self, name, sql_file):
self.run_local(name, ('/bin/sh', '-c', 'sqlite3 %r < %r' % (self.db_file, sql_file)))