Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogReader: add arg to sort by time #23346

Merged
merged 2 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we get rid of wraparound? think it was only used for unlogger

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!

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