Commit 90ed5f5b authored by hydrargyrum's avatar hydrargyrum

log-ts-diff: parse log and replace timestamps with diff to previous timestamp

parent ef565f7d
......@@ -25,6 +25,7 @@ This repository hosts various small personal tools.
* json2table: pretty-print a JSON list of objects in an ASCII-art table
* keepassxprint: dump info and passwords from a KeePassX database
* log-snippet: parse compilation-log and show snippets of files with context
* log-ts-diff: parse log and replace timestamps with diff to previous timestamp
* morse: text from/to Morse code converter, and optional beep player
* morsehtml: HTML page with its background flashing a Morse code message
* moversleep: move incoming files from a dir into another
......
#!/usr/bin/env python3
# license: Do What the Fuck You Want to Public License v2 [http://wtfpl.net]
# parse log and replace timestamps with diff to previous timestamp
import datetime
from fileinput import input
import re
from dateutil.parser import parse as parse_base
from dateutil.relativedelta import relativedelta
def parse(line):
m = re.match(r'[\dT\s:,.Z+-]+', line)
if not m:
return {}
try:
ts, toks = parse_base(m[0], fuzzy_with_tokens=True)
except ValueError:
return {}
return {
'timestamp': ts,
'rest': (''.join(toks) + line[m.end():]).strip(),
}
def duration_to_str(duration):
attrs = {
'years': 'y',
'months': 'mo',
'days': 'd',
'hours': 'h',
'minutes': 'm',
}
parts = []
for attr in attrs:
factor = getattr(duration, attr)
if factor:
parts.append(f'{factor:+2d} {attr}')
if duration.microseconds:
secs = duration.seconds + duration.microseconds / 1_000_000
parts.append(f'{secs:+6.3f}s')
elif duration.seconds:
parts.append(f'{duration.seconds:+3d}')
if not parts:
parts.append('+0')
return ', '.join(parts)
def main():
old_time = None
for line in input():
line = line.rstrip()
info = parse(line.lstrip())
if not info:
print(line)
continue
time = info['timestamp']
if not old_time:
old_time = time
print(line)
continue
print(duration_to_str(relativedelta(time, old_time)), info['rest'])
old_time = time
if __name__ == '__main__':
main()
#!/usr/bin/env python3
import pathlib
import subprocess
output = subprocess.check_output(
[pathlib.Path(__file__).with_name('ts-diff.py')],
encoding='utf-8',
input='''
2020-11-09 19:24:08,582:FOO
EXTRA
2020-11-09 19:24:08,628:BAR
2020-11-09 19:24:08,351:BAZ
LINE
'''.strip()
)
assert output == '''
2020-11-09 19:24:08,582:FOO
EXTRA
+0.046s :BAR
-0.277s :BAZ
LINE
'''.lstrip()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment