diff --git a/src/modules/logger/logger.cpp b/src/modules/logger/logger.cpp index 403dab2134..0df97f474e 100644 --- a/src/modules/logger/logger.cpp +++ b/src/modules/logger/logger.cpp @@ -47,6 +47,14 @@ #include #include #include +#include + +#ifdef __PX4_DARWIN +#include +#include +#else +#include +#endif #define GPS_EPOCH_SECS ((time_t)1234567890ULL) @@ -452,6 +460,10 @@ void Logger::run() return; } + if (check_free_space() == 1) { + return; + } + uORB::Subscription vehicle_status_sub(ORB_ID(vehicle_status)); uORB::Subscription parameter_update_sub(ORB_ID(parameter_update)); @@ -647,6 +659,11 @@ void Logger::run() if (ret) { PX4_WARN("join failed: %d", ret); } + + if (_mavlink_log_pub) { + orb_unadvertise(_mavlink_log_pub); + _mavlink_log_pub = nullptr; + } } int Logger::create_log_dir(tm *tt) @@ -803,6 +820,9 @@ void Logger::start_log() return; } + /* print logging path, important to find log file later */ + mavlink_log_info(&_mavlink_log_pub, "[logger] file: %s", file_name); + _writer.start_log(file_name); write_version(); write_formats(); @@ -1001,5 +1021,25 @@ void Logger::write_changed_parameters() _writer.notify(); } +int Logger::check_free_space() +{ + /* use statfs to determine the number of blocks left */ + struct statfs statfs_buf; + + if (statfs(LOG_ROOT, &statfs_buf) != 0) { + return PX4_ERROR; + } + + /* use a threshold of 50 MiB */ + if (statfs_buf.f_bavail < (px4_statfs_buf_f_bavail_t)(50 * 1024 * 1024 / statfs_buf.f_bsize)) { + mavlink_and_console_log_critical(&_mavlink_log_pub, + "[logger] Not logging; SD almost full: %u MiB", + (unsigned int)(statfs_buf.f_bavail / 1024U * statfs_buf.f_bsize / 1024U)); + return 1; + } + + return PX4_OK; +} + } } diff --git a/src/modules/logger/logger.h b/src/modules/logger/logger.h index 7ce35bf816..0022b05e06 100644 --- a/src/modules/logger/logger.h +++ b/src/modules/logger/logger.h @@ -107,6 +107,12 @@ private: */ int get_log_file_name(char *file_name, size_t file_name_size); + /** + * Check if there is enough free space left on the SD Card + * @return 0 on success, 1 if not enough space, <0 on error + */ + int check_free_space(); + void start_log(); void stop_log(); @@ -164,6 +170,7 @@ private: LogWriter _writer; uint32_t _log_interval; param_t _log_utc_offset; + orb_advert_t _mavlink_log_pub = nullptr; }; Logger *logger_ptr = nullptr;