Commit c7693dd0 authored by Olivier Kaufmann's avatar Olivier Kaufmann
Browse files

Tests event logging

Showing with 56 additions and 0 deletions
+56 -0
import datetime
import importlib
from ohmpi.config import HARDWARE_CONFIG
import adafruit_ads1x15.ads1115 as ads # noqa
......@@ -205,6 +206,7 @@ class Rx(RxAbstract):
def __init__(self, **kwargs):
kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
super().__init__(**kwargs)
self.exec_logger.event(f'Init_RX\tbegin\t{datetime.datetime.utcnow()}')
if self.ctl is None:
self.ctl = ctl_module.Ctl()
# elif isinstance(self.ctl, dict):
......@@ -216,6 +218,7 @@ class Rx(RxAbstract):
self._ads_voltage = ads.ADS1115(self.ctl.bus, gain=self._adc_gain, data_rate=860, address=self._ads_voltage_address)
self._ads_voltage.mode = Mode.CONTINUOUS
self._sampling_rate = kwargs.pop('sampling_rate', sampling_rate)
self.exec_logger.event(f'Init_RX\tend\t{datetime.datetime.utcnow()}')
@property
def adc_gain(self):
......
......@@ -10,6 +10,58 @@ import sys
from termcolor import colored
def add_logging_level(levelName, levelNum, methodName=None):
"""
Comprehensively adds a new logging level to the `logging` module and the
currently configured logging class.
`levelName` becomes an attribute of the `logging` module with the value
`levelNum`. `methodName` becomes a convenience method for both `logging`
itself and the class returned by `logging.getLoggerClass()` (usually just
`logging.Logger`). If `methodName` is not specified, `levelName.lower()` is
used.
To avoid accidental clobberings of existing attributes, this method will
raise an `AttributeError` if the level name is already an attribute of the
`logging` module or if the method name is already present
comes from https://stackoverflow.com/questions/2183233
Example
-------
>>> add_logging_level('TRACE', logging.DEBUG - 5)
>>> logging.getLogger(__name__).setLevel("TRACE")
>>> logging.getLogger(__name__).trace('that worked')
>>> logging.trace('so did this')
>>> logging.TRACE
5
"""
if not methodName:
methodName = levelName.lower()
if hasattr(logging, levelName):
raise AttributeError('{} already defined in logging module'.format(levelName))
if hasattr(logging, methodName):
raise AttributeError('{} already defined in logging module'.format(methodName))
if hasattr(logging.getLoggerClass(), methodName):
raise AttributeError('{} already defined in logger class'.format(methodName))
# This method was inspired by the answers to Stack Overflow post
# http://stackoverflow.com/q/2183233/2988730, especially
# http://stackoverflow.com/a/13638084/2988730
def logForLevel(self, message, *args, **kwargs):
if self.isEnabledFor(levelNum):
self._log(levelNum, message, args, **kwargs)
def logToRoot(message, *args, **kwargs):
logging.log(levelNum, message, *args, **kwargs)
logging.addLevelName(levelNum, levelName)
setattr(logging, levelName, levelNum)
setattr(logging.getLoggerClass(), methodName, logForLevel)
setattr(logging, methodName, logToRoot)
def create_stdout_logger(name):
logger = logging.getLogger(f'{name}_logger')
log_format = f'%(asctime)-15s | {name[:8]:8s} | %(levelname)s: %(message)s'
......@@ -24,6 +76,7 @@ def create_stdout_logger(name):
def setup_loggers(mqtt=True):
add_logging_level('EVENT', logging.INFO + 1) # TODO : check if we should set the level to DEBUG...
msg = ''
# Message logging setup
log_path = path.join(path.dirname(__file__), 'logs')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment