Add an option to ovs-ofctl utility so as to obtain colorized output in tty, for easier reading. Currently, only the dump-flows command supports colors.
A new `--color` option has been added to ovs-ofctl so as to indicate whether color markers should be used or not. It can be set to `always` (force colors), `never` (no colors) or `auto` (use colors only if output is a tty). If provided without any value, it is the same as `auto`. If the option is not provided at all, colors are disabled by default. Examples: This first call will output colorized flows: ovs-ofctl dump-flows br0 --color=always These two calls will produce colorized output on a tty, but they will not use color markers if the output is redirected to a file or piped into another command: ovs-ofctl dump-flows br0 --color=auto ovs-ofctl dump-flows br0 --color These two calls will not use color markers: ovs-ofctl dump-flows br0 --color=never ovs-ofctl dump-flows br0 The result of this option is stored into a variable which is to be forwarded (in next commits) as a function argument until it reaches the functions that print the elements of the flows. Signed-off-by: Quentin Monnet <quentin.mon...@6wind.com> --- lib/util.c | 11 +++++++++++ lib/util.h | 2 ++ utilities/ovs-ofctl.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/lib/util.c b/lib/util.c index f06dee5e7f6b..e3519f2bd3ac 100644 --- a/lib/util.c +++ b/lib/util.c @@ -2041,6 +2041,17 @@ xsleep(unsigned int seconds) ovsrcu_quiesce_end(); } +/* Determine whether standard output is a tty or not. This is useful to decide + * whether to use color output or not when --color option for utilities is set + * to `auto`. + */ +bool +is_stdout_a_tty(void) +{ + char const *t = getenv("TERM"); + return (isatty(STDOUT_FILENO) && t && strcmp(t, "dumb") != 0); +} + #ifdef _WIN32 char * diff --git a/lib/util.h b/lib/util.h index afd8e378c337..389c093a2fd7 100644 --- a/lib/util.h +++ b/lib/util.h @@ -613,6 +613,8 @@ ovs_be128_is_zero(const ovs_be128 *val) void xsleep(unsigned int seconds); +bool is_stdout_a_tty(void); + #ifdef _WIN32 char *ovs_format_message(int error); diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c index 7bcfc66a351f..c535005797af 100644 --- a/utilities/ovs-ofctl.c +++ b/utilities/ovs-ofctl.c @@ -75,6 +75,9 @@ VLOG_DEFINE_THIS_MODULE(ofctl); */ static bool bundle = false; +/* --color: Use color markers. */ +static bool enable_color; + /* --strict: Use strict matching for flow mod commands? Additionally governs * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match. */ @@ -170,6 +173,7 @@ parse_options(int argc, char *argv[]) OPT_RSORT, OPT_UNIXCTL, OPT_BUNDLE, + OPT_COLOR, DAEMON_OPTION_ENUMS, OFP_VERSION_OPTION_ENUMS, VLOG_OPTION_ENUMS @@ -188,6 +192,7 @@ parse_options(int argc, char *argv[]) {"help", no_argument, NULL, 'h'}, {"option", no_argument, NULL, 'o'}, {"bundle", no_argument, NULL, OPT_BUNDLE}, + {"color", optional_argument, NULL, OPT_COLOR}, DAEMON_LONG_OPTIONS, OFP_VERSION_LONG_OPTIONS, VLOG_LONG_OPTIONS, @@ -289,6 +294,34 @@ parse_options(int argc, char *argv[]) unixctl_path = optarg; break; + case OPT_COLOR: + if (optarg) { + if (!strcasecmp (optarg, "always") + || !strcasecmp (optarg, "yes") + || !strcasecmp (optarg, "force")) { + enable_color = true; + } + else if (!strcasecmp (optarg, "never") + || !strcasecmp (optarg, "no") + || !strcasecmp (optarg, "none")) { + enable_color = false; + } + /* If --color is empty or set to 'auto', determine whether we + * need colors, i.e. whether standard output is a tty. */ + else if (!strcasecmp (optarg, "auto") + || !strcasecmp (optarg, "tty") + || !strcasecmp (optarg, "if-tty")) { + enable_color = is_stdout_a_tty(); + } + else { + ovs_fatal(0, "incorrect value `%s' for --color", optarg); + } + } + else { + enable_color = is_stdout_a_tty(); + } + break; + DAEMON_OPTION_HANDLERS OFP_VERSION_OPTION_HANDLERS VLOG_OPTION_HANDLERS @@ -417,6 +450,7 @@ usage(void) " --sort[=field] sort in ascending order\n" " --rsort[=field] sort in descending order\n" " --unixctl=SOCKET set control socket name\n" + " --color[=always|never|auto] use colors markers\n" " -h, --help display this help message\n" " -V, --version display version information\n"); exit(EXIT_SUCCESS); -- 1.9.1 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev