openpilot is an open source driver assistance system. openpilot performs the functions of Automated Lane Centering and Adaptive Cruise Control for over 200 supported car makes and models.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

#!/usr/bin/env python3
5 years ago
import os
import sys
import bz2
import urllib.parse
5 years ago
import capnp
import warnings
from typing import Iterable, Iterator
5 years ago
from cereal import log as capnp_log
from openpilot.tools.lib.filereader import FileReader
5 years ago
LogIterable = Iterable[capnp._DynamicStructReader]
class LogReader:
def __init__(self, fn, canonicalize=True, only_union_types=False, sort_by_time=False, dat=None):
self.data_version = None
self._only_union_types = only_union_types
ext = None
if not dat:
_, ext = os.path.splitext(urllib.parse.urlparse(fn).path)
if ext not in ('', '.bz2'):
# old rlogs weren't bz2 compressed
raise Exception(f"unknown extension {ext}")
5 years ago
with FileReader(fn) as f:
dat = f.read()
if ext == ".bz2" or dat.startswith(b'BZh9'):
dat = bz2.decompress(dat)
ents = capnp_log.Event.read_multiple_bytes(dat)
5 years ago
_ents = []
try:
for e in ents:
_ents.append(e)
except capnp.KjException:
warnings.warn("Corrupted events detected", RuntimeWarning, stacklevel=1)
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]
5 years ago
@classmethod
def from_bytes(cls, dat):
return cls("", dat=dat)
def __iter__(self) -> Iterator[capnp._DynamicStructReader]:
5 years ago
for ent in self._ents:
if self._only_union_types:
5 years ago
try:
ent.which()
yield ent
except capnp.lib.capnp.KjException:
pass
else:
yield ent
5 years ago
if __name__ == "__main__":
import codecs
# capnproto <= 0.8.0 throws errors converting byte data to string
# below line catches those errors and replaces the bytes with \x__
codecs.register_error("strict", codecs.backslashreplace_errors)
5 years ago
log_path = sys.argv[1]
lr = LogReader(log_path, sort_by_time=True)
5 years ago
for msg in lr:
print(msg)