Skip to content

Commit

Permalink
LogReader: add arg to sort by time (#23346)
Browse files Browse the repository at this point in the history
* logreader optional sort by time

* robust logreader sort by time option
  • Loading branch information
gregjhogan authored Jan 2, 2022
1 parent 2ca0925 commit 9992ea1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 6 additions & 5 deletions tools/lib/logreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# this is an iterator itself, and uses private variables from LogReader
class MultiLogIterator:
def __init__(self, log_paths, wraparound=False):
def __init__(self, log_paths, wraparound=False, sort_by_time=False):
self._log_paths = log_paths
self._wraparound = wraparound

Expand All @@ -22,11 +22,12 @@ def __init__(self, log_paths, wraparound=False):
self._idx = 0
self._log_readers = [None]*len(log_paths)
self.start_time = self._log_reader(self._first_log_idx)._ts[0]
self.sort_by_time = sort_by_time

def _log_reader(self, i):
if self._log_readers[i] is None and self._log_paths[i] is not None:
log_path = self._log_paths[i]
self._log_readers[i] = LogReader(log_path)
self._log_readers[i] = LogReader(log_path, sort_by_time=self.sort_by_time)

return self._log_readers[i]

Expand Down Expand Up @@ -75,7 +76,7 @@ def seek(self, ts):


class LogReader:
def __init__(self, fn, canonicalize=True, only_union_types=False):
def __init__(self, fn, canonicalize=True, only_union_types=False, sort_by_time=False):
data_version = None
_, ext = os.path.splitext(urllib.parse.urlparse(fn).path)
with FileReader(fn) as f:
Expand All @@ -90,7 +91,7 @@ def __init__(self, fn, canonicalize=True, only_union_types=False):
else:
raise Exception(f"unknown extension {ext}")

self._ents = list(ents)
self._ents = list(sorted(ents, key=lambda x: x.logMonoTime) if sort_by_time else ents)
self._ts = [x.logMonoTime for x in self._ents]
self.data_version = data_version
self._only_union_types = only_union_types
Expand All @@ -112,6 +113,6 @@ def __iter__(self):
# below line catches those errors and replaces the bytes with \x__
codecs.register_error("strict", codecs.backslashreplace_errors)
log_path = sys.argv[1]
lr = LogReader(log_path)
lr = LogReader(log_path, sort_by_time=True)
for msg in lr:
print(msg)
4 changes: 2 additions & 2 deletions tools/lib/robust_logreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class RobustLogReader(LogReader):
def __init__(self, fn, canonicalize=True, only_union_types=False): # pylint: disable=super-init-not-called
def __init__(self, fn, canonicalize=True, only_union_types=False, sort_by_time=False): # pylint: disable=super-init-not-called
data_version = None
_, ext = os.path.splitext(urllib.parse.urlparse(fn).path)
with FileReader(fn) as f:
Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(self, fn, canonicalize=True, only_union_types=False): # pylint: di
while True:
try:
ents = capnp_log.Event.read_multiple_bytes(dat)
self._ents = list(ents)
self._ents = list(sorted(ents, key=lambda x: x.logMonoTime) if sort_by_time else ents)
break
except capnp.lib.capnp.KjException:
if progress is None:
Expand Down

0 comments on commit 9992ea1

Please sign in to comment.