diff --git a/sys/log/full/src/log_console.c b/sys/log/full/src/log_console.c index 9a31666230..13e7e1169a 100644 --- a/sys/log/full/src/log_console.c +++ b/sys/log/full/src/log_console.c @@ -52,18 +52,65 @@ log_console_get(void) #if MYNEWT_VAL(LOG_CONSOLE_PRETTY) #define CSI "\x1b[" -#define COLOR_BLUE "36m" -#define COLOR_YELLOW "33m" -#define COLOR_RED "31m" -#define COLOR_RED_BG "41m" +#define COLOR_BLACK CSI "30m" +#define COLOR_RED CSI "31m" +#define COLOR_GREEN CSI "32m" +#define COLOR_YELLOW CSI "33m" +#define COLOR_BLUE CSI "34m" +#define COLOR_MAGENTA CSI "35m" +#define COLOR_CYAN CSI "36m" +#define COLOR_WHITE CSI "37m" +#define COLOR_BLACK_BG CSI "40m" +#define COLOR_RED_BG CSI "41m" +#define COLOR_GREEN_BG CSI "42m" +#define COLOR_YELLOW_BG CSI "43m" +#define COLOR_WHITE_BG CSI "47m" #if MYNEWT_VAL(LOG_CONSOLE_PRETTY_WITH_COLORS) -#define COLOR_DBG CSI COLOR_BLUE +#define COLOR_DBG COLOR_CYAN #define COLOR_INF "" -#define COLOR_WRN CSI COLOR_YELLOW -#define COLOR_ERR CSI COLOR_RED -#define COLOR_CRI CSI COLOR_RED_BG +#define COLOR_WRN COLOR_YELLOW +#define COLOR_ERR COLOR_RED +#define COLOR_CRI COLOR_RED_BG #define COLOR_RESET CSI "0m" + +const char *const module_colors[] = { + COLOR_GREEN COLOR_BLACK_BG, + COLOR_RED COLOR_WHITE_BG, + COLOR_BLUE, + COLOR_YELLOW COLOR_BLACK_BG, + COLOR_MAGENTA COLOR_WHITE_BG, + COLOR_CYAN COLOR_BLACK_BG, + COLOR_WHITE COLOR_RED_BG, + COLOR_BLACK COLOR_GREEN_BG, + COLOR_BLACK COLOR_YELLOW_BG, +}; + +static uint8_t log_module_color_index[10]; + +static void +log_module_color(uint8_t module, char *color_on, char *color_off) +{ + int i; + + *color_on = 0; + *color_off = 0; + + if (module) { + for (i = 0; i < ARRAY_SIZE(log_module_color_index) && + log_module_color_index[i] != 0 && + log_module_color_index[i] != module; ++i) { + } + if (i < ARRAY_SIZE(log_module_color_index)) { + if (log_module_color_index[i] == 0) { + log_module_color_index[i] = module; + } + strcpy(color_on, module_colors[i]); + strcpy(color_off, COLOR_RESET); + } + } +} + #else #define COLOR_DBG "" #define COLOR_INF "" @@ -71,6 +118,7 @@ log_console_get(void) #define COLOR_ERR "" #define COLOR_CRI "" #define COLOR_RESET "" +#define log_module_color(hdr, on, off) #endif static const char * const log_level_color[] = { @@ -82,27 +130,34 @@ static const char * const log_level_color[] = { }; static const char * const log_level_str[] = { - "[DBG]", - "[INF]", - "[WRN]", - "[ERR]", - "[CRI]", + "DBG", + "INF", + "WRN", + "ERR", + "CRI", }; -static void +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]; + char level_str_buf[23]; const char *level_str = ""; const char *module_name = NULL; - const char *color = ""; - const char *color_off = ""; + char color[11]; + char color_off[6]; /* Find module defined in syscfg.logcfg sections */ module_name = log_module_get_name(hdr->ue_module); + if (MYNEWT_VAL(LOG_CONSOLE_PRETTY_WITH_COLORS)) { + log_module_color(hdr->ue_module, color, color_off); + } else { + color[0] = 0; + color_off[0] = 0; + } + if (module_name == NULL) { module_name = module_num; sprintf(module_num, "mod=%u", hdr->ue_module); @@ -115,26 +170,29 @@ log_console_print_hdr(const struct log_entry_hdr *hdr) } if (hdr->ue_level <= LOG_LEVEL_CRITICAL) { if (MYNEWT_VAL(LOG_CONSOLE_PRETTY_WITH_COLORS)) { - color = log_level_color[hdr->ue_level]; - color_off = COLOR_RESET; + strcpy(level_str_buf, log_level_color[hdr->ue_level]); + strcat(level_str_buf, log_level_str[hdr->ue_level]); + strcat(level_str_buf, COLOR_RESET); + level_str = level_str_buf; } else { level_str = log_level_str[hdr->ue_level]; } } else { - sprintf(level_str_buf, "[level=%u]", hdr->ue_level); + sprintf(level_str_buf, "%-3u", hdr->ue_level); + level_str = level_str_buf; } if (MYNEWT_VAL(LOG_CONSOLE_PRETTY_WITH_TIMESTAMP)) { unsigned int us = (unsigned int)hdr->ue_ts % 1000000; unsigned int s = (unsigned int)(hdr->ue_ts / 1000000); - console_printf("[%u.%06u][%s%7s%s]%s%s ", s, us, color, module_name, color_off, level_str, image_hash_str); + console_printf("[%u.%06u][%s%7s%s][%s]%s ", s, us, color, module_name, color_off, level_str, image_hash_str); } else { - console_printf("[%s%7s%s]%s%s ", color, module_name, color_off, level_str, image_hash_str); + console_printf("[%s%7s%s][%s]%s ", color, module_name, color_off, level_str, image_hash_str); } } #else -static void +void log_console_print_hdr(const struct log_entry_hdr *hdr) { console_printf("[ts=" "%" PRIu64 "us, mod=%u level=%u ", diff --git a/sys/log/full/src/log_shell.c b/sys/log/full/src/log_shell.c index 2f05b9940e..a10c08eb11 100644 --- a/sys/log/full/src/log_shell.c +++ b/sys/log/full/src/log_shell.c @@ -39,6 +39,8 @@ #include "tinycbor/compilersupport_p.h" #include "log_cbor_reader/log_cbor_reader.h" +void log_console_print_hdr(const struct log_entry_hdr *hdr); + static uint32_t shell_log_count; @@ -84,6 +86,7 @@ shell_log_dump_entry(struct log *log, struct log_offset *log_offset, int blksz; bool read_data = ueh->ue_etype != LOG_ETYPE_CBOR; bool read_hash = ueh->ue_flags & LOG_FLAGS_IMG_HASH; + bool add_lf = true; if (arg) { arg->count++; @@ -103,18 +106,27 @@ shell_log_dump_entry(struct log *log, struct log_offset *log_offset, data[rc] = 0; } - if (read_hash) { - console_printf("[ih=0x%x%x%x%x]", ueh->ue_imghash[0], ueh->ue_imghash[1], - ueh->ue_imghash[2], ueh->ue_imghash[3]); + /* When LOG_CONSOLE_PRETTY is set use same function to dump log header that + * is used when logs are printed in real time */ + if (MYNEWT_VAL(LOG_CONSOLE_PRETTY)) { + log_console_print_hdr(ueh); + } else { + if (read_hash) { + console_printf("[ih=0x%02x%02x%02x%02x]", ueh->ue_imghash[0], ueh->ue_imghash[1], + ueh->ue_imghash[2], ueh->ue_imghash[3]); + } + console_printf(" [%llu] ", ueh->ue_ts); } - console_printf(" [%llu] ", ueh->ue_ts); + #if MYNEWT_VAL(LOG_SHELL_SHOW_INDEX) console_printf(" [ix=%lu] ", ueh->ue_index); #endif switch (ueh->ue_etype) { case LOG_ETYPE_STRING: - console_write(data, strlen(data)); + dlen = strlen(data); + console_write(data, dlen); + add_lf = dlen < 1 || data[dlen - 1] != '\n'; break; case LOG_ETYPE_CBOR: log_cbor_reader_init(&cbor_reader, log, dptr, len); @@ -135,7 +147,9 @@ shell_log_dump_entry(struct log *log, struct log_offset *log_offset, } } - console_write("\n", 1); + if (add_lf) { + console_write("\n", 1); + } if (arg) { if ((arg->count_limit > 0) && (arg->count - arg->skip >= arg->count_limit)) { return 1; diff --git a/sys/log/full/syscfg.yml b/sys/log/full/syscfg.yml index d48ae9703b..dc35b6c549 100644 --- a/sys/log/full/syscfg.yml +++ b/sys/log/full/syscfg.yml @@ -41,6 +41,10 @@ syscfg.defs: description: > Use color for mod log levels. value: 0 + LOG_CONSOLE_PRETTY_COLOR_MODULES: + description: > + Use color for module names. + value: 0 LOG_CONSOLE_PRETTY_WITH_TIMESTAMP: description: >