From ecd436f9268b8da11bc3bab45f471643f68b55ae Mon Sep 17 00:00:00 2001 From: Robin Clark Date: Wed, 4 Dec 2024 14:07:00 +0000 Subject: [PATCH] timestamping suitable for terminal traces --- time_stamp.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 time_stamp.py diff --git a/time_stamp.py b/time_stamp.py new file mode 100644 index 0000000..2c922b9 --- /dev/null +++ b/time_stamp.py @@ -0,0 +1,20 @@ + + +# Time stamp CAN messages from the serial stream. + +import sys +from datetime import datetime +import io + +def add_timestamp_to_lines(): + # Ensure stdin is read with a fallback encoding + with io.TextIOWrapper(sys.stdin.buffer, encoding='ISO8859-1', errors='replace') as stdin: + for line in stdin: + if 'CANMSG' in line or 'CMQ' in line: + timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S.%f] ") + sys.stdout.write(timestamp + line) + #else: + #sys.stdout.write(line) + +if __name__ == "__main__": + add_timestamp_to_lines()