2
1
mirror of https://github.com/deadc0de6/catcli synced 2024-11-11 13:11:00 +00:00

refactor logging

This commit is contained in:
deadc0de6 2022-01-14 10:39:24 +01:00
parent 38bc83d3b9
commit f181f33753

View File

@ -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')