Skip to content

Commit

Permalink
AP_Logger: Prepare the maximum number of log files in the config para…
Browse files Browse the repository at this point in the history
…meter
  • Loading branch information
khancyr authored and tridge committed Nov 22, 2023
1 parent 8cd91b4 commit d98a400
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
21 changes: 20 additions & 1 deletion libraries/AP_Logger/AP_Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ extern const AP_HAL::HAL& hal;
#define LOGGING_FIRST_DYNAMIC_MSGID 254
#endif

static constexpr uint16_t MAX_LOG_FILES = 500;
static constexpr uint16_t MIN_LOG_FILES = 2;

const AP_Param::GroupInfo AP_Logger::var_info[] = {
// @Param: _BACKEND_TYPE
Expand Down Expand Up @@ -176,7 +178,16 @@ const AP_Param::GroupInfo AP_Logger::var_info[] = {
// @Increment: 0.1
// @User: Standard
AP_GROUPINFO("_DARM_RATEMAX", 11, AP_Logger, _params.disarm_ratemax, 0),


// @Param: _MAX_FILES
// @DisplayName: Maximum number of log files
// @Description: This sets the maximum number of log file that will be written on dataflash or sd card before starting to rotate log number. Limit is capped at 500 logs.
// @Range: 2 500
// @Increment: 1
// @User: Advanced
// @RebootRequired: True
AP_GROUPINFO("_MAX_FILES", 12, AP_Logger, _params.max_log_files, MAX_LOG_FILES),

AP_GROUPEND
};

Expand Down Expand Up @@ -822,6 +833,14 @@ uint16_t AP_Logger::get_num_logs(void) {
return backends[0]->get_num_logs();
}

uint16_t AP_Logger::get_max_num_logs() {
const auto max_logs = constrain_uint16(_params.max_log_files.get(), MIN_LOG_FILES, MAX_LOG_FILES);
if (_params.max_log_files.get() != max_logs) {
_params.max_log_files.set_and_save_ifchanged(static_cast<int16_t>(max_logs));
}
return static_cast<uint16_t>(_params.max_log_files.get());
}

/* we're started if any of the backends are started */
bool AP_Logger::logging_started(void) {
for (uint8_t i=0; i< _next_backend; i++) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/AP_Logger/AP_Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class AP_Logger
uint16_t find_last_log() const;
void get_log_boundaries(uint16_t log_num, uint32_t & start_page, uint32_t & end_page);
uint16_t get_num_logs(void);
uint16_t get_max_num_logs();

void setVehicle_Startup_Writer(vehicle_startup_message_Writer writer);

Expand Down Expand Up @@ -336,6 +337,7 @@ class AP_Logger
AP_Float mav_ratemax;
AP_Float blk_ratemax;
AP_Float disarm_ratemax;
AP_Int16 max_log_files;
} _params;

const struct LogStructure *structure(uint16_t num) const;
Expand Down
5 changes: 3 additions & 2 deletions libraries/AP_Logger/AP_Logger_Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,9 @@ uint16_t AP_Logger_Backend::log_num_from_list_entry(const uint16_t list_entry)
}

uint32_t log_num = oldest_log + list_entry - 1;
if (log_num > MAX_LOG_FILES) {
log_num -= MAX_LOG_FILES;
const auto max_logs_num = _front.get_max_num_logs();
if (log_num > (uint32_t)max_logs_num) {
log_num -= max_logs_num;
}
return (uint16_t)log_num;
}
Expand Down
2 changes: 0 additions & 2 deletions libraries/AP_Logger/AP_Logger_Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

class LoggerMessageWriter_DFLogStart;

#define MAX_LOG_FILES 500

// class to handle rate limiting of log messages
class AP_Logger_RateLimiter
{
Expand Down
13 changes: 7 additions & 6 deletions libraries/AP_Logger/AP_Logger_File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ bool AP_Logger_File::dirent_to_log_num(const dirent *de, uint16_t &log_num) cons
}

uint16_t thisnum = strtoul(de->d_name, nullptr, 10);
if (thisnum > MAX_LOG_FILES) {
if (thisnum > _front.get_max_num_logs()) {
return false;
}
log_num = thisnum;
Expand Down Expand Up @@ -330,7 +330,7 @@ void AP_Logger_File::Prep_MinSpace()
if (avail >= target_free) {
break;
}
if (count++ > MAX_LOG_FILES+10) {
if (count++ > _front.get_max_num_logs() + 10) {
// *way* too many deletions going on here. Possible internal error.
INTERNAL_ERROR(AP_InternalError::error_t::logger_too_many_deletions);
break;
Expand Down Expand Up @@ -360,7 +360,7 @@ void AP_Logger_File::Prep_MinSpace()
}
}
log_to_remove++;
if (log_to_remove > MAX_LOG_FILES) {
if (log_to_remove > _front.get_max_num_logs()) {
log_to_remove = 1;
}
} while (log_to_remove != first_log_to_remove);
Expand Down Expand Up @@ -719,14 +719,15 @@ uint16_t AP_Logger_File::get_num_logs()
// not a log filename
continue;
}

if (thisnum > high && (smallest_above_last == 0 || thisnum < smallest_above_last)) {
smallest_above_last = thisnum;
}
}
AP::FS().closedir(d);
if (smallest_above_last != 0) {
// we have wrapped, add in the logs with high numbers
ret += (MAX_LOG_FILES - smallest_above_last) + 1;
ret += (_front.get_max_num_logs() - smallest_above_last) + 1;
}

return ret;
Expand Down Expand Up @@ -826,7 +827,7 @@ void AP_Logger_File::start_new_log(void)
if (_get_log_size(log_num) > 0 || log_num == 0) {
log_num++;
}
if (log_num > MAX_LOG_FILES) {
if (log_num > _front.get_max_num_logs()) {
log_num = 1;
}
if (!write_fd_semaphore.take(1)) {
Expand Down Expand Up @@ -1115,7 +1116,7 @@ void AP_Logger_File::erase_next(void)
free(fname);

erase.log_num++;
if (erase.log_num <= MAX_LOG_FILES) {
if (erase.log_num <= _front.get_max_num_logs()) {
return;
}

Expand Down

0 comments on commit d98a400

Please sign in to comment.