diff options
Diffstat (limited to 'poezio/logger.py')
-rw-r--r-- | poezio/logger.py | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/poezio/logger.py b/poezio/logger.py index 7ac7ad7e..d43cc759 100644 --- a/poezio/logger.py +++ b/poezio/logger.py @@ -56,14 +56,14 @@ class LogMessage(LogItem): self.nick = nick -def parse_log_line(msg: str) -> Optional[LogItem]: +def parse_log_line(msg: str, jid: str) -> Optional[LogItem]: match = re.match(MESSAGE_LOG_RE, msg) if match: return LogMessage(*match.groups()) match = re.match(INFO_LOG_RE, msg) if match: return LogInfo(*match.groups()) - log.debug('Error while parsing "%s"', msg) + log.debug('Error while parsing %s’s logs: “%s”', jid, msg) return None @@ -169,14 +169,14 @@ class Logger: # do that efficiently, instead of seek()s and read()s which are costly. with fd: try: - lines = get_lines_from_fd(fd, nb=nb) + lines = _get_lines_from_fd(fd, nb=nb) except Exception: # file probably empty log.error( 'Unable to mmap the log file for (%s)', filename, exc_info=True) return None - return parse_log_lines(lines) + return parse_log_lines(lines, jid) def log_message(self, jid: str, @@ -290,26 +290,23 @@ def build_log_message(nick: str, return logged_msg + ''.join(' %s\n' % line for line in lines) -def get_lines_from_fd(fd: IO[Any], nb: int = 10) -> List[str]: +def _get_lines_from_fd(fd: IO[Any], nb: int = 10) -> List[str]: """ Get the last log lines from a fileno """ with mmap.mmap(fd.fileno(), 0, prot=mmap.PROT_READ) as m: - pos = m.rfind(b"\nM") # start of messages begin with MI or MR, - # after a \n + # start of messages begin with MI or MR, after a \n + pos = m.rfind(b"\nM") + 1 # number of message found so far count = 0 - while pos != -1 and count < nb - 1: + while pos != 0 and count < nb - 1: count += 1 - pos = m.rfind(b"\nM", 0, pos) - if pos == -1: # If we don't have enough lines in the file - pos = 1 # 1, because we do -1 just on the next line - # to get 0 (start of the file) - lines = m[pos - 1:].decode(errors='replace').splitlines() + pos = m.rfind(b"\nM", 0, pos) + 1 + lines = m[pos:].decode(errors='replace').splitlines() return lines -def parse_log_lines(lines: List[str]) -> List[Dict[str, Any]]: +def parse_log_lines(lines: List[str], jid: str) -> List[Dict[str, Any]]: """ Parse raw log lines into poezio log objects """ @@ -323,7 +320,7 @@ def parse_log_lines(lines: List[str]) -> List[Dict[str, Any]]: idx += 1 log.debug('fail?') continue - log_item = parse_log_line(lines[idx]) + log_item = parse_log_line(lines[idx], jid) idx += 1 if not isinstance(log_item, LogItem): log.debug('wrong log format? %s', log_item) |