The Logging System provides a custom infrastructure for openpilot that addresses the unique requirements of a real-time autonomous driving system. The system is built around SwagLogger, which implements a two-tier logging strategy: immediate IPC-based log transmission for aggregation and deferred file-based persistence with automatic rotation. This architecture prevents disk I/O operations from blocking critical real-time processes.
Beyond standard text logging, the system includes loggerd and encoderd, which handle high-bandwidth data logging (CAN messages and video) into structured, compressed segments.
Sources: openpilot/common/swaglog.cc5-156 openpilot/system/loggerd/loggerd.cc16-54
The logging system consists of multiple layers that work together to provide non-blocking, persistent logging of both diagnostic text and high-frequency vehicle data.
Architecture: openpilot Logging Infrastructure
Each openpilot process uses the SwagLogger interface. High-bandwidth data follows a separate path through loggerd and encoderd:
encoderd consumes frames via VisionIPC, encodes them (H.264/H.265), and publishes them back to the messaging bus openpilot/system/loggerd/encoderd.cc112-118loggerd subscribes to all relevant services and writes them to compressed rlog.zst files in 60-second segments openpilot/system/loggerd/loggerd.cc24-30 openpilot/system/loggerd/loggerd.h26Sources: openpilot/common/swaglog.cc18-81 openpilot/system/loggerd/loggerd.cc24-54 openpilot/system/loggerd/encoderd.cc48-125
The C++ implementation is centered around the SwaglogState class. It initializes a ZeroMQ context and a ZMQ_PUSH socket connected to the local swaglog IPC path openpilot/common/swaglog.cc21-27
Key features include:
ZMQ_NOBLOCK in zmq_send to ensure that if the logging daemon is slow or the buffer is full, the calling process is not stalled openpilot/common/swaglog.cc74dongle_id, version, device name, and git state (origin, branch, commit, dirty) to every log entry openpilot/common/swaglog.cc43-62ZMQ_LINGER is set to allow messages to flush during process shutdown openpilot/common/swaglog.cc25-26The system provides several entry points:
cloudlog_e: Standard formatted logging that constructs a JSON object openpilot/common/swaglog.cc113-122cloudlog_te: Timestamped logging, which includes nanos_since_boot and an optional frame_id for synchronizing events with sensor data openpilot/common/swaglog.cc124-155print_level (defaulting to CLOUDLOG_WARNING) determines which logs are echoed to stdout openpilot/common/swaglog.cc32-41 openpilot/common/swaglog.cc71-73Sources: openpilot/common/swaglog.cc21-75 openpilot/common/swaglog.cc113-155
loggerd is responsible for recording all cereal messages to disk. It organizes data into "routes" and "segments."
000001a3--c20ba54385) openpilot/system/loggerd/logger.cc96-118ZstdFileWriter instances for rlog.zst (full log) and qlog.zst (reduced log for quick upload) openpilot/system/loggerd/logger.cc164-197Rotation is synchronized between loggerd and encoderd. loggerd waits for all active encoders to signal they are ready to rotate to the next segment openpilot/system/loggerd/loggerd.cc32-54
loggerd auto-rotates after a timeout (NO_CAMERA_PATIENCE) to prevent oversized segments openpilot/system/loggerd/loggerd.cc40-49Sources: openpilot/system/loggerd/logger.cc164-202 openpilot/system/loggerd/loggerd.cc24-54
encoderd handles the compression of raw camera frames. It supports multiple backends depending on the hardware platform.
| Class | Platform | Technology | Use Case |
|---|---|---|---|
V4LEncoder | TICI (comma 3/3X) | Hardware V4L2 (QCOM) | Production video encoding openpilot/system/loggerd/encoder/v4l_encoder.cc146-155 |
FfmpegEncoder | PC / Mac | Software (FFmpeg) | Simulation and development openpilot/system/loggerd/encoder/ffmpeg_encoder.cc24-65 |
JpegEncoder | All | libavcodec (MJPEG) | Generating thumbnails openpilot/system/loggerd/encoder/jpeg_encoder.cc11-44 |
To ensure frames from different cameras (Road, Wide Road, Driver) are aligned in the same segment, encoderd implements a synchronization mechanism. It waits for all available camera streams to reach a consistent start_frame_id before beginning the first segment openpilot/system/loggerd/encoderd.cc27-45
Code Entity Space: encoderd Loop
sync_encoders: Ensures all camera streams start logging at the same global frame ID openpilot/system/loggerd/encoderd.cc27-45encode_frame: High-level interface for encoding a VisionBuf openpilot/system/loggerd/encoder/encoder.h26publisher_publish: Wraps encoded bitstream into a cereal::Event and sends it via PubMaster openpilot/system/loggerd/encoder/encoder.cc12-42JpegEncoder::pushThumbnail: Specifically handles downsampling and MJPEG compression for UI/Cloud previews openpilot/system/loggerd/encoder/jpeg_encoder.cc52-62Sources: openpilot/system/loggerd/encoderd.cc27-125 openpilot/system/loggerd/encoder/ffmpeg_encoder.cc82-156 openpilot/system/loggerd/encoder/jpeg_encoder.cc11-115 openpilot/system/loggerd/encoder/encoder.cc12-42
The system uses several mechanisms to prevent disk I/O from impacting real-time performance:
ZMQ_PUSH socket openpilot/common/swaglog.cc74loggerd runs as a separate process from control loops, ensuring that file write latency does not jitter the controlsd or plannerd loops openpilot/system/loggerd/loggerd.cc111-112rlog and qlog use ZstdFileWriter to compress data before writing, reducing the total bytes hit on the storage medium openpilot/system/loggerd/logger.cc190-191Code Entity Space: Data Flow to Storage
Sources: openpilot/common/swaglog.cc74 openpilot/system/loggerd/logger.cc190-202 openpilot/system/loggerd/loggerd.cc111-112
The logging system is validated via comprehensive test suites:
system/loggerd/tests/test_loggerd.py validates segment rotation, file integrity, and metadata accuracy (dongle ID, git commit, etc.) openpilot/system/loggerd/tests/test_loggerd.py150-196common/tests/test_swaglog.cc ensures thread-safety and JSON formatting correctness openpilot/common/tests/test_swaglog.cc16-88Sources: openpilot/system/loggerd/tests/test_loggerd.py34-196 openpilot/common/tests/test_swaglog.cc74-88
Refresh this wiki