Commit bb2dda19 authored by Piergiorgio Pancino's avatar Piergiorgio Pancino Committed by Matias Guijarro

test_logtools:

* add recursive test for check complex tree activation
* add test for toggling a loglevel hardset with setLevel
parent 1ba9bb36
......@@ -10,7 +10,7 @@ import logging
import re
from bliss.common.logtools import map_update_loggers, Log, LogMixin, logging_startup
from bliss.common.standard import debugon, debugoff
from bliss.common.standard import debugon, debugoff, lslog
from bliss.common.mapping import Map
from bliss.common import session
import bliss
......@@ -39,6 +39,8 @@ def params(beacon, map):
logging.shutdown()
logging.setLoggerClass(logging.Logger)
logging.getLogger().handlers.clear() # deletes all handlers
logging.getLogger().manager.loggerDict.clear() # deletes all loggers
class NotMappedController(LogMixin):
......@@ -48,14 +50,20 @@ class NotMappedController(LogMixin):
name = "nmc"
def msg_debug(self):
self._logger.debug("Debug message")
def __init__(self, name="nmc"):
self.name = name
def msg_debug_data(self):
self._logger.debug_data("Debug message", b"asdasdadsa")
def msg_debug(self, msg=""):
self._logger.debug(f"Debug message {msg}")
def msg_info(self):
self._logger.info("Info message")
def msg_debug_data(self, msg=""):
self._logger.debug_data(f"Debug message {msg}", b"asdasdadsa")
def msg_info(self, msg=""):
self._logger.info(f"Info message {msg}")
def msg_error(self, msg=""):
self._logger.error(f"Error message {msg}")
class MappedController(NotMappedController, LogMixin):
......@@ -63,10 +71,9 @@ class MappedController(NotMappedController, LogMixin):
Logging on this device should succeed
"""
name = "mc"
def __init__(self):
session.get_current().map.register(self)
def __init__(self, name="mc", parents_list=None, children_list=None):
self.name = name
session.get_current().map.register(self, parents_list, children_list)
def test_bare_system(params):
......@@ -179,9 +186,9 @@ def test_LogMixin(params, caplog):
"""
beacon, log = params
nmc = NotMappedController()
nmc = NotMappedController("nmc")
assert nmc._logger.name == "session.controllers.nmc"
mc = MappedController()
mc = MappedController("mc")
assert mc._logger.name == "session.controllers.mc"
mc._logger.debugon() # activates debug logging level
......@@ -221,3 +228,100 @@ def test_standard_debugon_debugoff(params):
assert roby._logger.level == logging.NOTSET
assert roby._logger.getEffectiveLevel() == logging.WARNING
def node_check(obj, caplog, children=None):
"""
Activate/deactivat debug for one parent node and recursively check
that message on children are printed
"""
if children is None:
children = []
# obj = locals()[name]
for child in children:
# for child in (locals()[obj_name] for obj_name in children):
dbg_msg = f"{child!r} debug"
err_msg = f"{child!r} error"
child.msg_debug(dbg_msg)
child.msg_error(err_msg)
assert dbg_msg not in caplog.text
assert err_msg in caplog.text
debugon(obj) # debug at parent level should activate debug on child
child.msg_debug(dbg_msg)
assert dbg_msg in caplog.text
debugoff(obj)
caplog.clear()
child.msg_debug(dbg_msg)
child.msg_error(err_msg)
assert dbg_msg not in caplog.text
assert err_msg in caplog.text
caplog.clear()
# activate with _logger
child._logger.debugon()
child.msg_debug(dbg_msg)
child.msg_error(err_msg)
assert dbg_msg in caplog.text
assert err_msg in caplog.text
caplog.clear()
child._logger.debugoff()
child.msg_debug(dbg_msg)
child.msg_error(err_msg)
assert dbg_msg not in caplog.text
assert err_msg in caplog.text
caplog.clear()
debugoff(obj)
def test_chain_devices_log(params, caplog):
"""
Complex logging structure
"""
# build the device tree
# d1 -- d2 -- d3 -- d4 -- d5
# \- d6 -- d7 -- d8
# \- d9 -- d10
# \- d11
map = session.get_current().map
d1 = MappedController("d1")
d2 = MappedController("d2", parents_list=[d1])
d3 = MappedController("d3", parents_list=[d2])
d4 = MappedController("d4", parents_list=[d3])
d5 = MappedController("d5", parents_list=[d4])
d6 = MappedController("d6", parents_list=[d1])
d7 = MappedController("d7", parents_list=[d6])
d8 = MappedController("d8", parents_list=[d7])
d9 = MappedController("d9", parents_list=[d6])
d10 = MappedController("d10", parents_list=[d9])
d11 = MappedController("d11", parents_list=[d9])
node_check(d1, caplog, children=[d2, d3, d4, d5, d6, d7, d8, d9, d10, d11])
node_check(d2, caplog, children=[d3, d4, d5])
node_check(d6, caplog, children=[d7, d8, d9, d10, d11])
def test_level_switch(params, caplog):
"""
When we change the level manually with a value
different than WARNING, this should toggle properly with
debugon/debugoff
"""
beacon, log = params
m0 = beacon.get("m0")
assert m0._logger.level == logging.NOTSET
assert m0._logger.getEffectiveLevel() == logging.WARNING
m0._logger.debugon()
assert m0._logger.level == logging.DEBUG
assert m0._logger.getEffectiveLevel() == logging.DEBUG
m0._logger.debugon() # repeat twice for replicate bug
assert m0._logger.level == logging.DEBUG
assert m0._logger.getEffectiveLevel() == logging.DEBUG
m0._logger.debugoff()
assert m0._logger.level == logging.NOTSET
assert m0._logger.getEffectiveLevel() == logging.WARNING
# this will change also the default level
m0._logger.setLevel(logging.INFO)
m0._logger.debugon()
m0._logger.debugoff()
assert m0._logger.level == logging.INFO
assert m0._logger.getEffectiveLevel() == logging.INFO
Markdown is supported
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