Shrink the logger
* Move _write function to the log.c * Change the way to setup the logger * Update the readme * Add terminal commands
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file log.c
|
||||
* @brief Logger implementation
|
||||
* @author Lorenz C.
|
||||
* @author Lorenz C. && Speetjens S.
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
@@ -10,71 +10,33 @@
|
||||
#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;
|
||||
}
|
||||
extern UART_HandleTypeDef huart1;
|
||||
|
||||
/**
|
||||
* @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();
|
||||
|
||||
int _write(int file, char *data, int len) {
|
||||
HAL_StatusTypeDef status;
|
||||
|
||||
switch (file) {
|
||||
case STDOUT_FILENO:
|
||||
case STDERR_FILENO:
|
||||
status = HAL_UART_Transmit(&huart1, (uint8_t*)data, len, HAL_MAX_DELAY);
|
||||
if (status != HAL_OK) {
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
errno = EBADF;
|
||||
return -1;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
uint32_t logger_get_timestamp(void) {
|
||||
return HAL_GetTick();
|
||||
}
|
||||
@@ -22,8 +22,7 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include <errno.h>
|
||||
#include <sys/unistd.h>
|
||||
#include "log.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -431,24 +430,6 @@ static void MX_GPIO_Init(void)
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
int _write(int file, char *data, int len) {
|
||||
HAL_StatusTypeDef status;
|
||||
|
||||
switch (file) {
|
||||
case STDOUT_FILENO:
|
||||
case STDERR_FILENO:
|
||||
status = HAL_UART_Transmit(&huart1, (uint8_t*)data, len, HAL_MAX_DELAY);
|
||||
if (status != HAL_OK) {
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
errno = EBADF;
|
||||
return -1;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user