1""" 2Logging support for make_layout. 3""" 4 5__author__ = "Steve Dower <steve.dower@python.org>" 6__version__ = "3.8" 7 8import logging 9import sys 10 11__all__ = [] 12 13LOG = None 14HAS_ERROR = False 15 16 17def public(f): 18 __all__.append(f.__name__) 19 return f 20 21 22@public 23def configure_logger(ns): 24 global LOG 25 if LOG: 26 return 27 28 LOG = logging.getLogger("make_layout") 29 LOG.level = logging.DEBUG 30 31 if ns.v: 32 s_level = max(logging.ERROR - ns.v * 10, logging.DEBUG) 33 f_level = max(logging.WARNING - ns.v * 10, logging.DEBUG) 34 else: 35 s_level = logging.ERROR 36 f_level = logging.INFO 37 38 handler = logging.StreamHandler(sys.stdout) 39 handler.setFormatter(logging.Formatter("{levelname:8s} {message}", style="{")) 40 handler.setLevel(s_level) 41 LOG.addHandler(handler) 42 43 if ns.log: 44 handler = logging.FileHandler(ns.log, encoding="utf-8", delay=True) 45 handler.setFormatter( 46 logging.Formatter("[{asctime}]{levelname:8s}: {message}", style="{") 47 ) 48 handler.setLevel(f_level) 49 LOG.addHandler(handler) 50 51 52class BraceMessage: 53 def __init__(self, fmt, *args, **kwargs): 54 self.fmt = fmt 55 self.args = args 56 self.kwargs = kwargs 57 58 def __str__(self): 59 return self.fmt.format(*self.args, **self.kwargs) 60 61 62@public 63def log_debug(msg, *args, **kwargs): 64 return LOG.debug(BraceMessage(msg, *args, **kwargs)) 65 66 67@public 68def log_info(msg, *args, **kwargs): 69 return LOG.info(BraceMessage(msg, *args, **kwargs)) 70 71 72@public 73def log_warning(msg, *args, **kwargs): 74 return LOG.warning(BraceMessage(msg, *args, **kwargs)) 75 76 77@public 78def log_error(msg, *args, **kwargs): 79 global HAS_ERROR 80 HAS_ERROR = True 81 return LOG.error(BraceMessage(msg, *args, **kwargs)) 82 83 84@public 85def log_exception(msg, *args, **kwargs): 86 global HAS_ERROR 87 HAS_ERROR = True 88 return LOG.exception(BraceMessage(msg, *args, **kwargs)) 89 90 91@public 92def error_was_logged(): 93 return HAS_ERROR 94