Skip to content

Add file_size() function to FileIOMessageWriter #1367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion can/io/blf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions can/io/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion can/io/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions can/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down