diff --git a/can/io/blf.py b/can/io/blf.py index efeba9488..911177d20 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -17,7 +17,7 @@ import datetime import time import logging -from typing import List, BinaryIO, Generator, Union, Tuple, Optional, cast +from typing import List, BinaryIO, Generator, Union, Tuple, Optional, cast, Any from ..message import Message from ..util import len2dlc, dlc2len, channel2int @@ -370,6 +370,8 @@ def __init__( append: bool = False, channel: int = 1, compression_level: int = -1, + *args: Any, + **kwargs: Any ) -> None: """ :param file: a path-like object or as file-like object to write to @@ -400,6 +402,9 @@ def __init__( self.compression_level = compression_level self._buffer: List[bytes] = [] self._buffer_size = 0 + # If max container size is located in kwargs, then update the instance + if kwargs.get("max_container_size", False): + self.max_container_size = kwargs["max_container_size"] if append: # Parse file header data = self.file.read(FILE_HEADER_STRUCT.size) @@ -566,6 +571,10 @@ def _flush(self): self.uncompressed_size += LOG_CONTAINER_STRUCT.size self.uncompressed_size += len(uncompressed_data) + def file_size(self) -> int: + """Return an estimate of the current file size in bytes.""" + return self.file.tell() + self._buffer_size + def stop(self): """Stops logging and closes the file.""" self._flush() diff --git a/can/io/generic.py b/can/io/generic.py index b45e4dd1f..acdf367d6 100644 --- a/can/io/generic.py +++ b/can/io/generic.py @@ -89,6 +89,10 @@ def __init__(self, file: can.typechecking.AcceptedIOType, mode: str = "wt") -> N super().__init__(file, mode) + def file_size(self) -> int: + """Return an estimate of the current file size in bytes.""" + return self.file.tell() + # pylint: disable=too-few-public-methods class MessageReader(BaseIOHandler, Iterable[can.Message], metaclass=ABCMeta): diff --git a/can/io/logger.py b/can/io/logger.py index 5e46f6dc2..071eeb300 100644 --- a/can/io/logger.py +++ b/can/io/logger.py @@ -326,7 +326,7 @@ def should_rollover(self, msg: Message) -> bool: if self.max_bytes <= 0: return False - if self.writer.file.tell() >= self.max_bytes: + if self.writer.file_size() >= self.max_bytes: return True return False diff --git a/can/logger.py b/can/logger.py index 209c1381c..dbf78e408 100644 --- a/can/logger.py +++ b/can/logger.py @@ -191,8 +191,9 @@ def main() -> None: "--file_size", dest="file_size", type=int, - help="Maximum file size in bytes. Rotate log file when size threshold " - "is reached.", + help="Maximum file size in bytes (or for the case of blf, maximum " + "buffer size before compression and flush to file). Rotate log " + "file when size threshold is reached.", default=None, )