ovs-ofctl: Add option for color output to dump-flows command.
authorQuentin Monnet <quentin.monnet@6wind.com>
Wed, 2 Mar 2016 14:56:16 +0000 (15:56 +0100)
committerBen Pfaff <blp@ovn.org>
Fri, 18 Mar 2016 21:01:30 +0000 (14:01 -0700)
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.monnet@6wind.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
AUTHORS
lib/util.c
lib/util.h
utilities/ovs-ofctl.c

diff --git a/AUTHORS b/AUTHORS
index 541c2f8..ddab436 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -162,6 +162,7 @@ Philippe Jung           phil.jung@free.fr
 Pim van den Berg        pim@nethuis.nl
 pritesh                 pritesh.kothari@cisco.com
 Pravin B Shelar         pshelar@nicira.com
+Quentin Monnet          quentin.monnet@6wind.com
 Raju Subramanian        rsubramanian@nicira.com
 Rami Rosen              ramirose@gmail.com
 Ramu Ramamurthy         ramu.ramamurthy@us.ibm.com
index f06dee5..e3519f2 100644 (file)
@@ -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
 \f
 char *
index afd8e37..389c093 100644 (file)
@@ -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
 \f
 char *ovs_format_message(int error);
index 7bcfc66..db59ff6 100644 (file)
@@ -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,30 @@ 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;
+                } else if (!strcasecmp(optarg, "auto")
+                           || !strcasecmp(optarg, "tty")
+                           || !strcasecmp(optarg, "if-tty")) {
+                    /* Determine whether we need colors, i.e. whether standard
+                     * output is a 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 +446,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] control use of color in output\n"
            "  -h, --help                  display this help message\n"
            "  -V, --version               display version information\n");
     exit(EXIT_SUCCESS);