Add a logging system for uniform log and debugging messages

This commit is contained in:
L-diy
2023-10-31 18:51:48 +01:00
parent 5e487e3c1a
commit 5cd054dced
3 changed files with 276 additions and 0 deletions

80
project/Core/Src/log.c Normal file
View File

@@ -0,0 +1,80 @@
/**
* @file log.c
* @brief Logger implementation
* @author Lorenz C.
*/
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include "stm32f7xx_hal.h"
#include "log.h"
/**
* @brief Entry in the tag list containing the tag and the log level
*/
typedef struct {
const char* tag;
log_level_t level;
} tag_entry_t;
static tag_entry_t tag_entries[LOGGER_MAX_TAG_ENTRIES];
static size_t tag_entries_count = 0;
static const char* TAG = "logger";
void logger_set_log_level(const char* tag, log_level_t level) {
// Check if the tag is already in the list
for (int i = 0; i < tag_entries_count; i++) {
if (tag_entries[i].tag == tag) {
tag_entries[i].level = level;
return;
}
}
// The tag is not in the list yet, so add it
if (tag_entries_count < LOGGER_MAX_TAG_ENTRIES) {
tag_entries[tag_entries_count].tag = tag;
tag_entries[tag_entries_count].level = level;
tag_entries_count++;
} else {
LOG_WARN(TAG, "Could not add tag %s to list: List is full. Try increasing LOGGER_MAX_TAG_ENTRIES", tag);
}
}
log_level_t logger_get_log_level(const char* tag) {
log_level_t level = LOGGER_MIN_LOG_LEVEL;
// Try to find the tag in the list
for (int i = 0; i < tag_entries_count; i++) {
if (tag_entries[i].tag == tag) {
level = tag_entries[i].level;
break;
}
}
return level;
}
/**
* @brief Get the current timestamp to be used in the logger
*
* @return The current timestamp in milliseconds since boot
*/
uint32_t logger_get_timestamp(void) {
return HAL_GetTick();
}
/**
* @brief Write a log message to the console
* For now, this is just a wrapper around printf.
*
* @param[in] format The format string (see printf)
* @param[in] ...
*/
void logger_write(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}