Skip to content

Commit

Permalink
logger: add SDLOG_DIRS_MAX param to limit the max number of log direc…
Browse files Browse the repository at this point in the history
…tories

Disabled by default, so that logs are not deleted unexpectedly.
  • Loading branch information
bkueng committed Apr 10, 2017
1 parent 85bee93 commit 1a4b057
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/modules/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ Logger::Logger(LogWriter::Backend backend, size_t buffer_size, uint32_t log_inte
_sdlog_mode(0)
{
_log_utc_offset = param_find("SDLOG_UTC_OFFSET");
_log_dirs_max = param_find("SDLOG_DIRS_MAX");
_sdlog_mode_handle = param_find("SDLOG_MODE");

if (poll_topic_name) {
Expand Down Expand Up @@ -1770,6 +1771,16 @@ int Logger::check_free_space()
{
struct statfs statfs_buf;

int32_t max_log_dirs_to_keep = 0;

if (_log_dirs_max != PARAM_INVALID) {
param_get(_log_dirs_max, &max_log_dirs_to_keep);
}

if (max_log_dirs_to_keep == 0) {
max_log_dirs_to_keep = INT32_MAX;
}

/* remove old logs if the free space falls below a threshold */
do {
if (statfs(LOG_ROOT, &statfs_buf) != 0) {
Expand Down Expand Up @@ -1838,8 +1849,9 @@ int Logger::check_free_space()
min_free_bytes = total_bytes / 10;
}

if (statfs_buf.f_bavail >= (px4_statfs_buf_f_bavail_t)(min_free_bytes / statfs_buf.f_bsize)) {
break; // enough free space
if (num_sess + num_dates <= max_log_dirs_to_keep &&
statfs_buf.f_bavail >= (px4_statfs_buf_f_bavail_t)(min_free_bytes / statfs_buf.f_bsize)) {
break; // enough free space and limit not reached
}

if (num_sess == 0 && num_dates == 0) {
Expand Down Expand Up @@ -1873,7 +1885,7 @@ int Logger::check_free_space()
} while (true);


/* use a threshold of 50 MiB */
/* use a threshold of 50 MiB: if below, do not start logging */
if (statfs_buf.f_bavail < (px4_statfs_buf_f_bavail_t)(50 * 1024 * 1024 / statfs_buf.f_bsize)) {
mavlink_log_critical(&_mavlink_log_pub,
"[logger] Not logging; SD almost full: %u MiB",
Expand Down
1 change: 1 addition & 0 deletions src/modules/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ class Logger
uint32_t _log_interval;
const orb_metadata *_polling_topic_meta = nullptr; ///< if non-null, poll on this topic instead of sleeping
param_t _log_utc_offset;
param_t _log_dirs_max;
orb_advert_t _mavlink_log_pub = nullptr;
uint16_t _next_topic_id = 0; ///< id of next subscribed ulog topic
char *_replay_file_name = nullptr;
Expand Down
19 changes: 19 additions & 0 deletions src/modules/logger/params.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ PARAM_DEFINE_INT32(SDLOG_UTC_OFFSET, 0);
*/
PARAM_DEFINE_INT32(SDLOG_MODE, 0);

/**
* Maximum number of log directories to keep
*
* If there are more log directories than this value,
* the system will delete the oldest directories during startup.
*
* In addition, the system will delete old logs if there is not enough free space left.
* The minimum amount is 300 MB.
*
* If this is set to 0, old directories will only be removed if the free space falls below
* the minimum.
*
* @min 0
* @max 1000
* @reboot_required true
* @group SD Logging
*/
PARAM_DEFINE_INT32(SDLOG_DIRS_MAX, 0);

/**
* Log UUID
*
Expand Down

0 comments on commit 1a4b057

Please sign in to comment.