forked from datalad/datalad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtime
executable file
·57 lines (48 loc) · 1.5 KB
/
dtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
#emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
#ex: set sts=4 ts=4 sw=4 noet:
"""Little helper to annotate logfile with difference between timestamps in consecutive lines
It prints time estimated from previous line on the previous line, with 0 always printed as well
so it becomes possible to sort -n the output to see from what line it took longest to the next
"""
import sys
import re
from datetime import datetime
reg = re.compile('^\d{4}-\d{2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2},\d{1,3}')
prevt = None
maxl = 0
prevl = None
if len(sys.argv) <= 1:
in_ = sys.stdin
else:
in_ = open(sys.argv[1])
trailer = []
for l in in_:
res = reg.search(l)
dtstr = ''
if res:
end = res.end()
t = datetime.strptime(l[:end], '%Y-%m-%d %H:%M:%S,%f')
if prevt is not None:
dt = t - prevt
ms = dt.microseconds // 1000 + dt.seconds*1000
dtstr = ("%5d" % ms if ms else ' 0')
maxl = max(maxl, len(dtstr))
dtstr = '%%%ds' % maxl % dtstr
prevl = "%s %s" % (dtstr, prevl)
prevt = t
else:
# no timestamp -- add to the trailer
trailer.append(l)
continue
if prevl is not None:
for l_ in trailer:
sys.stdout.write(" - " + l_)
trailer = []
sys.stdout.write(prevl)
if res:
prevl = l
if prevl:
sys.stdout.write("----- " + prevl)
for l_ in trailer:
sys.stdout.write(" - " + l_)