From dfe85ca516c207eadc4ae77066c6706f17068f20 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lo=C3=AFc=20Collignon?= Date: Mon, 22 Oct 2018 16:44:03 +0200 Subject: [PATCH] Added an option to color out the ouput MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Enhance the readability using basic colorization to the logging system. As the '--call' option is almost unused, it doesn't require to have a shortname. So we use the '-c' and '--color' for the new colorization option, and keep only the longname for the 'call' option. Change-Id: I095fc9f38133fb742e0f0003540cd120feec8f5e Signed-off-by: Loïc Collignon --- docs/afb-daemon-options.md | 7 ++++++- src/afb-config.c | 12 ++++++++---- src/afb-export.c | 2 +- src/verbose.c | 40 +++++++++++++++++++++++++++++++++++++++- src/verbose.h | 14 ++++++++++++++ 5 files changed, 68 insertions(+), 7 deletions(-) diff --git a/docs/afb-daemon-options.md b/docs/afb-daemon-options.md index 020991ec..0588fef2 100644 --- a/docs/afb-daemon-options.md +++ b/docs/afb-daemon-options.md @@ -4,6 +4,7 @@ The launch options for binder **afb-daemon** are: ``` -v, --verbose Verbose Mode, repeat to increase verbosity + -c, --color Colorize the ouput -q, --quiet Quiet Mode, repeat to decrease verbosity -l, --log=xxxx Tune log level --foreground Get all in foreground mode @@ -40,7 +41,7 @@ The launch options for binder **afb-daemon** are: --traceglob=xxxx Log the globals: none, all --traceditf=xxxx Log the daemons: no, common, all --tracesvc=xxxx Log the services: no, all - -c, --call=xxxx call at start format of val: API/VERB:json-args + --call=xxxx call at start format of val: API/VERB:json-args --no-httpd Forbid HTTP service -e, --exec Execute the remaining arguments -M, --monitoring Enable HTTP monitoring at /monitoring/ @@ -62,6 +63,10 @@ Display version and copyright Increases the verbosity, can be repeated +## color + +Add basic colorization to the ouput. + ## quiet Decreases the verbosity, can be repeated diff --git a/src/afb-config.c b/src/afb-config.c index 669bbc57..fcc69a41 100644 --- a/src/afb-config.c +++ b/src/afb-config.c @@ -107,9 +107,8 @@ #define SET_TRACEDITF 25 #define SET_TRACESVC 26 #endif - -#define SET_TRAP_FAULTS 27 - +#define SET_TRAP_FAULTS 27 +#define ADD_CALL 28 #if defined(WITH_DBUS_TRANSPARENCY) # define ADD_DBUS_CLIENT 30 # define ADD_DBUS_SERVICE 31 @@ -118,7 +117,7 @@ #define ADD_AUTO_API 'A' #define ADD_BINDING 'b' #define SET_CONFIG 'C' -#define ADD_CALL 'c' +#define SET_COLOR 'c' #define SET_DAEMON 'D' #define SET_EXEC 'e' #define GET_HELP 'h' @@ -151,6 +150,7 @@ struct option_desc { static struct option_desc optdefs[] = { /* *INDENT-OFF* */ {SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"}, + {SET_COLOR, 0, "color", "Colorize the ouput"}, {SET_QUIET, 0, "quiet", "Quiet Mode, repeat to decrease verbosity"}, {SET_LOG, 1, "log", "Tune log level"}, @@ -756,6 +756,10 @@ static void parse_arguments_inner(int argc, char **argv, struct json_object *con verbose_inc(); break; + case SET_COLOR: + verbose_colorize(); + break; + case SET_QUIET: verbose_dec(); break; diff --git a/src/afb-export.c b/src/afb-export.c index fdd4573f..fb1888ed 100644 --- a/src/afb-export.c +++ b/src/afb-export.c @@ -274,7 +274,7 @@ static void vverbose_cb(struct afb_api_x3 *closure, int level, const char *file, if (!fmt || vasprintf(&p, fmt, args) < 0) vverbose(level, file, line, function, fmt, args); else { - verbose(level, file, line, function, "[API %s] %s", export->api.apiname, p); + verbose(level, file, line, function, (verbose_is_colorized() == 0 ? "[API %s] %s" : COLOR_API "[API %s]" COLOR_DEFAULT " %s"), export->api.apiname, p); free(p); } } diff --git a/src/verbose.c b/src/verbose.c index 5fd1940c..34dad746 100644 --- a/src/verbose.c +++ b/src/verbose.c @@ -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; +} diff --git a/src/verbose.h b/src/verbose.h index bd36f97e..183932bb 100644 --- a/src/verbose.h +++ b/src/verbose.h @@ -109,6 +109,8 @@ extern void verbose_inc(); extern void verbose_clear(); extern void verbose_add(int level); extern void verbose_sub(int level); +extern void verbose_colorize(); +extern int verbose_is_colorized(); extern int verbose_level_of_name(const char *name); extern const char *verbose_name_of_level(int level); @@ -120,3 +122,15 @@ extern void verbosity_set(int verbo); extern int verbosity_from_mask(int mask); extern int verbosity_to_mask(int verbo); +#define COLOR_EMERGENCY "\x1B[101m" +#define COLOR_ALERT "\x1B[43m" +#define COLOR_CRITICAL "\x1B[41m" +#define COLOR_ERROR "\x1B[91m" +#define COLOR_WARNING "\x1B[93m" +#define COLOR_NOTICE "\x1B[94m" +#define COLOR_INFO "\x1B[96m" +#define COLOR_DEBUG "\x1B[95m" +#define COLOR_API "\x1B[1m" +#define COLOR_FILE "\x1B[90m" +#define COLOR_DEFAULT "\x1B[0m" + -- 2.16.6