AP_Logger: support logging of files contents

this allows us to see key internal data in log files
This commit is contained in:
Andrew Tridgell 2021-10-28 17:31:51 +11:00
parent b74dca02ca
commit 0be1017843
3 changed files with 112 additions and 1 deletions

View File

@ -1358,6 +1358,11 @@ void AP_Logger::io_thread(void)
if (++counter % 4 == 0) {
hal.util->log_stack_info();
}
#if HAL_LOGGER_FILE_CONTENTS_ENABLED
if (counter % 100 == 0) {
file_content_update();
}
#endif
}
}
@ -1439,6 +1444,80 @@ bool AP_Logger::log_while_disarmed(void) const
return false;
}
#if HAL_LOGGER_FILE_CONTENTS_ENABLED
/*
log the content of a file in FILE log messages
*/
void AP_Logger::log_file_content(const char *filename)
{
WITH_SEMAPHORE(file_content.sem);
auto *file = new file_list;
if (file == nullptr) {
return;
}
file->filename = filename;
if (file_content.head == nullptr) {
file_content.tail = file_content.head = file;
file_content.fd = -1;
} else {
file_content.tail->next = file;
file_content.tail = file;
}
}
/*
periodic call to log file content
*/
void AP_Logger::file_content_update(void)
{
auto *file = file_content.head;
if (file == nullptr) {
return;
}
// remove a file structure from the linked list
auto remove_from_list = [this,file]()
{
WITH_SEMAPHORE(file_content.sem);
file_content.head = file->next;
if (file_content.tail == file) {
file_content.tail = file_content.head;
}
delete file;
file_content.fd = -1;
};
if (file_content.fd == -1) {
// open a new file
file_content.fd = AP::FS().open(file->filename, O_RDONLY);
if (file_content.fd == -1) {
remove_from_list();
return;
}
file_content.offset = 0;
}
struct log_File pkt {
LOG_PACKET_HEADER_INIT(LOG_FILE_MSG),
};
strncpy_noterm(pkt.filename, file->filename, sizeof(pkt.filename));
const auto length = AP::FS().read(file_content.fd, pkt.data, sizeof(pkt.data));
if (length <= 0) {
AP::FS().close(file_content.fd);
remove_from_list();
return;
}
pkt.offset = file_content.offset;
pkt.length = length;
if (WriteBlock_first_succeed(&pkt, sizeof(pkt))) {
file_content.offset += length;
} else {
// seek back ready for another try
AP::FS().lseek(file_content.fd, file_content.offset, SEEK_SET);
}
}
#endif // HAL_LOGGER_FILE_CONTENTS_ENABLED
namespace AP {
AP_Logger &logger()

View File

@ -67,6 +67,10 @@
#endif
#ifndef HAL_LOGGER_FILE_CONTENTS_ENABLED
#define HAL_LOGGER_FILE_CONTENTS_ENABLED HAL_LOGGING_FILESYSTEM_ENABLED
#endif
#include <AP_HAL/AP_HAL.h>
#include <AP_AHRS/AP_AHRS.h>
#include <AP_AHRS/AP_AHRS_DCM.h>
@ -85,7 +89,6 @@
#include "LoggerMessageWriter.h"
class AP_Logger_Backend;
class AP_AHRS;
class AP_AHRS_View;
@ -464,6 +467,9 @@ public:
return _log_start_count;
}
// add a filename to list of files to log. The name must be a constant string, not allocated
void log_file_content(const char *name);
protected:
const struct LogStructure *_structures;
@ -550,6 +556,21 @@ private:
void start_io_thread(void);
void io_thread();
#if HAL_LOGGER_FILE_CONTENTS_ENABLED
// support for logging file content
struct file_list {
struct file_list *next;
const char *filename;
};
struct {
struct file_list *head, *tail;
int fd;
uint16_t offset;
HAL_Semaphore sem;
} file_content;
void file_content_update(void);
#endif
/* support for retrieving logs via mavlink: */
enum class TransferActivity {

View File

@ -687,6 +687,14 @@ struct PACKED log_STAK {
char name[16];
};
struct PACKED log_File {
LOG_PACKET_HEADER;
char filename[16];
uint16_t offset;
uint16_t length;
char data[64];
};
// FMT messages define all message formats other than FMT
// UNIT messages define units which can be referenced by FMTU messages
// FMTU messages associate types (e.g. centimeters/second/second) to FMT message fields
@ -1318,6 +1326,8 @@ LOG_STRUCTURE_FROM_VISUALODOM \
"PSCD", "Qffffffff", "TimeUS,TPD,PD,DVD,TVD,VD,DAD,TAD,AD", "smmnnnooo", "F00000000" }, \
{ LOG_STAK_MSG, sizeof(log_STAK), \
"STAK", "QBBHHN", "TimeUS,Id,Pri,Total,Free,Name", "s#----", "F-----", true }, \
{ LOG_FILE_MSG, sizeof(log_File), \
"FILE", "NhhZ", "FileName,Offset,Length,Data", "----", "----" }, \
LOG_STRUCTURE_FROM_AIS \
// message types 0 to 63 reserved for vehicle specific use
@ -1396,6 +1406,7 @@ enum LogMessages : uint8_t {
LOG_IDS_FROM_PRECLAND,
LOG_IDS_FROM_AIS,
LOG_STAK_MSG,
LOG_FILE_MSG,
_LOG_LAST_MSG_
};