pretty up logging

This commit is contained in:
Kilian Bracher 2025-01-20 19:16:46 +01:00
parent 2577484813
commit 975bc6ad58
Signed by: k.bracher
SSH Key Fingerprint: SHA256:mXpyZkK7RDiJ7qeHCKJX108woM0cl5TrCvNBJASu6lM
1 changed files with 42 additions and 40 deletions

View File

@ -13,6 +13,8 @@
#include <stdarg.h>
#include <stdio.h>
#define MAX_MESSAGE_LENGTH 256
enum log_level_t {
LOG_LEVEL_NOISY,
LOG_LEVEL_DEBUG,
@ -22,28 +24,27 @@ enum log_level_t {
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;
}
//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) {
// 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)
((ITM->TER & (1UL << channel)) != 0UL)) /* ITM Port enabled */
{
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))
{
if (!__ITM_channel_enabled(channel)) {
return;
}
while(*str)
{
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);
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 */