diff --git a/SDP/SDP/SDP.py b/SDP/SDP/SDP.py index df14607f1eea86e2b441cd03ae90532875b39a33..3dffae225596c61521835463c45c0176716152bd 100644 --- a/SDP/SDP/SDP.py +++ b/SDP/SDP/SDP.py @@ -26,11 +26,12 @@ import numpy from wrappers import only_in_states, only_when_on, fault_on_error from opcua_connection import OPCUAConnection -from lofar_logging import PythonLoggingMixin +from lofar_logging import device_logging_to_python, log_exceptions __all__ = ["SDP", "main"] -class SDP(Device, PythonLoggingMixin): +@device_logging_to_python("SDP") +class SDP(Device): """ **Properties:** @@ -193,6 +194,8 @@ class SDP(Device, PythonLoggingMixin): self.info_stream("Mapping OPC-UA MP/CP to attributes done.") + @log_exceptions + @DebugIt() def init_device(self): """ Instantiates the device in the OFF state. """ @@ -201,6 +204,7 @@ class SDP(Device, PythonLoggingMixin): self.set_state(DevState.OFF) + @log_exceptions def initialise(self): """Initialises the attributes and properties of the SDP.""" @@ -476,7 +480,6 @@ class SDP(Device, PythonLoggingMixin): """ self.set_state(DevState.FAULT) - # ---------- # Run server # ---------- diff --git a/SDP/SDP/lofar_logging.py b/SDP/SDP/lofar_logging.py index 09b8e52682ec60676081bedc5faf9d32d230c505..4330985c8f873583e20abbcbbb5a7b3252546c97 100644 --- a/SDP/SDP/lofar_logging.py +++ b/SDP/SDP/lofar_logging.py @@ -1,4 +1,6 @@ import logging +import tango +from functools import wraps def configured_logger(name=''): logger = logging.getLogger(name) @@ -13,17 +15,31 @@ def configured_logger(name=''): return logger -logger = configured_logger() - -class PythonLoggingMixin(object): - """ Also inherit this class to have your Tango Device log to python instead. """ - - # Monkey patch the python logger to replace the tango logger - debug_stream = logger.debug - info_stream = logger.info - warn_stream = logger.warning - warning_stream = logger.warning - error_stream = logger.error - fatal_stream = logger.fatal - critical_stream = logger.critical - +def device_logging_to_python(name = ''): + """ Call this on a Tango Device instance or class to have your Tango Device log to python instead. """ + + logger = configured_logger(name) + + def inner(cls): + # Monkey patch the python logger to replace the tango logger + cls.debug_stream = logger.debug + cls.info_stream = logger.info + cls.warn_stream = logger.warning + cls.warning_stream = logger.warning + cls.error_stream = logger.error + cls.fatal_stream = logger.fatal + cls.critical_stream = logger.critical + return cls + + return inner + +def log_exceptions(func): + @wraps(func) + def inner(self, *args, **kwargs): + try: + return func(self, *args, **kwargs) + except Exception as e: + self.error_stream("Caught exception: %s: %s", e.__class__.__name__, e, exc_info=1) + raise e + + return inner