Make log parsing a little better

Separate log items in two different classes instead of just trying the
length of a tuple
This commit is contained in:
mathieui 2016-08-24 23:14:34 +02:00
parent ec04070611
commit 4f942bd48f

View file

@ -27,20 +27,36 @@ log = logging.getLogger(__name__)
from poezio.config import LOG_DIR as log_dir from poezio.config import LOG_DIR as log_dir
message_log_re = re.compile(r'MR (\d{4})(\d{2})(\d{2})T' MESSAGE_LOG_RE = re.compile(r'MR (\d{4})(\d{2})(\d{2})T'
r'(\d{2}):(\d{2}):(\d{2})Z ' r'(\d{2}):(\d{2}):(\d{2})Z '
r'(\d+) <([^ ]+)>  (.*)') r'(\d+) <([^ ]+)>  (.*)')
info_log_re = re.compile(r'MI (\d{4})(\d{2})(\d{2})T' INFO_LOG_RE = re.compile(r'MI (\d{4})(\d{2})(\d{2})T'
r'(\d{2}):(\d{2}):(\d{2})Z ' r'(\d{2}):(\d{2}):(\d{2})Z '
r'(\d+) (.*)') r'(\d+) (.*)')
def _parse_message_line(msg): class LogItem:
if re.match(message_log_re, msg): def __init__(self, year, month, day, hour, minute, second, nb_lines, message):
return [i for i in re.split(message_log_re, msg) if i] self.time = datetime(int(year), int(month), int(day), int(hour),
if re.match(info_log_re, msg): int(minute), int(second))
return [i for i in re.split(info_log_re, msg) if i] self.nb_lines = int(nb_lines)
return None self.text = message
class LogInfo(LogItem):
def __init__(self, *args):
LogItem.__init__(self, *args)
class LogMessage(LogItem):
def __init__(self, year, month, day, hour, minute, seconds, nb_lines, nick, message):
LogItem.__init__(self, year, month, day, hour, minute, seconds,
nb_lines, message)
self.nick = nick
def parse_message_line(msg):
if re.match(MESSAGE_LOG_RE, msg):
return LogMessage(*[i for i in re.split(MESSAGE_LOG_RE, msg) if i])
if re.match(INFO_LOG_RE, msg):
return LogInfo(*[i for i in re.split(INFO_LOG_RE, msg) if i])
return None
class Logger(object): class Logger(object):
""" """
@ -168,21 +184,20 @@ class Logger(object):
idx += 1 idx += 1
log.debug('fail?') log.debug('fail?')
continue continue
tup = _parse_message_line(lines[idx]) log_item = parse_message_line(lines[idx])
idx += 1 idx += 1
if not tup or len(tup) not in (8, 9): # skip if not isinstance(log_item, LogItem):
log.debug('format? %s', tup) log.debug('wrong log format? %s', tup)
continue continue
time = [int(i) for i in tup[:6]]
message = {'lines': [], message = {'lines': [],
'history': True, 'history': True,
'time': common.get_local_time(datetime(*time))} 'time': common.get_local_time(log_item.time)}
size = int(tup[6]) size = log_item.nb_lines
if len(tup) == 8: #info line if isinstance(log_item, LogInfo):
message['lines'].append(color + tup[7]) message['lines'].append(color + log_item.text)
else: # message line elif isinstance(log_item, LogMessage):
message['nickname'] = tup[7] message['nickname'] = log_item.nick
message['lines'].append(color + tup[8]) message['lines'].append(color + log_item.text)
while size != 0 and idx < len(lines): while size != 0 and idx < len(lines):
message['lines'].append(lines[idx][1:]) message['lines'].append(lines[idx][1:])
size -= 1 size -= 1