LogReader: live_logreader helpers (#31416)

live helper
old-commit-hash: 0a92c5bf966530335e804a8f240b10ae982bfcb2
This commit is contained in:
Justin Newberry
2024-02-12 15:58:25 -05:00
committed by GitHub
parent adcac1f207
commit fbf5ada69f
2 changed files with 35 additions and 20 deletions
+28 -1
View File
@@ -16,7 +16,8 @@ import warnings
from typing import Dict, Iterable, Iterator, List, Type
from urllib.parse import parse_qs, urlparse
from cereal import log as capnp_log
from cereal import log as capnp_log, messaging
from cereal.services import SERVICE_LIST
from openpilot.common.swaglog import cloudlog
from openpilot.tools.lib.comma_car_segments import get_url as get_comma_segments_url
from openpilot.tools.lib.openpilotci import get_url
@@ -26,6 +27,7 @@ from openpilot.tools.lib.route import Route, SegmentRange
LogMessage = Type[capnp._DynamicStructReader]
LogIterable = Iterable[LogMessage]
RawLogIterable = Iterable[bytes]
class _LogFileReader:
@@ -292,6 +294,31 @@ are uploaded or auto fallback to qlogs with '/a' selector at the end of the rout
return next(self.filter(msg_type), None)
ALL_SERVICES = list(SERVICE_LIST.keys())
def raw_live_logreader(services: List[str] = ALL_SERVICES, addr: str = '127.0.0.1') -> RawLogIterable:
if addr != "127.0.0.1":
os.environ["ZMQ"] = "1"
messaging.context = messaging.Context()
poller = messaging.Poller()
for m in services:
messaging.sub_sock(m, poller, addr=addr)
while True:
polld = poller.poll(100)
for sock in polld:
msg = sock.receive()
yield msg
def live_logreader(services: List[str] = ALL_SERVICES, addr: str = '127.0.0.1') -> LogIterable:
for m in raw_live_logreader(services, addr):
with capnp_log.Event.from_bytes(m) as evt:
yield evt
if __name__ == "__main__":
import codecs
# capnproto <= 0.8.0 throws errors converting byte data to string