From 512ae3f51ad8959d6b197d61b39a344f267eabd1 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Wed, 3 Apr 2024 11:49:13 +0200 Subject: [PATCH] log_console: Add pretty print This introduces alternative output of mod log header. Log mod module was printed: mod=0 now it is displayed this way: [DEFAULT] Log level used to be: level=2 Now it is: [WRN] Time stamp is not displayed by default if CONSOLE_TICKS is 1 Severity level can be printed in color. Additional syscfg values that can be used: LOG_CONSOLE_PRETTIFY enable alternative display LOG_CONSOLE_PRETTIFY_WITH_COLORS enables colors for severity level LOG_CONSOLE_PRETTIFY_WITH_TIMESTAMP enables/disables time stamp printing Signed-off-by: Jerzy Kasenberg --- sys/log/full/src/log_console.c | 66 ++++++++++++++++++++++++++++++++++ sys/log/full/syscfg.yml | 19 ++++++++++ 2 files changed, 85 insertions(+) diff --git a/sys/log/full/src/log_console.c b/sys/log/full/src/log_console.c index 5552409e7c..65c0137d7d 100644 --- a/sys/log/full/src/log_console.c +++ b/sys/log/full/src/log_console.c @@ -50,6 +50,71 @@ log_console_get(void) return &log_console; } +#if MYNEWT_VAL(LOG_CONSOLE_PRETTIFY) +#define CSI "\x1b[" +#define COLOR_BLUE "36m" +#define COLOR_YELLOW "33m" +#define COLOR_RED "31m" +#define COLOR_RED_BG "41m" + +#if MYNEWT_VAL(LOG_CONSOLE_PRETTIFY_WITH_COLORS) +#define COLOR_DBG CSI COLOR_BLUE +#define COLOR_WRN CSI COLOR_YELLOW +#define COLOR_ERR CSI COLOR_RED +#define COLOR_CRI CSI COLOR_RED_BG +#define COLOR_RESET CSI "0m" +#else +#define COLOR_DBG "" +#define COLOR_WRN "" +#define COLOR_ERR "" +#define COLOR_CRI "" +#define COLOR_RESET "" +#endif + +static const char *log_level_str[] = { + COLOR_DBG "DBG" COLOR_RESET, + "INF", + COLOR_WRN "WRN" COLOR_RESET, + COLOR_ERR "ERR" COLOR_RESET, + COLOR_CRI "CRI" COLOR_RESET, +}; + +static void +log_console_print_hdr(const struct log_entry_hdr *hdr) +{ + char module_num[10]; + char image_hash_str[17]; + char level_str_buf[13]; + const char *level_str = level_str_buf; + const char *module_name = NULL; + + /* Find module defined in syscfg.logcfg sections */ + module_name = log_module_get_name(hdr->ue_module); + + if (module_name == NULL) { + module_name = module_num; + sprintf(module_num, "mod=%u", hdr->ue_module); + } + if (hdr->ue_flags & LOG_FLAGS_IMG_HASH) { + sprintf(image_hash_str, "[ih=0x%02x%02x%02x%02x]", hdr->ue_imghash[0], hdr->ue_imghash[1], + hdr->ue_imghash[2], hdr->ue_imghash[3]); + } else { + image_hash_str[0] = 0; + } + if (hdr->ue_level <= LOG_LEVEL_CRITICAL) { + level_str = log_level_str[hdr->ue_level]; + } else { + sprintf(level_str_buf, "level=%u", hdr->ue_level); + } + + if (MYNEWT_VAL(LOG_CONSOLE_PRETTIFY_WITH_TIMESTAMP)) { + console_printf("[%7lluus][%7s][%s]%s ", hdr->ue_ts, module_name, level_str, image_hash_str); + } else { + console_printf("[%7s][%s]%s ", module_name, level_str, image_hash_str); + } +} + +#else static void log_console_print_hdr(const struct log_entry_hdr *hdr) { @@ -62,6 +127,7 @@ log_console_print_hdr(const struct log_entry_hdr *hdr) } console_printf("]"); } +#endif static int log_console_append_body(struct log *log, const struct log_entry_hdr *hdr, diff --git a/sys/log/full/syscfg.yml b/sys/log/full/syscfg.yml index 356753eaa9..68dc3d2e31 100644 --- a/sys/log/full/syscfg.yml +++ b/sys/log/full/syscfg.yml @@ -32,6 +32,22 @@ syscfg.defs: description: 'Limits what level log messages are compiled in.' value: 0 + LOG_CONSOLE_PRETTIFY: + description: > + Use alternative formatting of mod log prints. + value: 0 + + LOG_CONSOLE_PRETTIFY_WITH_COLORS: + description: > + Use color for mod log levels. + value: 0 + + LOG_CONSOLE_PRETTIFY_WITH_TIMESTAMP: + description: > + Print timestamp in us. + Turned off by default when CONSOLE_TICKS is on. + value: 1 + LOG_FLAGS_IMAGE_HASH: description: > Enable logging of the first 4 bytes of the image hash. 0 - disable; @@ -155,5 +171,8 @@ syscfg.defs: the global index to be sequentially increasing for persisted logs. value: 0 +syscfg.vals.CONSOLE_TICKS: + LOG_CONSOLE_PRETTIFY_WITH_TIMESTAMP: 0 + syscfg.vals.LOG_NEWTMGR: LOG_MGMT: MYNEWT_VAL(LOG_MGMT)