Commit c81dfb3c authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby
Browse files

windows: Fix posix color on windows terminal.

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