Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_Logger: tidy construction of backends #19130

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 24 additions & 71 deletions libraries/AP_Logger/AP_Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,95 +172,48 @@ void AP_Logger::Init(const struct LogStructure *structures, uint8_t num_types)
validate_structures(structures, num_types);
dump_structures(structures, num_types);
#endif
if (_next_backend == LOGGER_MAX_BACKENDS) {
AP_HAL::panic("Too many backends");
return;
}
_num_types = num_types;
_structures = structures;

// the "main" logging type needs to come before mavlink so that
// index 0 is correct
static const struct {
Backend_Type type;
AP_Logger_Backend* (*probe_fn)(AP_Logger&, LoggerMessageWriter_DFLogStart*);
} backend_configs[] {
#if HAL_LOGGING_FILESYSTEM_ENABLED
if (_params.backend_types & uint8_t(Backend_Type::FILESYSTEM)) {
LoggerMessageWriter_DFLogStart *message_writer =
new LoggerMessageWriter_DFLogStart();
if (message_writer != nullptr) {
backends[_next_backend] = new AP_Logger_File(*this,
message_writer,
HAL_BOARD_LOG_DIRECTORY);
}
if (backends[_next_backend] == nullptr) {
hal.console->printf("Unable to open AP_Logger_File");
// note that message_writer is leaked here; costs several
// hundred bytes to fix for marginal utility
} else {
_next_backend++;
}
}
#endif // HAL_LOGGING_FILESYSTEM_ENABLED

{ Backend_Type::FILESYSTEM, AP_Logger_File::probe },
#endif
#if HAL_LOGGING_DATAFLASH_ENABLED
if (_params.backend_types & uint8_t(Backend_Type::BLOCK)) {
if (_next_backend == LOGGER_MAX_BACKENDS) {
AP_HAL::panic("Too many backends");
return;
}
LoggerMessageWriter_DFLogStart *message_writer =
new LoggerMessageWriter_DFLogStart();
if (message_writer != nullptr) {
backends[_next_backend] = new AP_Logger_DataFlash(*this, message_writer);
}
if (backends[_next_backend] == nullptr) {
hal.console->printf("Unable to open AP_Logger_DataFlash");
// note that message_writer is leaked here; costs several
// hundred bytes to fix for marginal utility
} else {
_next_backend++;
}
}
{ Backend_Type::BLOCK, AP_Logger_DataFlash::probe },
#endif

#if HAL_LOGGING_SITL_ENABLED
if (_params.backend_types & uint8_t(Backend_Type::BLOCK)) {
if (_next_backend == LOGGER_MAX_BACKENDS) {
AP_HAL::panic("Too many backends");
return;
}
LoggerMessageWriter_DFLogStart *message_writer =
new LoggerMessageWriter_DFLogStart();
if (message_writer != nullptr) {
backends[_next_backend] = new AP_Logger_SITL(*this, message_writer);
}
if (backends[_next_backend] == nullptr) {
hal.console->printf("Unable to open AP_Logger_SITL");
// note that message_writer is leaked here; costs several
// hundred bytes to fix for marginal utility
} else {
_next_backend++;
}
}
{ Backend_Type::BLOCK, AP_Logger_SITL::probe },
#endif
// the "main" logging type needs to come before mavlink so that index 0 is correct
#if HAL_LOGGING_MAVLINK_ENABLED
if (_params.backend_types & uint8_t(Backend_Type::MAVLINK)) {
{ Backend_Type::MAVLINK, AP_Logger_MAVLink::probe },
#endif
};

for (const auto &backend_config : backend_configs) {
if ((_params.backend_types & uint8_t(backend_config.type)) == 0) {
continue;
}
if (_next_backend == LOGGER_MAX_BACKENDS) {
AP_HAL::panic("Too many backends");
AP_BoardConfig::config_error("Too many backends");
return;
}
LoggerMessageWriter_DFLogStart *message_writer =
new LoggerMessageWriter_DFLogStart();
if (message_writer != nullptr) {
backends[_next_backend] = new AP_Logger_MAVLink(*this,
message_writer);
if (message_writer == nullptr) {
AP_BoardConfig::allocation_error("mesage writer");
}
backends[_next_backend] = backend_config.probe_fn(*this, message_writer);
if (backends[_next_backend] == nullptr) {
hal.console->printf("Unable to open AP_Logger_MAVLink");
// note that message_writer is leaked here; costs several
// hundred bytes to fix for marginal utility
} else {
_next_backend++;
AP_BoardConfig::allocation_error("logger backend");
}
_next_backend++;
}
#endif

for (uint8_t i=0; i<_next_backend; i++) {
backends[i]->Init();
Expand Down
4 changes: 4 additions & 0 deletions libraries/AP_Logger/AP_Logger_DataFlash.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class AP_Logger_DataFlash : public AP_Logger_Block {
public:
AP_Logger_DataFlash(AP_Logger &front, LoggerMessageWriter_DFLogStart *writer) :
AP_Logger_Block(front, writer) {}
static AP_Logger_Backend *probe(AP_Logger &front,
LoggerMessageWriter_DFLogStart *ls) {
return new AP_Logger_DataFlash(front, ls);
}
void Init(void) override;
bool CardInserted() const override { return !flash_died && df_NumPages > 0; }

Expand Down
5 changes: 2 additions & 3 deletions libraries/AP_Logger/AP_Logger_File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ extern const AP_HAL::HAL& hal;
constructor
*/
AP_Logger_File::AP_Logger_File(AP_Logger &front,
LoggerMessageWriter_DFLogStart *writer,
const char *log_directory) :
LoggerMessageWriter_DFLogStart *writer) :
AP_Logger_Backend(front, writer),
_log_directory(log_directory)
_log_directory(HAL_BOARD_LOG_DIRECTORY)
{
df_stats_clear();
}
Expand Down
8 changes: 6 additions & 2 deletions libraries/AP_Logger/AP_Logger_File.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ class AP_Logger_File : public AP_Logger_Backend
public:
// constructor
AP_Logger_File(AP_Logger &front,
LoggerMessageWriter_DFLogStart *,
const char *log_directory);
LoggerMessageWriter_DFLogStart *);

static AP_Logger_Backend *probe(AP_Logger &front,
LoggerMessageWriter_DFLogStart *ls) {
return new AP_Logger_File(front, ls);
}

// initialisation
void Init() override;
Expand Down
5 changes: 5 additions & 0 deletions libraries/AP_Logger/AP_Logger_MAVLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class AP_Logger_MAVLink : public AP_Logger_Backend
// ::fprintf(stderr, "DM: Using %u blocks\n", _blockcount);
}

static AP_Logger_Backend *probe(AP_Logger &front,
LoggerMessageWriter_DFLogStart *ls) {
return new AP_Logger_MAVLink(front, ls);
}

// initialisation
void Init() override;

Expand Down
6 changes: 6 additions & 0 deletions libraries/AP_Logger/AP_Logger_SITL.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class AP_Logger_SITL : public AP_Logger_Block {
public:
AP_Logger_SITL(AP_Logger &front, LoggerMessageWriter_DFLogStart *writer) :
AP_Logger_Block(front, writer) {}

static AP_Logger_Backend *probe(AP_Logger &front,
LoggerMessageWriter_DFLogStart *ls) {
return new AP_Logger_SITL(front, ls);
}

void Init() override;
bool CardInserted() const override;
static constexpr const char *filename = "dataflash.bin";
Expand Down