diff --git a/lisa/utils.py b/lisa/utils.py index 85fa4e4915dbd896e1943d98a4215f30710688c0..e9552dafa41414802232ff97076f8805a6f95c8a 100644 --- a/lisa/utils.py +++ b/lisa/utils.py @@ -235,13 +235,36 @@ class instancemethod: ) -class _DummyLogger: +class _WrappedLogger: + def __init__(self, logger): + self.logger = logger + def __getattr__(self, attr): - x = getattr(logging, attr) + x = getattr(self.logger, attr) + if callable(x): - return lambda *args, **kwargs: None + @functools.wraps(x) + def wrapper(*args, **kwargs): + try: + return x(*args, **kwargs) + except Exception as e: + # If we are invoked inside a destructor, the world may be + # broken and problems are expected, so exceptions can be + # silenced. + # + # Note: We only do the check if a problem occurs as + # inspect.stack() is very costly (150ms for 25 frames) + if any ( + frame.function == '__del__' + for frame in inspect.stack() + ): + return None + else: + raise + + return wrapper else: - return None + return x class Loggable: @@ -259,21 +282,16 @@ class Loggable: @classmethod def get_logger(cls, suffix=None): - if any ( - frame.function == '__del__' - for frame in inspect.stack() - ): - return _DummyLogger() + cls_name = cls.__name__ + module = inspect.getmodule(cls) + if module: + name = module.__name__ + '.' + cls_name else: - cls_name = cls.__name__ - module = inspect.getmodule(cls) - if module: - name = module.__name__ + '.' + cls_name - else: - name = cls_name - if suffix: - name += '.' + suffix - return logging.getLogger(name) + name = cls_name + if suffix: + name += '.' + suffix + logger = logging.getLogger(name) + return _WrappedLogger(logger) @classmethod def log_locals(cls, var_names=None, level='debug'): diff --git a/tests/test_wlgen_rtapp.py b/tests/test_wlgen_rtapp.py index 49d0953a42bdb8e258080f054676c3449177e0d6..08eecc3602263780f3e193b042135465b9a3eb1a 100644 --- a/tests/test_wlgen_rtapp.py +++ b/tests/test_wlgen_rtapp.py @@ -313,9 +313,7 @@ class TestRTACalibrationConf(RTABase): self.target, name='test', res_dir=self.res_dir, profile=profile, calibration=calibration) - rtapp.deploy() - - with open(rtapp.local_json) as fh: + with rtapp, open(rtapp.local_json) as fh: return json.load(fh)['global']['calibration'] def test_calibration_conf_pload_nodata(self):