Skip to content

Commit

Permalink
log now has a more flexible input
Browse files Browse the repository at this point in the history
  • Loading branch information
IDV7 committed Feb 21, 2024
1 parent 1dc2a9f commit 8438b63
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 24 deletions.
14 changes: 7 additions & 7 deletions software/Core/Inc/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ typedef enum {
} type_color_t;

void log_init(log_level_t log_level, bool color);
void log_msg(log_level_t log_level,uint8_t *message);
void LOGD(uint8_t *message);
void LOGI(uint8_t *message);
void LOGW(uint8_t *message);
void LOGE(uint8_t *message);
void LOGH(uint8_t *message);
void LOG(uint8_t *message);
void log_msg(log_level_t log_level, const uint8_t *format, ...);
void LOGD(const uint8_t *format, ...);
void LOGI(const uint8_t *format, ...);
void LOGW(const uint8_t *format, ...);
void LOGE(const uint8_t *format, ...);
void LOGH(const uint8_t *format, ...);
void LOG(const uint8_t *format, ...);

#endif //BETTERFLIGHT_LOG_H
76 changes: 59 additions & 17 deletions software/Core/Src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stdint.h>
#include <stdio.h>
#include "misc.h"
#include <stdarg.h>


log_t logging;

Expand Down Expand Up @@ -32,43 +34,83 @@ uint8_t * get_log_level_color_attr(log_level_t log_level) {
}
}

void log_msg(log_level_t log_level,uint8_t *message) {
void log_msg(log_level_t log_level, const uint8_t *format, ...) {
if (log_level < logging.level) {
return;
}

va_list args;
va_start(args, format);

printf("%s", get_log_level_color_attr(log_level));
switch (log_level) {
case LOG_LEVEL_DEBUG:
printf("%s[DEBUG] %s%s\r\n",get_log_level_color_attr(log_level), message, ANSI_COLOR(COLOR_RESET));
printf("[DEBUG] ");
break;
case LOG_LEVEL_INFO:
printf("%s[INFO] %s%s\r\n",get_log_level_color_attr(log_level), message, ANSI_COLOR(COLOR_RESET));
printf("[INFO] ");
break;
case LOG_LEVEL_WARN:
printf("%s[WARN] %s%s\r\n",get_log_level_color_attr(log_level),message, ANSI_COLOR(COLOR_RESET));
printf("[WARN] ");
break;
case LOG_LEVEL_ERROR:
printf("%s[ERR] %s%s\r\n",get_log_level_color_attr(log_level),message, ANSI_COLOR(COLOR_RESET));
printf("[ERR] ");
break;
default:
break;
}
vprintf((char*)format, args);
printf("%s\r\n", ANSI_COLOR(COLOR_RESET));

va_end(args);
}

void LOGD(uint8_t *message) {
log_msg(LOG_LEVEL_DEBUG, message);
void LOGD(const uint8_t *format, ...) {
va_list args;
va_start(args, format);
log_msg(LOG_LEVEL_DEBUG, format, args);
va_end(args);
}
void LOGI(uint8_t *message) {
log_msg(LOG_LEVEL_INFO, message);

void LOGI(const uint8_t *format, ...) {
va_list args;
va_start(args, format);
log_msg(LOG_LEVEL_INFO, format, args);
va_end(args);
}
void LOGW(uint8_t *message) {
log_msg(LOG_LEVEL_WARN, message);

void LOGW(const uint8_t *format, ...) {
va_list args;
va_start(args, format);
log_msg(LOG_LEVEL_WARN, format, args);
va_end(args);
}
void LOGE(uint8_t *message) {
log_msg(LOG_LEVEL_ERROR, message);

void LOGE(const uint8_t *format, ...) {
va_list args;
va_start(args, format);
log_msg(LOG_LEVEL_ERROR, format, args);
va_end(args);
}
void LOGH(uint8_t *message) {
printf("%s%s%s\n", ANSI_COLOR(COLOR_HIGHLIGHT), message, ANSI_COLOR(COLOR_RESET));

void LOGH(const uint8_t *format, ...) {
va_list args;
va_start(args, format);

printf("%s", ANSI_COLOR(COLOR_HIGHLIGHT));
vprintf((char*)format, args);
printf("%s\n", ANSI_COLOR(COLOR_RESET));

va_end(args);
}
void LOG(uint8_t *message) {
printf("%s%s\n", ANSI_COLOR(COLOR_RESET), message);

void LOG(const uint8_t *format, ...) {
va_list args;
va_start(args, format);

printf(ANSI_COLOR(COLOR_RESET));
vprintf((char*)format, args);
printf("\n");

va_end(args);
}

0 comments on commit 8438b63

Please sign in to comment.