def error(msg, label="parent", logger=None, **kwargs): logger = logger or logging if get_logging_level(logger) > logging.ERROR: return if label == "parent": label = get_caller() msg = format_msg(msg, label=label, color="red", **kwargs) logger.error(msg)
def warn(msg, label="parent", logger=None, **kwargs): logger = logger or logging if get_logging_level(logger) > logging.WARNING: return if label == "parent": label = get_caller() msg = format_msg(msg, label=label, color="yellow", **kwargs) logger.warning(msg)
def dbg(msg, label="parent", logger=None, autocolor=True, **kwargs): logger = logger or logging if get_logging_level(logger) > logging.DEBUG: return if label == "parent": label = get_caller() msg = format_msg(msg, label=label, autocolor=autocolor, **kwargs) logger.debug(msg)
def format_msg( msg, label="parent", indent=None, color=None, autocolor=False, format_func=pf ): term_width = get_terminal_width() or DEFAULT_TERM_WIDTH effective_width = term_width indent_str = None if label == "parent": label = get_caller() if indent is not None: if indent == "label": assert label, "indent=label but no label given" indent_str = " " * (len(label) + 2) else: indent = int(indent) indent_str = " " * indent effective_width = ( max(effective_width - len(indent_str), 0) or DEFAULT_TERM_WIDTH ) if isinstance(format_func, str): format_func = globals()[format_func] if format_func and format_func != pf: msg = format_func(msg) if label and not indent: msg = "\n" + msg elif not isinstance(msg, str): msg = pf(msg, width=effective_width) if label and not indent: msg = "\n" + msg if indent is not None: lines = msg.splitlines(True) msg = "\n".join(["\n".join(wrap(line, effective_width)) for line in lines]) if indent == "label": msg = indent_str.join(msg.splitlines(True)) else: msg = indent_str + indent_str.join(msg.splitlines(True)) if label: msg = "\n" + msg if label: msg = label.strip() + ": " + msg if (not color) and autocolor: assert label, "No label provided, can not use autocolor" color_index = ord(label[0]) % len(COLOR_OPTIONS) color = COLOR_OPTIONS[color_index] if color: msg = getattr(FontColors, color.upper()) + msg + FontSpecialChars.ENDC return msg
def my_error(msg): error(msg, label=get_caller(), logger=my_logger)
def my_warn(msg): warn(msg, label=get_caller(), logger=my_logger)
def my_info(msg): info(msg, label=get_caller(), logger=my_logger)
def my_dbg(msg): dbg(msg, label=get_caller(), logger=my_logger)