X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fverbose.c;h=e0f951001d8e3619d94f1b013a0172749f4edd5d;hb=65353dce81a629e042800bb7b86fcd869a76727e;hp=5fd1940cdbbd2039501b5192cd1735bc6343001a;hpb=4521c1e7ae5371ab9d639adc617d17fb4e8ded0c;p=src%2Fapp-framework-binder.git diff --git a/src/verbose.c b/src/verbose.c index 5fd1940c..e0f95100 100644 --- a/src/verbose.c +++ b/src/verbose.c @@ -1,5 +1,5 @@ /* - Copyright (C) 2016, 2017, 2018 "IoT.bzh" + Copyright (C) 2015-2020 "IoT.bzh" author: José Bollo @@ -132,6 +132,19 @@ static const char *prefixes[] = { "<7> DEBUG" }; +static const char *prefixes_colorized[] = { + "<0> " COLOR_EMERGENCY "EMERGENCY" COLOR_DEFAULT, + "<1> " COLOR_ALERT "ALERT" COLOR_DEFAULT, + "<2> " COLOR_CRITICAL "CRITICAL" COLOR_DEFAULT, + "<3> " COLOR_ERROR "ERROR" COLOR_DEFAULT, + "<4> " COLOR_WARNING "WARNING" COLOR_DEFAULT, + "<5> " COLOR_NOTICE "NOTICE" COLOR_DEFAULT, + "<6> " COLOR_INFO "INFO" COLOR_DEFAULT, + "<7> " COLOR_DEBUG "DEBUG" COLOR_DEFAULT +}; + +static int colorize = 0; + static int tty; static const char chars[] = { '\n', '?', ':', ' ', '[', ',', ']' }; @@ -153,7 +166,10 @@ static void _vverbose_(int loglevel, const char *file, int line, const char *fun tty = 1 + isatty(STDERR_FILENO); /* prefix */ - iov[0].iov_base = (void*)prefixes[CROP_LOGLEVEL(loglevel)] + (tty - 1 ? 4 : 0); + if (colorize) + iov[0].iov_base = (void*)prefixes_colorized[CROP_LOGLEVEL(loglevel)] + (tty - 1 ? 4 : 0); + else + iov[0].iov_base = (void*)prefixes[CROP_LOGLEVEL(loglevel)] + (tty - 1 ? 4 : 0); iov[0].iov_len = strlen(iov[0].iov_base); /* " " */ @@ -175,6 +191,13 @@ static void _vverbose_(int loglevel, const char *file, int line, const char *fun iov[n++].iov_len = (size_t)rc; } if (file && (!fmt || tty == 1 || loglevel <= Log_Level_Warning)) { + + if (colorize) + { + iov[n].iov_base = (void*)COLOR_FILE; + iov[n++].iov_len = strlen(COLOR_FILE); + } + /* "[" (!fmt) or " [" (fmt) */ iov[n].iov_base = (void*)&chars[3 + !fmt]; iov[n++].iov_len = 2 - !fmt; @@ -207,6 +230,12 @@ static void _vverbose_(int loglevel, const char *file, int line, const char *fun } iov[n].iov_base = (void*)&chars[6]; iov[n++].iov_len = 1; + + if (colorize) + { + iov[n].iov_base = (void*)COLOR_DEFAULT; + iov[n++].iov_len = strlen(COLOR_DEFAULT); + } } if (n == 2) { /* "?" */ @@ -335,3 +364,12 @@ const char *verbose_name_of_level(int level) return level == CROP_LOGLEVEL(level) ? names[level] : NULL; } +void verbose_colorize() +{ + colorize = 1; +} + +int verbose_is_colorized() +{ + return colorize; +}