Sometimes it is beneficial to attach real time stamps to console output, for instance when there is a need to correlate two independently captured logs.
This patch adds another command line option (-r,--realtime), when given the generated log is printed with second's resolution real time timestamps.
Signed-off-by: Vadim Bendebury vbendeb@chromium.org --- scripts/readserial.py | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-)
diff --git a/scripts/readserial.py b/scripts/readserial.py index a7383e8..dc294eb 100755 --- a/scripts/readserial.py +++ b/scripts/readserial.py @@ -8,7 +8,7 @@ # Usage: # scripts/readserial.py /dev/ttyUSB0 115200
-import sys, os, time, select, optparse +import datetime, sys, os, time, select, optparse
from python23compat import as_bytes
@@ -54,8 +54,10 @@ def calibrateserialread(infile, byteadjust): totalchars += len(d) lasttime = curtime
-def readserial(infile, logfile, byteadjust): +def readserial(infile, logfile, byteadjust, real_time): lasttime = 0 + isnewline = 0 + charcount = 0 while 1: # Read data try: @@ -74,25 +76,29 @@ def readserial(infile, logfile, byteadjust): return 0 datatime = time.time()
- datatime -= len(d) * byteadjust + if not real_time: + datatime -= len(d) * byteadjust
- # Reset start time if no data for some time - if datatime - lasttime > RESTARTINTERVAL: - starttime = datatime - charcount = 0 - isnewline = 1 - msg = "\n\n======= %s (adjust=%.1fus)\n" % ( - time.asctime(time.localtime(datatime)), byteadjust * 1000000) - sys.stdout.write(msg) - logfile.write(as_bytes(msg)) - lasttime = datatime + # Reset start time if no data for some time + if datatime - lasttime > RESTARTINTERVAL: + starttime = datatime + charcount = 0 + isnewline = 1 + msg = "\n\n======= %s (adjust=%.1fus)\n" % ( + time.asctime(time.localtime(datatime)), byteadjust * 1000000) + sys.stdout.write(msg) + logfile.write(as_bytes(msg)) + lasttime = datatime
# Translate unprintable chars; add timestamps out = as_bytes("") for c in d: if isnewline: - delta = datatime - starttime - (charcount * byteadjust) - out += "%06.3f: " % delta + if real_time: + out += datetime.datetime.now().strftime('%H:%M:%S: ') + else: + delta = datatime - starttime - (charcount * byteadjust) + out += "%06.3f: " % delta isnewline = 0 oc = ord(c) charcount += 1 @@ -138,6 +144,10 @@ def main(): opts.add_option("-t", "--time", type="float", dest="time", default=None, help="time to write one byte on serial port (in us)") + opts.add_option("-r", "--realtime", + action="store_true", dest="realtime", default=False, + help="print real time timestamps instead of deltas") + options, args = opts.parse_args() serialport = 0 baud = 115200 @@ -176,12 +186,12 @@ Or: apt-get install python-serial logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log") f = open(logname, 'wb') if options.serial: - readserial(ser, f, byteadjust) + readserial(ser, f, byteadjust, options.realtime) else: # Read from a pipe while 1: ser = os.fdopen(os.open(serialport, os.O_RDONLY|os.O_NONBLOCK), 'rb') - res = readserial(ser, f, byteadjust) + res = readserial(ser, f, byteadjust, options.realtime) ser.close() if res < 0: break