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>
```
-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
--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/
Increases the verbosity, can be repeated
+## color
+
+Add basic colorization to the ouput.
+
## quiet
Decreases the verbosity, can be repeated
#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'
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"},
verbose_inc();
break;
+ case SET_COLOR:
+ verbose_colorize();
+ break;
+
case SET_QUIET:
verbose_dec();
break;
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);
}
}
"<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', '?', ':', ' ', '[', ',', ']' };
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);
/* " " */
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;
}
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) {
/* "?" */
return level == CROP_LOGLEVEL(level) ? names[level] : NULL;
}
+void verbose_colorize()
+{
+ colorize = 1;
+}
+
+int verbose_is_colorized()
+{
+ return colorize;
+}
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);
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"
+