summaryrefslogtreecommitdiffstats
path: root/src/logging.c
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2018-01-16 02:56:01 +0100
committerHarald Welte <laforge@gnumonks.org>2018-01-17 11:13:58 +0000
commitf3fa3694f596b50a5d85ae1e8b2a8627a3308a7e (patch)
tree59bd7c029c20f34c00f8188df9254cb8c77462a7 /src/logging.c
parent886e548ab080896da6760036f38b93ff97fd01a4 (diff)
logging: color the log level
When log_set_use_color() is enabled, color the log level string according to the log level. The log line before and after the log level is printed in the category's color. ERROR and FATAL are red, NOTICE is yellow, INFO is green and DEBUG is blue. The default behavior remains unchanged. Change-Id: If2e52ae9ab83e538e04321c338e3fdffb2c7f9d3
Diffstat (limited to 'src/logging.c')
-rw-r--r--src/logging.c30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/logging.c b/src/logging.c
index 80fc7d2c..c01294cb 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -298,6 +298,23 @@ static const char* color(int subsys)
return NULL;
}
+static const struct value_string level_colors[] = {
+ { LOGL_DEBUG, "\033[1;34m" },
+ { LOGL_INFO, "\033[1;32m" },
+ { LOGL_NOTICE, "\033[1;33m" },
+ { LOGL_ERROR, "\033[1;31m" },
+ { LOGL_FATAL, "\033[1;31m" },
+ { 0, NULL }
+};
+
+static const char *level_color(int level)
+{
+ const char *c = get_value_string_or_null(level_colors, level);
+ if (!c)
+ return get_value_string(level_colors, LOGL_FATAL);
+ return c;
+}
+
const char* log_category_name(int subsys)
{
if (subsys < osmo_log_info->num_cat)
@@ -312,12 +329,13 @@ static void _output(struct log_target *target, unsigned int subsys,
{
char buf[4096];
int ret, len = 0, offset = 0, rem = sizeof(buf);
+ const char *c_subsys = NULL;
/* are we using color */
if (target->use_color) {
- const char *c = color(subsys);
- if (c) {
- ret = snprintf(buf + offset, rem, "%s", c);
+ c_subsys = color(subsys);
+ if (c_subsys) {
+ ret = snprintf(buf + offset, rem, c_subsys);
if (ret < 0)
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
@@ -354,7 +372,11 @@ static void _output(struct log_target *target, unsigned int subsys,
OSMO_SNPRINTF_RET(ret, rem, offset, len);
}
if (target->print_level) {
- ret = snprintf(buf + offset, rem, "%s ", log_level_str(level));
+ ret = snprintf(buf + offset, rem, "%s%s%s%s ",
+ target->use_color ? level_color(level) : "",
+ log_level_str(level),
+ target->use_color ? "\033[0;m" : "",
+ c_subsys ? c_subsys : "");
if (ret < 0)
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);