From 975bc6ad5827b3d7e6d2713d73a9dc036c00b9e1 Mon Sep 17 00:00:00 2001 From: Kilian Bracher Date: Mon, 20 Jan 2025 19:16:46 +0100 Subject: [PATCH] pretty up logging --- AMS_Master_Code/Core/Inc/swo_log.h | 82 +++++++++++++++--------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/AMS_Master_Code/Core/Inc/swo_log.h b/AMS_Master_Code/Core/Inc/swo_log.h index f097e5d..d3ed1f7 100644 --- a/AMS_Master_Code/Core/Inc/swo_log.h +++ b/AMS_Master_Code/Core/Inc/swo_log.h @@ -13,37 +13,38 @@ #include #include +#define MAX_MESSAGE_LENGTH 256 + enum log_level_t { - LOG_LEVEL_NOISY, - LOG_LEVEL_DEBUG, - LOG_LEVEL_INFO, - LOG_LEVEL_WARNING, - LOG_LEVEL_ERROR, - LOG_LEVEL_FATAL + LOG_LEVEL_NOISY, + LOG_LEVEL_DEBUG, + LOG_LEVEL_INFO, + LOG_LEVEL_WARNING, + LOG_LEVEL_ERROR, + LOG_LEVEL_FATAL }; -[[maybe_unused]] static const char * log_level_names[] = { - "\033[37m[NOISY]\033[0m", - "\033[37m[DEBUG]\033[0m", - "\033[97m[INFO] \033[0m", - "\033[93m[WARN] \033[0m", - "\033[31m[ERROR]\033[0m", - "\033[31m[FATAL]\033[0m" +[[maybe_unused]] static const char * const log_level_names[] = { + "\033[37m[NOISY]\033[0m ", + "\033[37m[DEBUG]\033[0m ", + "\033[97m[INFO] \033[0m ", + "\033[93m[WARN] \033[0m ", + "\033[31m[ERROR]\033[0m ", + "\033[31m[FATAL]\033[0m " }; static inline bool __ITM_channel_enabled(uint32_t channel) { - return (ITM->TER & (1UL << channel)) != 0UL; + return (ITM->TER & (1UL << channel)) != 0UL; } -//adapted from ITM_SendChar() in the CMSIS-Core +// adapted from ITM_SendChar() in the CMSIS-Core // -//channel should be between 0 and 31 -__STATIC_INLINE uint32_t __swo_putc (uint32_t c, unsigned int channel) { - if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && /* ITM enabled */ - ((ITM->TER & (1UL << channel) ) != 0UL) ) /* ITM Port enabled */ +// channel should be between 0 and 31 +static inline uint32_t __swo_putc(uint32_t c, unsigned int channel) { + if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && /* ITM enabled */ + ((ITM->TER & (1UL << channel)) != 0UL)) /* ITM Port enabled */ { - while (ITM->PORT[channel].u32 == 0UL) - { + while (ITM->PORT[channel].u32 == 0UL) { __NOP(); } ITM->PORT[channel].u8 = (uint8_t)c; @@ -52,29 +53,30 @@ __STATIC_INLINE uint32_t __swo_putc (uint32_t c, unsigned int channel) { } static inline void __swo_print(const char *str, unsigned int channel) { - if (!__ITM_channel_enabled(channel)) - { - return; - } - while(*str) - { - __swo_putc(*str++, channel); - } + if (!__ITM_channel_enabled(channel)) { + return; + } + while (*str) { + __swo_putc(*str++, channel); + } } -//Print a message to the SWO interface +// Print a message to the SWO interface // -//Each log level is printed to a different ITM channel +// Each log level is printed to a different ITM channel +[[gnu::format(printf, 2, 3)]] static inline void debug_log(enum log_level_t level, const char *msg, ...) { - va_list args; - va_start(args, msg); - char buffer[256]; - vsnprintf(buffer, sizeof(buffer), msg, args); - va_end(args); - __swo_print(log_level_names[level], level); - __swo_print(buffer, level); - __swo_putc('\n', level); + va_list args; + va_start(args, msg); + char buffer[MAX_MESSAGE_LENGTH]; + size_t len = vsnprintf(buffer, sizeof(buffer), msg, args); + va_end(args); + __swo_print(log_level_names[level], level); + __swo_print(buffer, level); + if (len >= sizeof(buffer)) { + __swo_print(" [message length exceeded] ", level); + } + __swo_putc('\n', level); } #endif /* __SWO_LOG_H */ -