2016-08-24 21:20:57 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
A simple script to parse and output logs from a poezio logfile
|
|
|
|
"""
|
|
|
|
|
2021-03-17 17:21:21 +00:00
|
|
|
import argparse
|
2016-08-24 21:20:57 +00:00
|
|
|
from functools import singledispatch
|
2021-03-17 17:21:21 +00:00
|
|
|
from typing import List, Optional, IO
|
2016-08-24 21:20:57 +00:00
|
|
|
from poezio import poopt
|
2021-03-17 17:21:21 +00:00
|
|
|
from poezio.logger import LogInfo, LogMessage, parse_log_line
|
2016-08-24 21:20:57 +00:00
|
|
|
|
|
|
|
INFO_COLOR = '\033[35;2m'
|
|
|
|
NICK_COLOR = '\033[36;2m'
|
|
|
|
NO_COLOR = '\033[0m'
|
|
|
|
TIME_COLOR = '\033[33;2m'
|
|
|
|
|
|
|
|
SHOW_INFO = True
|
|
|
|
SHOW_TIME = True
|
|
|
|
|
2021-03-17 17:21:21 +00:00
|
|
|
|
2016-08-24 21:20:57 +00:00
|
|
|
@singledispatch
|
2021-03-17 17:21:21 +00:00
|
|
|
def print_log(log_object: LogMessage,
|
|
|
|
additional_lines: Optional[List[str]] = None):
|
2016-08-24 21:20:57 +00:00
|
|
|
time = log_object.time.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
|
|
|
nick = log_object.nick
|
2021-03-17 17:21:21 +00:00
|
|
|
offset = ((
|
|
|
|
(poopt.wcswidth(time) + 1) if SHOW_TIME else 0)
|
|
|
|
+ 2 + poopt.wcswidth(nick)
|
|
|
|
)
|
2016-08-24 21:20:57 +00:00
|
|
|
pad = ' ' * offset
|
|
|
|
|
|
|
|
if additional_lines:
|
|
|
|
more = ''.join([(pad + line) for line in additional_lines])
|
|
|
|
else:
|
|
|
|
more = ''
|
|
|
|
|
|
|
|
if SHOW_TIME:
|
2021-03-17 17:21:21 +00:00
|
|
|
print(
|
|
|
|
f'{TIME_COLOR}{time}{NO_COLOR} {NICK_COLOR}{nick}{NO_COLOR}> '
|
|
|
|
f'{log_object.text}\n' + more,
|
|
|
|
end='',
|
|
|
|
)
|
2016-08-24 21:20:57 +00:00
|
|
|
else:
|
2021-03-17 17:21:21 +00:00
|
|
|
print(
|
|
|
|
f'{NICK_COLOR}{nick}{NO_COLOR}> '
|
|
|
|
f'{log_object.text}\n' + more,
|
|
|
|
end='',
|
|
|
|
)
|
|
|
|
|
2016-08-24 21:20:57 +00:00
|
|
|
|
|
|
|
@print_log.register(type(None))
|
2021-03-17 17:21:21 +00:00
|
|
|
def print_log_none(log_object, additional_lines=None):
|
2016-08-24 21:20:57 +00:00
|
|
|
return
|
|
|
|
|
2021-03-17 17:21:21 +00:00
|
|
|
|
2016-08-24 21:20:57 +00:00
|
|
|
@print_log.register(LogInfo)
|
2021-03-17 17:21:21 +00:00
|
|
|
def print_log_loginfo(log_object: LogInfo,
|
|
|
|
additional_lines: Optional[List[str]] = None):
|
|
|
|
if not SHOW_INFO:
|
|
|
|
return
|
2016-08-24 21:20:57 +00:00
|
|
|
time = log_object.time.strftime('%Y-%m-%d %H:%M:%S') + ' '
|
|
|
|
|
|
|
|
offset = (poopt.wcswidth(time) + 1) if SHOW_TIME else 0
|
|
|
|
pad = ' ' * offset
|
|
|
|
|
|
|
|
if additional_lines:
|
|
|
|
more = ''.join([(pad + line) for line in additional_lines])
|
|
|
|
else:
|
|
|
|
more = ''
|
|
|
|
|
|
|
|
if SHOW_TIME:
|
2021-03-17 17:21:21 +00:00
|
|
|
print(
|
|
|
|
f'{TIME_COLOR}{time}{NO_COLOR}{log_object.text}\n' + more,
|
|
|
|
end=''
|
|
|
|
)
|
2016-08-24 21:20:57 +00:00
|
|
|
else:
|
2021-03-17 17:21:21 +00:00
|
|
|
print(f'{INFO_COLOR}{log_object.text}\n' + more, end='')
|
2016-08-24 21:20:57 +00:00
|
|
|
|
2021-03-17 17:21:21 +00:00
|
|
|
|
|
|
|
def parse_messages(fd: IO[str]):
|
2016-08-24 21:20:57 +00:00
|
|
|
in_text = False
|
2021-03-17 17:21:21 +00:00
|
|
|
more_lines: List[str] = []
|
2016-08-24 21:20:57 +00:00
|
|
|
current_log = None
|
|
|
|
for line in fd:
|
|
|
|
if in_text and not line.startswith(' '):
|
|
|
|
print_log(current_log, more_lines)
|
|
|
|
more_lines = []
|
|
|
|
in_text = False
|
|
|
|
current_log = None
|
|
|
|
elif in_text:
|
|
|
|
more_lines.append(line[1:])
|
|
|
|
continue
|
2021-03-17 17:21:21 +00:00
|
|
|
current_log = parse_log_line(line, fd.name)
|
2016-08-24 21:20:57 +00:00
|
|
|
in_text = True
|
|
|
|
print_log(current_log, more_lines)
|
|
|
|
|
2021-03-17 17:21:21 +00:00
|
|
|
|
2016-08-24 21:20:57 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser('poezio_logs', description="""
|
|
|
|
Show the logs stored in poezio format in a more human-readable way.
|
|
|
|
""")
|
|
|
|
parser.add_argument('-i', '--hide-info', dest='hide_info',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='Hide info lines')
|
|
|
|
parser.add_argument('-t', '--hide-time', dest='hide_time',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='Hide timestamps')
|
|
|
|
parser.add_argument('-c', '--no-color', dest='no_color',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='Remove color')
|
|
|
|
parser.add_argument('log_file', type=argparse.FileType('r'))
|
|
|
|
result = parser.parse_args()
|
|
|
|
SHOW_INFO = not result.hide_info
|
|
|
|
SHOW_TIME = not result.hide_time
|
|
|
|
if result.no_color:
|
|
|
|
INFO_COLOR = ''
|
|
|
|
NICK_COLOR = ''
|
|
|
|
NO_COLOR = ''
|
|
|
|
TIME_COLOR = ''
|
|
|
|
|
|
|
|
parse_messages(result.log_file)
|