From fdac4af358370b78eb8ac34b3767692719532002 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Sun, 28 Apr 2024 17:12:14 +0800 Subject: [PATCH] Revert "update nicelogger" This reverts commit 0f441b0bf266db7003872dcc6e265c26ac0a63b4. nicelogger seems to have local changes and is dependent by nvchecker/tools.py --- nvchecker/lib/nicelogger.py | 71 ++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/nvchecker/lib/nicelogger.py b/nvchecker/lib/nicelogger.py index 58a7964..5f586bc 100644 --- a/nvchecker/lib/nicelogger.py +++ b/nvchecker/lib/nicelogger.py @@ -7,16 +7,14 @@ A Tornado-inspired logging formatter, with displayed time with millisecond accur FYI: pyftpdlib also has a Tornado-style logger. ''' -from __future__ import annotations - import sys import time import logging -class TornadoLogFormatter(logging.Formatter): - def __init__(self, color, *args, **kwargs): - super().__init__(*args, **kwargs) - self._color = color +class Colors: + def __init__(self, color=None): + if color is None: + color = support_color() if color: import curses curses.setupterm() @@ -25,19 +23,32 @@ class TornadoLogFormatter(logging.Formatter): curses.tigetstr("setf") or "", "ascii") else: fg_color = curses.tigetstr("setaf") or curses.tigetstr("setf") or b"" + + self.blue = str(curses.tparm(fg_color, 4), "ascii") + self.yellow = str(curses.tparm(fg_color, 3), "ascii") + self.green = str(curses.tparm(fg_color, 2), "ascii") + self.red = str(curses.tparm(fg_color, 1), "ascii") + self.bright_red = str(curses.tparm(fg_color, 9), "ascii") + self.normal = str(curses.tigetstr("sgr0"), "ascii") + + else: + self.blue = self.yellow = self.green = self.red = self.bright_red = self.normal = "" + + +class TornadoLogFormatter(logging.Formatter): + def __init__(self, color, *args, **kwargs): + super().__init__(*args, **kwargs) + self._color = color + if color: + colors = Colors(color=color) self._colors = { - logging.DEBUG: str(curses.tparm(fg_color, 4), # Blue - "ascii"), - logging.INFO: str(curses.tparm(fg_color, 2), # Green - "ascii"), - logging.WARNING: str(curses.tparm(fg_color, 3), # Yellow - "ascii"), - logging.ERROR: str(curses.tparm(fg_color, 1), # Red - "ascii"), - logging.CRITICAL: str(curses.tparm(fg_color, 9), # Bright Red - "ascii"), + logging.DEBUG: colors.blue, + logging.INFO: colors.green, + logging.WARNING: colors.yellow, + logging.ERROR: colors.red, + logging.CRITICAL: colors.bright_red, } - self._normal = str(curses.tigetstr("sgr0"), "ascii") + self._normal = colors.normal def format(self, record): try: @@ -60,7 +71,6 @@ class TornadoLogFormatter(logging.Formatter): 'filename', 'exc_info', 'exc_text', 'created', 'funcName', 'processName', 'process', 'msecs', 'relativeCreated', 'thread', 'threadName', 'name', 'levelno', 'msg', 'pathname', 'stack_info', - 'taskName', }) if record.exc_info: @@ -70,6 +80,18 @@ class TornadoLogFormatter(logging.Formatter): formatted = formatted.rstrip() + "\n" + record.exc_text return formatted.replace("\n", "\n ") +def support_color(stream=sys.stderr): + if stream.isatty(): + try: + import curses + curses.setupterm() + if curses.tigetnum("colors") > 0: + return True + except: + import traceback + traceback.print_exc() + return False + def enable_pretty_logging(level=logging.DEBUG, handler=None, color=None): ''' handler: specify a handler instead of default StreamHandler @@ -81,17 +103,8 @@ def enable_pretty_logging(level=logging.DEBUG, handler=None, color=None): h = logging.StreamHandler() else: h = handler - if color is None: - color = False - if handler is None and sys.stderr.isatty(): - try: - import curses - curses.setupterm() - if curses.tigetnum("colors") > 0: - color = True - except: - import traceback - traceback.print_exc() + if color is None and handler is None: + color = support_color() formatter = TornadoLogFormatter(color=color) h.setLevel(level) h.setFormatter(formatter)