Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, *args, general: Mapping[Verbosity, logging.Formatter] = None,
special: Mapping[str, Mapping[Verbosity, logging.Formatter]] = None,
verbosity: Verbosity = Verbosity.NORMAL, **kwargs):
super().__init__(*args, **kwargs) # will be '%(message)s' if no arguments were given
self.verbosity = verbosity
self._warn_was_shown = False
if general is not None:
self.general = general
else:
warnings.warn("'general' argument for DispatchingFormatter was not provided. It contains formatters for "
"all the logging events except special ones and should be a dict with verbosity levels keys "
"and logging.Formatter values")
self.general = {}
if special is not None:
self.special = special
dummy: create a NullHandler logger if true
Returns:
logging.Logger instance
"""
if dummy:
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
else:
logger = logging.getLogger('stm32pio')
logger.setLevel(logging.DEBUG if args_verbose_counter else logging.INFO)
handler = logging.StreamHandler()
formatter = stm32pio.util.DispatchingFormatter(
verbosity=stm32pio.util.Verbosity.VERBOSE if args_verbose_counter else stm32pio.util.Verbosity.NORMAL,
general={
stm32pio.util.Verbosity.NORMAL: logging.Formatter("%(levelname)-8s %(message)s"),
stm32pio.util.Verbosity.VERBOSE: logging.Formatter(
f"%(levelname)-8s %(funcName)-{stm32pio.settings.log_fieldwidth_function}s %(message)s")
})
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug("debug logging enabled")
return logger
def verbose_setter(value):
"""Use this to toggle the verbosity of all loggers at once"""
module_logger.setLevel(logging.DEBUG if value else logging.INFO)
qml_logger.setLevel(logging.DEBUG if value else logging.INFO)
projects_logger.setLevel(logging.DEBUG if value else logging.INFO)
formatter.verbosity = stm32pio.util.Verbosity.VERBOSE if value else stm32pio.util.Verbosity.NORMAL
settings = Settings(prefix='app/settings/', qs_kwargs={ 'parent': app },
external_triggers={ 'verbose': verbose_setter })
# Use "singleton" real logger for all projects just wrapping it into the LoggingAdapter for every project
projects_logger = logging.getLogger('stm32pio_gui.projects')
projects_logger.setLevel(logging.DEBUG if settings.get('verbose') else logging.INFO)
formatter = stm32pio.util.DispatchingFormatter(
general={
stm32pio.util.Verbosity.NORMAL: logging.Formatter("%(levelname)-8s %(message)s"),
stm32pio.util.Verbosity.VERBOSE: logging.Formatter(
f"%(levelname)-8s %(funcName)-{stm32pio.settings.log_fieldwidth_function}s %(message)s")
})
projects_logger_handler.setFormatter(formatter)
projects_logger.addHandler(projects_logger_handler)
verbose_setter(settings.get('verbose')) # set initial verbosity settings based on the saved state
settings.beginGroup('app')
restored_projects_paths: List[str] = []
for index in range(settings.beginReadArray('projects')):
settings.setArrayIndex(index)
restored_projects_paths.append(settings.value('path'))
settings.endArray()
settings.endGroup()