add: introduce timestamp logging option for SWO messages

This commit is contained in:
Kilian Bracher 2025-03-09 00:57:33 +01:00
parent 96c21d55c8
commit 4bd55843e6

View File

@ -16,6 +16,7 @@
#define MAX_MESSAGE_LENGTH 256
#define USE_MULTIPLE_CHANNELS false // if true, each log level has its own channel (FATAL : 0, ERROR : 1, etc.)
#define USE_ANSI_ESCAPE_CODES true // if true, log messages will be colored according to their log level, and the console can be cleared
#define PRINT_TIMESTAMP false // if true, timestamp (from HAL_GetTick) is printed before each log message
#if !USE_MULTIPLE_CHANNELS
#define DEBUG_CHANNEL 0 // channel to output messages on
@ -48,12 +49,12 @@ enum log_level_t {
static inline bool __ITM_channel_enabled(uint32_t channel) {
#if !USE_MULTIPLE_CHANNELS
#if USE_CHANNEL_MASK_VARIABLE
#if USE_CHANNEL_MASK_VARIABLE
return ((ITM->TER & (1UL << DEBUG_CHANNEL)) != 0UL) &&
((MASK_VARIABLE & (1UL << channel)) != 0UL);
#else
#else
channel = DEBUG_CHANNEL;
#endif
#endif
#endif
return ((ITM->TER & (1UL << channel)) != 0UL);
}
@ -110,6 +111,18 @@ static void debug_clear_console() {
size_t len = \
snprintf(__swo_buffer, sizeof(__swo_buffer), msg, ##__VA_ARGS__); \
__swo_putc('\n', level); \
/* Print timestamp if enabled */ \
if (PRINT_TIMESTAMP) { \
char __time_buffer[16]; \
if (USE_ANSI_ESCAPE_CODES) { \
snprintf(__time_buffer, sizeof(__time_buffer), \
"\033[90m[%lu]\033[0m ", HAL_GetTick()); \
} else { \
snprintf(__time_buffer, sizeof(__time_buffer), "[%lu] ", \
HAL_GetTick()); \
} \
__swo_print(level, __time_buffer); \
} \
__swo_print(level, log_level_names[level]); \
__swo_print(level, __swo_buffer); \
if (len >= sizeof(__swo_buffer)) { \