reorder-logs.py 924 B

1234567891011121314151617181920212223242526272829
  1. import datetime as dt
  2. def isotime_to_ms(isotime):
  3. ms = dt.timedelta(microseconds=1)
  4. time = dt.time.fromisoformat(isotime)
  5. ms_time = (dt.datetime.combine(dt.date.min, time) - dt.datetime.min) / ms
  6. return ms_time
  7. def line_time(line):
  8. return isotime_to_ms(line.split()[1])
  9. lines = []
  10. filenames = {"Client": "/tmp/a.txt", "Server": "/tmp/b.txt"}
  11. for label, filename in filenames.items():
  12. with open(filename) as file:
  13. file_lines = file.read().split("\n")
  14. # Cleanup a bit
  15. file_lines = [line for line in file_lines if line and line[0].isdigit()]
  16. # Attach the label to each line
  17. file_lines = ["%s: %s" % (label, line) for line in file_lines]
  18. lines.extend(file_lines)
  19. lines.sort(key=line_time)
  20. for line in lines:
  21. # Now remove timestamps and other info we don't need
  22. line = line.split()
  23. line = line[0] + " " + " ".join(line[4:])
  24. print(line)