If the output is a terminal, use color codes to mark OK, warn, and
FAIL messages with green, bold, and bright red accents, respectively.

Signed-off-by: Philipp Zabel <p.za...@pengutronix.de>
---
 utils/v4l2-compliance/v4l2-compliance.cpp | 11 ++++++++---
 utils/v4l2-compliance/v4l2-compliance.h   | 15 ++++++++++++---
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/utils/v4l2-compliance/v4l2-compliance.cpp 
b/utils/v4l2-compliance/v4l2-compliance.cpp
index f7611d75cb1b..488e746d055d 100644
--- a/utils/v4l2-compliance/v4l2-compliance.cpp
+++ b/utils/v4l2-compliance/v4l2-compliance.cpp
@@ -89,6 +89,7 @@ static int grand_total, grand_ok, grand_warnings;
 bool show_info;
 bool no_progress;
 bool show_warnings = true;
+bool show_colors;
 bool exit_on_fail;
 bool exit_on_warn;
 bool is_vivid;
@@ -250,15 +251,17 @@ const char *ok(int res)
        static char buf[100];
 
        if (res == ENOTTY) {
-               strcpy(buf, "OK (Not Supported)");
+               strcpy(buf, show_colors ?
+                      COLOR_GREEN("OK") " (Not Supported)" :
+                      "OK (Not Supported)");
                res = 0;
        } else {
-               strcpy(buf, "OK");
+               strcpy(buf, show_colors ? COLOR_GREEN("OK") : "OK");
        }
        tests_total++;
        if (res) {
                app_result = res;
-               sprintf(buf, "FAIL");
+               sprintf(buf, show_colors ? COLOR_RED("FAIL") : "FAIL");
        } else {
                tests_ok++;
        }
@@ -1630,6 +1633,8 @@ int main(int argc, char **argv)
        bool direct = !options[OptUseWrapper];
        int fd;
 
+       show_colors = isatty(STDOUT_FILENO);
+
        if (type == MEDIA_TYPE_UNKNOWN)
                type = mi_media_detect_type(device.c_str());
        if (type == MEDIA_TYPE_CANT_STAT) {
diff --git a/utils/v4l2-compliance/v4l2-compliance.h 
b/utils/v4l2-compliance/v4l2-compliance.h
index f7e1ea5d7124..7909c3822cd0 100644
--- a/utils/v4l2-compliance/v4l2-compliance.h
+++ b/utils/v4l2-compliance/v4l2-compliance.h
@@ -52,6 +52,7 @@
 extern bool show_info;
 extern bool show_warnings;
 extern bool no_progress;
+extern bool show_colors;
 extern bool exit_on_fail;
 extern bool exit_on_warn;
 extern bool is_vivid; // We're testing the vivid driver
@@ -185,17 +186,24 @@ private:
        std::set<int> fhs;
 };
 
+#define COLOR_GREEN(s) "\033[32m" s "\033[0m"
+#define COLOR_RED(s) "\033[1;31m" s "\033[0m"
+#define COLOR_BOLD(s) "\033[1m" s "\033[0m"
+
 #define info(fmt, args...)                                     \
        do {                                                    \
                if (show_info)                                  \
-                       printf("\t\tinfo: " fmt, ##args);       \
+                       printf("\t\tinfo: " fmt, ##args);       \
        } while (0)
 
 #define warn(fmt, args...)                                     \
        do {                                                    \
                warnings++;                                     \
                if (show_warnings)                              \
-                       printf("\t\twarn: %s(%d): " fmt, __FILE__, __LINE__, 
##args);   \
+                       printf("\t\t%s: %s(%d): " fmt,          \
+                              show_colors ?                    \
+                              COLOR_BOLD("warn") : "warn",     \
+                              __FILE__, __LINE__, ##args);     \
                if (exit_on_warn)                               \
                        exit(1);                                \
        } while (0)
@@ -218,7 +226,8 @@ private:
 
 #define fail(fmt, args...)                                             \
 ({                                                                     \
-       printf("\t\tfail: %s(%d): " fmt, __FILE__, __LINE__, ##args);   \
+       printf("\t\t%s: %s(%d): " fmt, show_colors ?                    \
+              COLOR_RED("fail") : "fail", __FILE__, __LINE__, ##args); \
        if (exit_on_fail)                                               \
                exit(1);                                                \
        1;                                                              \
-- 
2.20.1

Reply via email to