From f181f33753c5d29164af1cc2db6d9e94da20bd60 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Fri, 14 Jan 2022 10:39:24 +0100 Subject: [PATCH] refactor logging --- catcli/logger.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/catcli/logger.py b/catcli/logger.py index d9d1e3a..52d2943 100644 --- a/catcli/logger.py +++ b/catcli/logger.py @@ -39,6 +39,9 @@ class Logger: Logger.BOLD = '' Logger.UND = '' + def fix_badchars(line): + return line.encode('utf-8', 'ignore').decode('utf-8') + ###################################################################### # node specific output ###################################################################### @@ -49,14 +52,14 @@ class Logger: end = ' {}({}){}'.format(Logger.GRAY, attr, Logger.RESET) s = '{}{}{}{}:'.format(pre, Logger.UND, Logger.STORAGE, Logger.RESET) s += ' {}{}{}{}\n'.format(Logger.PURPLE, - _fix_badchars(name), + Logger.fix_badchars(name), Logger.RESET, end) s += ' {}{}{}'.format(Logger.GRAY, args, Logger.RESET) sys.stdout.write('{}\n'.format(s)) def file(pre, name, attr): '''print a file node''' - s = '{}{}'.format(pre, _fix_badchars(name)) + s = '{}{}'.format(pre, Logger.fix_badchars(name)) s += ' {}[{}]{}'.format(Logger.GRAY, attr, Logger.RESET) sys.stdout.write('{}\n'.format(s)) @@ -70,13 +73,13 @@ class Logger: if end: end = ' [{}]'.format(', '.join(end)) s = '{}{}{}{}'.format(pre, Logger.BLUE, - _fix_badchars(name), Logger.RESET) + Logger.fix_badchars(name), Logger.RESET) s += '{}{}{}'.format(Logger.GRAY, end, Logger.RESET) sys.stdout.write('{}\n'.format(s)) def arc(pre, name, archive): s = '{}{}{}{}'.format(pre, Logger.YELLOW, - _fix_badchars(name), Logger.RESET) + Logger.fix_badchars(name), Logger.RESET) s += ' {}[{}:{}]{}'.format(Logger.GRAY, Logger.ARCHIVE, archive, Logger.RESET) sys.stdout.write('{}\n'.format(s)) @@ -86,44 +89,44 @@ class Logger: ###################################################################### def out(string): '''to stdout no color''' - string = _fix_badchars(string) + string = Logger.fix_badchars(string) sys.stdout.write('{}\n'.format(string)) def out_err(string): '''to stderr no color''' - string = _fix_badchars(string) + string = Logger.fix_badchars(string) sys.stderr.write('{}\n'.format(string)) def debug(string): '''to stderr no color''' - string = _fix_badchars(string) + string = Logger.fix_badchars(string) sys.stderr.write('[DBG] {}\n'.format(string)) def info(string): '''to stdout in color''' - string = _fix_badchars(string) + string = Logger.fix_badchars(string) s = '{}{}{}'.format(Logger.MAGENTA, string, Logger.RESET) sys.stdout.write('{}\n'.format(s)) def err(string): '''to stderr in RED''' - string = _fix_badchars(string) + string = Logger.fix_badchars(string) s = '{}{}{}'.format(Logger.RED, string, Logger.RESET) sys.stderr.write('{}\n'.format(s)) def progr(string): '''print progress''' - string = _fix_badchars(string) + string = Logger.fix_badchars(string) sys.stderr.write('{}\r'.format(string)) sys.stderr.flush() def bold(string): '''make it bold''' - string = _fix_badchars(string) + string = Logger.fix_badchars(string) return '{}{}{}'.format(Logger.BOLD, string, Logger.RESET) def flog(path, string, append=True): - string = _fix_badchars(string) + string = Logger.fix_badchars(string) mode = 'w' if append: mode = 'a' @@ -131,5 +134,3 @@ class Logger: f.write(string) -def _fix_badchars(line): - return line.encode('utf-8', 'ignore').decode('utf-8')