diff options
Diffstat (limited to 'poezio/logger.py')
-rw-r--r-- | poezio/logger.py | 48 |
1 files changed, 15 insertions, 33 deletions
diff --git a/poezio/logger.py b/poezio/logger.py index 6e4a6ff0..29eaad32 100644 --- a/poezio/logger.py +++ b/poezio/logger.py @@ -3,7 +3,7 @@ # This file is part of Poezio. # # Poezio is free software: you can redistribute it and/or modify -# it under the terms of the zlib license. See the COPYING file. +# it under the terms of the GPL-3.0+ license. See the COPYING file. """ The logger module that handles logging of the poezio conversations and roster changes @@ -157,11 +157,11 @@ class Logger: def close(self, jid: str) -> None: """Close the log file for a JID.""" - jid = str(jid).replace('/', '\\') - if jid in self._fds: - self._fds[jid].close() + jidstr = str(jid).replace('/', '\\') + if jidstr in self._fds: + self._fds[jidstr].close() log.debug('Log file for %s closed.', jid) - del self._fds[jid] + del self._fds[jidstr] def reload_all(self) -> None: """Close and reload all the file handles (on SIGHUP)""" @@ -184,7 +184,7 @@ class Logger: self._check_and_create_log_dir(room) log.debug('Log handle for %s re-created', room) - def _check_and_create_log_dir(self, jid: str, + def _check_and_create_log_dir(self, jid: Union[str, JID], open_fd: bool = True) -> Optional[IO[str]]: """ Check that the directory where we want to log the messages @@ -196,6 +196,8 @@ class Logger: """ if not config.get_by_tabname('use_log', JID(jid)): return None + # POSIX filesystems don't support / in filename, so we replace it with a backslash + jid = str(jid).replace('/', '\\') try: self.log_dir.mkdir(parents=True, exist_ok=True) except OSError: @@ -205,7 +207,7 @@ class Logger: return None if not open_fd: return None - filename = self.log_dir / jid + filename = self.get_file_path(jid) try: fd = filename.open('a', encoding='utf-8') self._fds[jid] = fd @@ -251,18 +253,18 @@ class Logger: :param force: Bypass the buffered fd check :returns: True if no error was encountered """ - jid = str(jid).replace('/', '\\') - if jid in self._fds.keys(): - fd = self._fds[jid] + jidstr = str(jid).replace('/', '\\') + if jidstr in self._fds.keys(): + fd = self._fds[jidstr] else: option_fd = self._check_and_create_log_dir(jid) if option_fd is None: return True fd = option_fd - filename = self.log_dir / jid + filename = self.get_file_path(jid) try: - if not force and self._busy_fds.get(jid): - self._buffered_fds[jid].append(logged_msg) + if not force and self._busy_fds.get(jidstr): + self._buffered_fds[jidstr].append(logged_msg) return True fd.write(logged_msg) except OSError: @@ -398,26 +400,6 @@ def iterate_messages_reverse(filepath: Path) -> Generator[LogDict, None, None]: pass -def _get_lines_from_fd(fd: IO[Any], nb: int = 10) -> List[str]: - """ - Get the last log lines from a fileno with mmap - - :param fd: File descriptor on the log file - :param nb: number of messages to fetch - :returns: A list of message lines - """ - with mmap.mmap(fd.fileno(), 0, prot=mmap.PROT_READ) as m: - # 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 != 0 and count < nb - 1: - count += 1 - pos = m.rfind(b"\nM", 0, pos) + 1 - lines = m[pos:].decode(errors='replace').splitlines() - return lines - - def parse_log_lines(lines: List[str], jid: str = '') -> List[LogDict]: """ Parse raw log lines into poezio log objects |