Added an option to color out the ouput 59/17359/3
authorLoïc Collignon <loic.collignon@iot.bzh>
Mon, 22 Oct 2018 14:44:03 +0000 (16:44 +0200)
committerLoïc Collignon <loic.collignon@iot.bzh>
Tue, 23 Oct 2018 09:56:42 +0000 (11:56 +0200)
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 <loic.collignon@iot.bzh>
docs/afb-daemon-options.md
src/afb-config.c
src/afb-export.c
src/verbose.c
src/verbose.h

index 020991e..0588fef 100644 (file)
@@ -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 <ROOT>/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
index 669bbc5..fcc69a4 100644 (file)
 #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
 #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;
index fdd4573..fb1888e 100644 (file)
@@ -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);
        }
 }
index 5fd1940..34dad74 100644 (file)
@@ -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;
+}
index bd36f97..183932b 100644 (file)
@@ -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"
+