Skip to content

Enhance can.logger to consider the append option #1327

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 14 commits into from
Jun 10, 2022
Merged
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
41 changes: 27 additions & 14 deletions can/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ def _create_base_argument_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"-c",
"--channel",
help='''Most backend interfaces require some sort of channel.
For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
With the socketcan interfaces valid channel examples include: "can0", "vcan0"''',
help=r"Most backend interfaces require some sort of channel. For "
r"example with the serial interface the channel might be a rfcomm"
r' device: "/dev/rfcomm0". With the socketcan interface valid '
r'channel examples include: "can0", "vcan0".',
)

parser.add_argument(
Expand All @@ -60,11 +61,10 @@ def _create_base_argument_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"extra_args",
nargs=argparse.REMAINDER,
help="""\
The remaining arguments will be used for the interface initialisation.
For example, `-i vector -c 1 --app-name=MyCanApp` is the equivalent to
opening the bus with `Bus('vector', channel=1, app_name='MyCanApp')`
""",
help=r"The remaining arguments will be used for the interface "
r"initialisation. For example, `-i vector -c 1 --app-name="
r"MyCanApp` is the equivalent to opening the bus with `Bus("
r"'vector', channel=1, app_name='MyCanApp')",
)


Expand All @@ -82,8 +82,10 @@ def _append_filter_argument(
*args,
"--filter",
help="R|Space separated CAN filters for the given CAN interface:"
"\n <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)"
"\n <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)"
"\n <can_id>:<can_mask> (matches when <received_can_id> & mask =="
" can_id & mask)"
"\n <can_id>~<can_mask> (matches when <received_can_id> & mask !="
" can_id & mask)"
"\nFx to show only frames with ID 0x100 to 0x103 and 0x200 to 0x20F:"
"\n python -m can.viewer -f 100:7FC 200:7F0"
"\nNote that the ID and mask are always interpreted as hex values",
Expand Down Expand Up @@ -141,7 +143,8 @@ def _parse_additonal_config(unknown_args):

def main() -> None:
parser = argparse.ArgumentParser(
description="Log CAN traffic, printing messages to stdout or to a given file.",
description="Log CAN traffic, printing messages to stdout or to a "
"given file.",
)

_create_base_argument_parser(parser)
Expand All @@ -154,12 +157,21 @@ def main() -> None:
default=None,
)

parser.add_argument(
"-a",
"--append",
dest="append",
help="Append to the log file if it already exists.",
action="store_true",
)

parser.add_argument(
"-s",
"--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. Rotate log file when size threshold "
"is reached.",
default=None,
)

Expand Down Expand Up @@ -201,12 +213,13 @@ def main() -> None:
print(f"Connected to {bus.__class__.__name__}: {bus.channel_info}")
print(f"Can Logger (Started on {datetime.now()})")

options = {"append": results.append}
if results.file_size:
logger = SizedRotatingLogger(
base_filename=results.log_file, max_bytes=results.file_size
base_filename=results.log_file, max_bytes=results.file_size, **options
)
else:
logger = Logger(filename=results.log_file) # type: ignore
logger = Logger(filename=results.log_file, **options) # type: ignore

try:
while True:
Expand Down