diff --git a/src/tools.py b/src/tools.py index 1ad8c382ec46c63e5881573f43eafe014bfbc30c..22e7a39ab9d7c917dcbb1e916b0732e233fafbc2 100644 --- a/src/tools.py +++ b/src/tools.py @@ -39,30 +39,28 @@ from functools import ( logger = logging.getLogger() +posix = os.name == "posix" + def logger_color_blue(): - posix = os.name == "posix" if posix: return f"{Style.BRIGHT}{Fore.BLUE}" return "" def logger_color_red(): - posix = os.name == "posix" if posix: return f"{Style.BRIGHT}{Fore.RED}" return "" def logger_color_green(): - posix = os.name == "posix" if posix: return f"{Style.BRIGHT}{Fore.GREEN}" return "" def logger_color_reset(): - posix = os.name == "posix" if posix: return f"{Style.RESET_ALL}" return "" @@ -70,12 +68,12 @@ def logger_color_reset(): def logger_exception(exception): logger.error( - f"[{Fore.RED}ERROR{Style.RESET_ALL}] " + - f"{Fore.RED}{e}{Style.RESET_ALL}" + f"[{logger_color_red()}ERROR{logger_color_reset()}] " + + f"{logger_color_red()}{e}{logger_color_reset()}" ) logger.debug( - f"{Fore.BLUE}{e}{Style.RESET_ALL}\n" + - f"{Fore.RED}{traceback.format_exc()}{Style.RESET_ALL}" + f"{logger_color_blue()}{e}{logger_color_reset()}\n" + + f"{logger_color_red()}{traceback.format_exc()}{logger_color_reset()}" ) ########## @@ -107,7 +105,7 @@ def display_timers(): ) head = " +--" - head += f"{Style.BRIGHT}{Fore.BLUE}Timers{Style.RESET_ALL}" + head += f"{logger_color_blue()}Timers{logger_color_reset()}" for t in range(fmax + 26): head += "-" head += "+" @@ -123,10 +121,10 @@ def display_timers(): ) for func, time, calls in lst: - name = (f"{Fore.BLUE}{func.__module__}{Style.RESET_ALL}" + - f".{Style.BRIGHT}{Fore.GREEN}" + + name = (f"{logger_color_blue()}{func.__module__}{logger_color_reset()}" + + f".{logger_color_green()}" + f"{func.__qualname__:<{fmax - len(func.__module__)}}" + - f"{Style.RESET_ALL}") + f"{logger_color_reset()}") logger.debug(f" | {name} | {time:>10.6f} sec | {calls:>5} calls |") tail = " +--" @@ -147,18 +145,18 @@ def timer(func): value = func(*args, **kwargs) except Exception as e: logger.error( - f"[{Fore.RED}ERROR{Style.RESET_ALL}] " + - f"{Fore.RED}{e}{Style.RESET_ALL}" + f"[{logger_color_red()}ERROR{logger_color_reset()}] " + + f"{logger_color_red()}{e}{logger_color_reset()}" ) logger.debug( - f"[{func.__module__}.{Fore.GREEN}" + + f"[{func.__module__}.{logger_color_green()}" + f"{func.__qualname__}" + - f"{Style.RESET_ALL}]: " + - f"{Fore.RED}{e}{Style.RESET_ALL}" + f"{logger_color_reset()}]: " + + f"{logger_color_red()}{e}{logger_color_reset()}" ) logger.debug( - f"{Fore.BLUE}{e}{Style.RESET_ALL}\n" + - f"{Fore.RED}{traceback.format_exc()}{Style.RESET_ALL}" + f"{logger_color_blue()}{e}{logger_color_reset()}\n" + + f"{logger_color_red()}{traceback.format_exc()}{logger_color_reset()}" ) end_time = time.perf_counter() @@ -183,10 +181,10 @@ def trace(func): @wraps(func) def wrapper(*args, **kwargs): t = time.ctime() - head = f"[{Fore.BLUE}TRACE{Style.RESET_ALL}]" + head = f"[{logger_color_blue()}TRACE{logger_color_reset()}]" c = ( - f"{head}[{t}] Call {func.__module__}.{Fore.GREEN}" + - f"{func.__qualname__}{Style.RESET_ALL}({args}, {kwargs})" + f"{head}[{t}] Call {func.__module__}.{logger_color_green()}" + + f"{func.__qualname__}{logger_color_reset()}({args}, {kwargs})" ) logger.debug(c) @@ -194,8 +192,8 @@ def trace(func): t = time.ctime() r = ( - f"{head}[{t}] Return {func.__module__}.{Fore.GREEN}" + - f"{func.__qualname__}{Style.RESET_ALL}: {value}" + f"{head}[{t}] Return {func.__module__}.{logger_color_green()}" + + f"{func.__qualname__}{logger_color_reset()}: {value}" ) logger.debug(r)