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> --- utilities/ovs-ofctl.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c index 96d6c89d92bf..a7dea78bcd3a 100644 --- a/utilities/ovs-ofctl.c +++ b/utilities/ovs-ofctl.c @@ -73,6 +73,9 @@ VLOG_DEFINE_THIS_MODULE(ofctl); */ static bool bundle = false; +/* --color: Use color markers. */ +static int color_option; + /* --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. */ @@ -168,6 +171,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 @@ -186,6 +190,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, @@ -287,6 +292,31 @@ 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")) { + color_option = 1; + } + else if (!strcasecmp (optarg, "never") + || !strcasecmp (optarg, "no") + || !strcasecmp (optarg, "none")) { + color_option = 0; + } + else if (!strcasecmp (optarg, "auto") + || !strcasecmp (optarg, "tty") + || !strcasecmp (optarg, "if-tty")) { + color_option = 2; + } + else { + usage(); + } + } + else + color_option = 2; + break; + DAEMON_OPTION_HANDLERS OFP_VERSION_OPTION_HANDLERS VLOG_OPTION_HANDLERS @@ -331,6 +361,13 @@ parse_options(int argc, char *argv[]) allowed_protocols &= version_protocols; mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap( allowed_protocols)); + + /* If --color is set to 'auto', determine whether we need colors, i.e. + * whether standard output is a tty. */ + char const *t = getenv("TERM"); + if (color_option == 2) { + color_option = isatty(STDOUT_FILENO) && t && strcmp(t, "dumb") != 0; + } } static void @@ -414,6 +451,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