feat(logger): rework log rotation and cleanup

Make the logger practical on small flash storage (e.g. 128 MB W25N
NAND) while keeping the existing SD-card behaviour sensible.

- Move cleanup from boot to log start so logs can be downloaded via
  MAVLink FTP before deletion.
- Delete individual .ulg files (oldest first) instead of whole
  directories, prioritising the naming scheme not currently in use
  (session dirs vs date dirs).
- Add SDLOG_MAX_SIZE (new, default 1024 MB): maximum size of a single
  log file. When reached, the log rotates to a new file. The value is
  also reserved as headroom in the cleanup threshold.
- Add SDLOG_ROTATE (new, default 90): maximum disk usage percentage.
  Cleanup guarantees (100 - SDLOG_ROTATE)% free at all times, even
  during writing of a new log file. 0 disables space-based cleanup,
  100 allows filling the disk.
- Repurpose SDLOG_DIRS_MAX as an orthogonal directory-count cap
  (default 0 = disabled). Useful for capping log usage by count
  independent of disk size, e.g. in SITL.
- Drop the implicit ~300 MB free-space floor that used to fire even
  with SDLOG_DIRS_MAX=0; space is now governed entirely by
  SDLOG_ROTATE + SDLOG_MAX_SIZE.
- Split pure parsing helpers into util_parse.{h,cpp} and add unit
  tests (loggerUtilTest.cpp).
- Simplify get_log_time to rely on clock_gettime (set by the GPS
  driver); safer string handling in directory parsing.
- Reset _total_written in LogFileBuffer::reset() so callers reading
  get_total_written() between stop and start don't see stale counts.

Sponsored by CubePilot.
This commit is contained in:
Julian Oes
2026-04-22 21:29:17 +12:00
parent 381149fb01
commit f9fdda1a29
12 changed files with 857 additions and 152 deletions

View File

@@ -22,7 +22,8 @@ Update these notes with features that are going to be in `main` (PX4 v1.18 or la
## Read Before Upgrading
- TBD …
- Log rotation is now enabled by default. Previously, a single log file grew for the entire flight and old logs were only deleted at boot once free space fell below a fixed 300 MB floor. The logger now caps each log file at [SDLOG_MAX_SIZE](../advanced_config/parameter_reference.md#SDLOG_MAX_SIZE) (new parameter, default `1024` MB) and keeps a configurable percentage of the disk free via [SDLOG_ROTATE](../advanced_config/parameter_reference.md#SDLOG_ROTATE) (new parameter, default `90`, so at least 10% free). Cleanup runs at log start rather than boot, so logs can still be downloaded via FTP before they are deleted. See [Log Cleanup](../dev_log/logging.md#log-cleanup) for details.
- `SDLOG_DIRS_MAX` behaviour changed: it is now an orthogonal directory-count cap that runs on top of the new space-based cleanup, and the default is `0` (disabled). Previously it enforced a fixed ~300 MB free-space floor even when set to `0`. If you relied on that implicit floor, set [SDLOG_ROTATE](../advanced_config/parameter_reference.md#SDLOG_ROTATE) instead.
Please continue reading for [upgrade instructions](#upgrade-guide).
@@ -74,6 +75,11 @@ Please continue reading for [upgrade instructions](#upgrade-guide).
### Debug & Logging
- [Asset Tracking](../debug/asset_tracking.md): Automatic tracking and logging of external device information including vendor name, firmware and hardware version, serial numbers. Currently supports DroneCAN devices. ([PX4-Autopilot#25617](https://github.com/PX4/PX4-Autopilot/pull/25617))
- Logger: support for small flash storage (e.g. 128 MB W25N NAND on kakuteh7mini, kakuteh7v2, airbrainh743). Logs can now be written directly to an internal littlefs volume instead of requiring an SD card.
- Logger: reworked log rotation and cleanup. Log rotation is now on by default, and cleanup runs at log start rather than boot so logs can be downloaded via FTP before being deleted.
- New [SDLOG_MAX_SIZE](../advanced_config/parameter_reference.md#SDLOG_MAX_SIZE) (default `1024` MB) caps the size of a single log file; once reached, the logger closes the current file and starts a new one.
- New [SDLOG_ROTATE](../advanced_config/parameter_reference.md#SDLOG_ROTATE) (default `90`) sets the maximum disk usage percentage. Cleanup guarantees `(100 - SDLOG_ROTATE)%` of the disk stays free at all times, even while writing a new log file. Set `0` to disable space-based cleanup, `100` to allow filling the disk completely.
- `SDLOG_DIRS_MAX` is now an orthogonal cap on the number of log directories (default `0` = disabled), on top of the space-based cleanup driven by `SDLOG_ROTATE` and `SDLOG_MAX_SIZE`. SITL defaults to `7`.
### Ethernet