Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/log: Use colors for module name when dumping log #3301

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions sys/log/full/src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,27 @@ log_module_register(uint8_t id, const char *name)
return id;
}

static const char *const default_modules[] = {
"DEFAULT",
"OS",
"NEWTMGR",
"NIMBLE_CTLR",
"NIMBLE_HOST",
"NFFS",
"REBOOT",
"IOTIVITY",
};

const char *
log_module_get_name(uint8_t module)
{
int idx;
const char *name;

if (module < LOG_MODULE_TEST) {
return default_modules[module];
}

/* Find module defined in syscfg.logcfg sections */
name = logcfg_log_module_name(module);

Expand Down
104 changes: 81 additions & 23 deletions sys/log/full/src/log_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,73 @@ 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 ""
#define COLOR_WRN ""
#define COLOR_ERR ""
#define COLOR_CRI ""
#define COLOR_RESET ""
#define log_module_color(hdr, on, off)
#endif

static const char * const log_level_color[] = {
Expand All @@ -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);
Expand All @@ -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 ",
Expand Down
26 changes: 20 additions & 6 deletions sys/log/full/src/log_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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++;
Expand All @@ -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);
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions sys/log/full/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >
Expand Down
Loading