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

@@ -83,6 +83,31 @@ This configuration will log sensor_accel 0 at full rate, sensor_accel 1 at 10Hz,
There are several scripts to analyze and convert logging files in the [pyulog](https://github.com/PX4/pyulog) repository.
## Log Cleanup
PX4 automatically manages log storage by rotating log files during writing and cleaning up old logs when starting a new log.
Rotation is **on by default**: when the current file reaches [SDLOG_MAX_SIZE](../advanced_config/parameter_reference.md#SDLOG_MAX_SIZE), the logger closes it and opens a new one, and old `.ulg` files are deleted (oldest first) to keep free space above the threshold set by [SDLOG_ROTATE](../advanced_config/parameter_reference.md#SDLOG_ROTATE).
Three parameters control how much space logs may use:
- [SDLOG_ROTATE](../advanced_config/parameter_reference.md#SDLOG_ROTATE) is the maximum disk usage percentage (default 90).
Cleanup prior to logging (see below) ensures at least `(100 - SDLOG_ROTATE)%` of the disk stays free at all times, **even while writing a new log file**.
Setting it to `0` disables space-based cleanup entirely; setting it to `100` lets logs fill the disk completely.
- [SDLOG_MAX_SIZE](../advanced_config/parameter_reference.md#SDLOG_MAX_SIZE) is the maximum size of a single log file in MB
(default 1024). It also reserves headroom so that a full new file always fits after cleanup.
- [SDLOG_DIRS_MAX](../advanced_config/parameter_reference.md#SDLOG_DIRS_MAX) optionally caps the number of log directories kept (default 0, disabled).
This runs on top of the space-based cleanup and is mainly useful for capping log usage by count independent of available disk size (e.g. in SITL, where it defaults to `7`).
At log start, the cleanup threshold is `((100 - SDLOG_ROTATE)% of disk) + SDLOG_MAX_SIZE`.
The oldest logs are deleted until the free space meets this threshold.
For example, on an 8 GB card with defaults, cleanup keeps at least `820 + 1024 = ~1.8 GB` free at log start,
so ~6 GB is usable for logs and disk usage never exceeds 90% during writing.
Small flash targets override `SDLOG_MAX_SIZE` to a smaller value to keep more logs within the available space.
PX4 stores logs in directories named with one of two formats, depending on whether the system has valid time: date directories (such as `2024-01-15` or `2024-01-16`) when it does, and session directories (`sess001`) when it doesn't.
The cleanup algorithm prioritises deleting logs from whichever format is not currently in use.
This ensures that stale logs from a different time mode are cleaned up before current logs.
## File size limitations
The maximum file size depends on the file system and OS.