| 1 |
#!/usr/bin/env python3 |
| 2 |
#emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*- |
| 3 |
#ex: set sts=4 ts=4 sw=4 noet: |
| 4 |
"""Little helper to annotate logfile with difference between timestamps in consecutive lines |
| 5 |
|
| 6 |
It prints time estimated from previous line on the previous line, with 0 always printed as well |
| 7 |
so it becomes possible to sort -n the output to see from what line it took longest to the next |
| 8 |
""" |
| 9 |
|
| 10 |
import re |
| 11 |
import sys |
| 12 |
from datetime import datetime |
| 13 |
|
| 14 |
reg = re.compile('^\d{4}-\d{2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2},\d{1,3}') |
| 15 |
prevt = None |
| 16 |
maxl = 0 |
| 17 |
prevl = None |
| 18 |
warned_negative = False |
| 19 |
|
| 20 |
if len(sys.argv) <= 1: |
| 21 |
in_ = sys.stdin |
| 22 |
else: |
| 23 |
in_ = open(sys.argv[1]) |
| 24 |
|
| 25 |
trailer = [] |
| 26 |
for l in in_: |
| 27 |
res = reg.search(l) |
| 28 |
dtstr = '' |
| 29 |
if res: |
| 30 |
end = res.end() |
| 31 |
t = datetime.strptime(l[:end], '%Y-%m-%d %H:%M:%S,%f') |
| 32 |
if prevt is not None: |
| 33 |
dt = t - prevt |
| 34 |
secs = dt.total_seconds() |
| 35 |
# negative dt has very weird form of -1 day + huge number of seconds |
| 36 |
if secs < 0: |
| 37 |
if not warned_negative: |
| 38 |
sys.stderr.write(f"Negative delta of {secs} seconds detected. Might be due to threading etc\n") |
| 39 |
warned_negative = True |
| 40 |
|
| 41 |
ms = secs * 1000 |
| 42 |
dtstr = ("%5d" % ms if ms else ' 0') |
| 43 |
|
| 44 |
maxl = max(maxl, len(dtstr)) |
| 45 |
dtstr = '%%%ds' % maxl % dtstr |
| 46 |
prevl = "%s %s" % (dtstr, prevl) |
| 47 |
prevt = t |
| 48 |
else: |
| 49 |
# no timestamp -- add to the trailer |
| 50 |
trailer.append(l) |
| 51 |
continue |
| 52 |
|
| 53 |
if prevl is not None: |
| 54 |
for l_ in trailer: |
| 55 |
sys.stdout.write(" - " + l_) |
| 56 |
trailer = [] |
| 57 |
sys.stdout.write(prevl) |
| 58 |
|
| 59 |
if res: |
| 60 |
prevl = l |
| 61 |
|
| 62 |
if prevl: |
| 63 |
sys.stdout.write("----- " + prevl) |
| 64 |
for l_ in trailer: |
| 65 |
sys.stdout.write(" - " + l_) |