From 57af021eba6218186a30af684e0b96fbbcac209d Mon Sep 17 00:00:00 2001 From: j-c-cook Date: Sat, 6 Aug 2022 11:31:49 -0500 Subject: [PATCH 1/3] Write 3 ms to ascii for CANoe and fix append error The ascii format currently is not setup to append. I recenly added in **options to the logger script. **options are only required for the rolling logger. --- can/io/asc.py | 11 +++++++++-- can/logger.py | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/can/io/asc.py b/can/io/asc.py index 3a320f007..9e66e0397 100644 --- a/can/io/asc.py +++ b/can/io/asc.py @@ -344,9 +344,8 @@ class ASCWriter(FileIOMessageWriter): "{bit_timing_conf_ext_data:>8}", ] ) + # Use trigger start time to replace file start time FORMAT_START_OF_FILE_DATE = "%a %b %d %I:%M:%S.%f %p %Y" - FORMAT_DATE = "%a %b %d %I:%M:%S.{} %p %Y" - FORMAT_EVENT = "{timestamp: 9.6f} {message}\n" def __init__( self, @@ -366,6 +365,10 @@ def __init__( # write start of file header now = datetime.now().strftime(self.FORMAT_START_OF_FILE_DATE) + # Note: CANoe requires that the microsecond field only have 3 digits + idx = now.index('.') # Find the index in the string of the decimal + # Keep decimal and first three ms digits (4), remove remaining digits + now = now.replace(now[idx+4:now[idx:].index(' ') + idx], '') self.file.write(f"date {now}\n") self.file.write("base hex timestamps absolute\n") self.file.write("internal events logged\n") @@ -400,6 +403,10 @@ def log_event(self, message: str, timestamp: Optional[float] = None) -> None: formatted_date = time.strftime( self.FORMAT_DATE.format(mlsec), time.localtime(self.last_timestamp) ) + # changed 2022-08-04, moved file start header here to be one. + self.file.write(f"date {formatted_date}\n") + self.file.write("base hex timestamps absolute\n") + self.file.write("internal events logged\n") self.file.write(f"Begin Triggerblock {formatted_date}\n") self.header_written = True self.log_event("Start of measurement") # caution: this is a recursive call! diff --git a/can/logger.py b/can/logger.py index 5aff5e4e1..cc16a799d 100644 --- a/can/logger.py +++ b/can/logger.py @@ -213,13 +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: + options = {"append": results.append} logger = SizedRotatingLogger( base_filename=results.log_file, max_bytes=results.file_size, **options ) else: - logger = Logger(filename=results.log_file, **options) # type: ignore + logger = Logger(filename=results.log_file) # type: ignore try: while True: From 9c088e20f97223b89b2ed9587679afdc8db8b7ca Mon Sep 17 00:00:00 2001 From: j-c-cook Date: Sat, 6 Aug 2022 11:40:23 -0500 Subject: [PATCH 2/3] Black and mypy fixes --- can/io/asc.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/can/io/asc.py b/can/io/asc.py index 9e66e0397..1ac3e9821 100644 --- a/can/io/asc.py +++ b/can/io/asc.py @@ -346,6 +346,8 @@ class ASCWriter(FileIOMessageWriter): ) # Use trigger start time to replace file start time FORMAT_START_OF_FILE_DATE = "%a %b %d %I:%M:%S.%f %p %Y" + FORMAT_DATE = "%a %b %d %I:%M:%S.{} %p %Y" + FORMAT_EVENT = "{timestamp: 9.6f} {message}\n" def __init__( self, @@ -366,9 +368,9 @@ def __init__( # write start of file header now = datetime.now().strftime(self.FORMAT_START_OF_FILE_DATE) # Note: CANoe requires that the microsecond field only have 3 digits - idx = now.index('.') # Find the index in the string of the decimal + idx = now.index(".") # Find the index in the string of the decimal # Keep decimal and first three ms digits (4), remove remaining digits - now = now.replace(now[idx+4:now[idx:].index(' ') + idx], '') + now = now.replace(now[idx + 4 : now[idx:].index(" ") + idx], "") self.file.write(f"date {now}\n") self.file.write("base hex timestamps absolute\n") self.file.write("internal events logged\n") @@ -406,7 +408,7 @@ def log_event(self, message: str, timestamp: Optional[float] = None) -> None: # changed 2022-08-04, moved file start header here to be one. self.file.write(f"date {formatted_date}\n") self.file.write("base hex timestamps absolute\n") - self.file.write("internal events logged\n") + self.file.write("internal events logged\n") self.file.write(f"Begin Triggerblock {formatted_date}\n") self.header_written = True self.log_event("Start of measurement") # caution: this is a recursive call! From 44b0489224a95613e79a7d2ee8cb3513bfc36fac Mon Sep 17 00:00:00 2001 From: j-c-cook Date: Thu, 11 Aug 2022 14:33:02 -0500 Subject: [PATCH 3/3] Revert "Write 3 ms to ascii for CANoe and fix append error" This reverts commit 57af021eba6218186a30af684e0b96fbbcac209d. --- can/io/asc.py | 5 ----- can/logger.py | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/can/io/asc.py b/can/io/asc.py index 6a81791c2..ba2214c71 100644 --- a/can/io/asc.py +++ b/can/io/asc.py @@ -344,7 +344,6 @@ class ASCWriter(FileIOMessageWriter): "{bit_timing_conf_ext_data:>8}", ] ) - # Use trigger start time to replace file start time FORMAT_START_OF_FILE_DATE = "%a %b %d %I:%M:%S.%f %p %Y" FORMAT_DATE = "%a %b %d %I:%M:%S.{} %p %Y" FORMAT_EVENT = "{timestamp: 9.6f} {message}\n" @@ -411,10 +410,6 @@ def log_event(self, message: str, timestamp: Optional[float] = None) -> None: formatted_date = time.strftime( self.FORMAT_DATE.format(mlsec), time.localtime(self.last_timestamp) ) - # changed 2022-08-04, moved file start header here to be one. - self.file.write(f"date {formatted_date}\n") - self.file.write("base hex timestamps absolute\n") - self.file.write("internal events logged\n") self.file.write(f"Begin Triggerblock {formatted_date}\n") self.header_written = True self.log_event("Start of measurement") # caution: this is a recursive call! diff --git a/can/logger.py b/can/logger.py index 979bbe5d6..dbf78e408 100644 --- a/can/logger.py +++ b/can/logger.py @@ -235,13 +235,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: - options = {"append": results.append} logger = SizedRotatingLogger( 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: