Skip to content

Commit c1736cc

Browse files
Add file_size() function to FileIOMessageWriter (#1367)
* Add file_size() function to FileIOMessageWriter * Modify help statement for -s to indicate blf behaves differently * Open max_container_size in blf writer to API via kwargs Co-authored-by: j-c-cook <[email protected]>
1 parent 73663b6 commit c1736cc

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

can/io/blf.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import datetime
1818
import time
1919
import logging
20-
from typing import List, BinaryIO, Generator, Union, Tuple, Optional, cast
20+
from typing import List, BinaryIO, Generator, Union, Tuple, Optional, cast, Any
2121

2222
from ..message import Message
2323
from ..util import len2dlc, dlc2len, channel2int
@@ -370,6 +370,8 @@ def __init__(
370370
append: bool = False,
371371
channel: int = 1,
372372
compression_level: int = -1,
373+
*args: Any,
374+
**kwargs: Any
373375
) -> None:
374376
"""
375377
:param file: a path-like object or as file-like object to write to
@@ -400,6 +402,9 @@ def __init__(
400402
self.compression_level = compression_level
401403
self._buffer: List[bytes] = []
402404
self._buffer_size = 0
405+
# If max container size is located in kwargs, then update the instance
406+
if kwargs.get("max_container_size", False):
407+
self.max_container_size = kwargs["max_container_size"]
403408
if append:
404409
# Parse file header
405410
data = self.file.read(FILE_HEADER_STRUCT.size)
@@ -566,6 +571,10 @@ def _flush(self):
566571
self.uncompressed_size += LOG_CONTAINER_STRUCT.size
567572
self.uncompressed_size += len(uncompressed_data)
568573

574+
def file_size(self) -> int:
575+
"""Return an estimate of the current file size in bytes."""
576+
return self.file.tell() + self._buffer_size
577+
569578
def stop(self):
570579
"""Stops logging and closes the file."""
571580
self._flush()

can/io/generic.py

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ def __init__(self, file: can.typechecking.AcceptedIOType, mode: str = "wt") -> N
8989

9090
super().__init__(file, mode)
9191

92+
def file_size(self) -> int:
93+
"""Return an estimate of the current file size in bytes."""
94+
return self.file.tell()
95+
9296

9397
# pylint: disable=too-few-public-methods
9498
class MessageReader(BaseIOHandler, Iterable[can.Message], metaclass=ABCMeta):

can/io/logger.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def should_rollover(self, msg: Message) -> bool:
326326
if self.max_bytes <= 0:
327327
return False
328328

329-
if self.writer.file.tell() >= self.max_bytes:
329+
if self.writer.file_size() >= self.max_bytes:
330330
return True
331331

332332
return False

can/logger.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ def main() -> None:
191191
"--file_size",
192192
dest="file_size",
193193
type=int,
194-
help="Maximum file size in bytes. Rotate log file when size threshold "
195-
"is reached.",
194+
help="Maximum file size in bytes (or for the case of blf, maximum "
195+
"buffer size before compression and flush to file). Rotate log "
196+
"file when size threshold is reached.",
196197
default=None,
197198
)
198199

0 commit comments

Comments
 (0)