timestamping suitable for terminal traces

This commit is contained in:
Robin Clark 2024-12-04 14:07:00 +00:00
parent 7859bfab0d
commit ecd436f926

20
time_stamp.py Normal file
View File

@ -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()