pretty up logging
This commit is contained in:
parent
2577484813
commit
975bc6ad58
|
@ -13,37 +13,38 @@
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAX_MESSAGE_LENGTH 256
|
||||||
|
|
||||||
enum log_level_t {
|
enum log_level_t {
|
||||||
LOG_LEVEL_NOISY,
|
LOG_LEVEL_NOISY,
|
||||||
LOG_LEVEL_DEBUG,
|
LOG_LEVEL_DEBUG,
|
||||||
LOG_LEVEL_INFO,
|
LOG_LEVEL_INFO,
|
||||||
LOG_LEVEL_WARNING,
|
LOG_LEVEL_WARNING,
|
||||||
LOG_LEVEL_ERROR,
|
LOG_LEVEL_ERROR,
|
||||||
LOG_LEVEL_FATAL
|
LOG_LEVEL_FATAL
|
||||||
};
|
};
|
||||||
|
|
||||||
[[maybe_unused]] static const char * log_level_names[] = {
|
[[maybe_unused]] static const char * const log_level_names[] = {
|
||||||
"\033[37m[NOISY]\033[0m",
|
"\033[37m[NOISY]\033[0m ",
|
||||||
"\033[37m[DEBUG]\033[0m",
|
"\033[37m[DEBUG]\033[0m ",
|
||||||
"\033[97m[INFO] \033[0m",
|
"\033[97m[INFO] \033[0m ",
|
||||||
"\033[93m[WARN] \033[0m",
|
"\033[93m[WARN] \033[0m ",
|
||||||
"\033[31m[ERROR]\033[0m",
|
"\033[31m[ERROR]\033[0m ",
|
||||||
"\033[31m[FATAL]\033[0m"
|
"\033[31m[FATAL]\033[0m "
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline bool __ITM_channel_enabled(uint32_t channel) {
|
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
|
// channel should be between 0 and 31
|
||||||
__STATIC_INLINE uint32_t __swo_putc (uint32_t c, unsigned int channel) {
|
static inline uint32_t __swo_putc(uint32_t c, unsigned int channel) {
|
||||||
if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && /* ITM enabled */
|
if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && /* ITM enabled */
|
||||||
((ITM->TER & (1UL << channel) ) != 0UL) ) /* ITM Port enabled */
|
((ITM->TER & (1UL << channel)) != 0UL)) /* ITM Port enabled */
|
||||||
{
|
{
|
||||||
while (ITM->PORT[channel].u32 == 0UL)
|
while (ITM->PORT[channel].u32 == 0UL) {
|
||||||
{
|
|
||||||
__NOP();
|
__NOP();
|
||||||
}
|
}
|
||||||
ITM->PORT[channel].u8 = (uint8_t)c;
|
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) {
|
static inline void __swo_print(const char *str, unsigned int channel) {
|
||||||
if (!__ITM_channel_enabled(channel))
|
if (!__ITM_channel_enabled(channel)) {
|
||||||
{
|
return;
|
||||||
return;
|
}
|
||||||
}
|
while (*str) {
|
||||||
while(*str)
|
__swo_putc(*str++, channel);
|
||||||
{
|
}
|
||||||
__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, ...) {
|
static inline void debug_log(enum log_level_t level, const char *msg, ...) {
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, msg);
|
va_start(args, msg);
|
||||||
char buffer[256];
|
char buffer[MAX_MESSAGE_LENGTH];
|
||||||
vsnprintf(buffer, sizeof(buffer), msg, args);
|
size_t len = vsnprintf(buffer, sizeof(buffer), msg, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
__swo_print(log_level_names[level], level);
|
__swo_print(log_level_names[level], level);
|
||||||
__swo_print(buffer, level);
|
__swo_print(buffer, level);
|
||||||
__swo_putc('\n', level);
|
if (len >= sizeof(buffer)) {
|
||||||
|
__swo_print(" [message length exceeded] ", level);
|
||||||
|
}
|
||||||
|
__swo_putc('\n', level);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* __SWO_LOG_H */
|
#endif /* __SWO_LOG_H */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue