From 1a7a3f0e4318bccbd3ed177d5b07fa3618d06868 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Sat, 10 Jun 2017 01:18:27 +0200 Subject: 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 --- src/osmo_gsm_tester/template.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/osmo_gsm_tester/template.py') diff --git a/src/osmo_gsm_tester/template.py b/src/osmo_gsm_tester/template.py index 33ce2f6..0ddfc84 100644 --- a/src/osmo_gsm_tester/template.py +++ b/src/osmo_gsm_tester/template.py @@ -26,7 +26,7 @@ from . import log from .util import dict2obj _lookup = None -_logger = log.Origin('no templates dir set') +_logger = log.Origin(log.C_CNF, 'no templates dir set') def set_templates_dir(*templates_dirs): global _lookup @@ -39,7 +39,7 @@ def set_templates_dir(*templates_dirs): raise RuntimeError('templates dir is not a dir: %r' % os.path.abspath(d)) _lookup = TemplateLookup(directories=templates_dirs) - _logger = log.Origin('Templates', category=log.C_CNF) + _logger = log.Origin(log.C_CNF, 'Templates') def render(name, values): '''feed values dict into template and return rendered result. @@ -48,11 +48,11 @@ def render(name, values): if _lookup is None: set_templates_dir() tmpl_name = name + '.tmpl' - with log.Origin(tmpl_name): - template = _lookup.get_template(tmpl_name) - _logger.dbg('rendering', tmpl_name) + log.ctx(tmpl_name) + template = _lookup.get_template(tmpl_name) + _logger.dbg('rendering', tmpl_name) - line_info_name = tmpl_name.replace('-', '_').replace('.', '_') - return template.render(**dict2obj(values)) + line_info_name = tmpl_name.replace('-', '_').replace('.', '_') + return template.render(**dict2obj(values)) # vim: expandtab tabstop=4 shiftwidth=4 -- cgit v1.2.3