Tests: Add a check for color support when printing status
authorChristian Babeux <christian.babeux@efficios.com>
Mon, 24 Sep 2012 16:11:44 +0000 (12:11 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Mon, 24 Sep 2012 20:25:01 +0000 (16:25 -0400)
When printing the status of test is OK or FAIL, check if stdout is
attached to a terminal device. This way the output is not cluttered with
useless escape characters. Some use cases where we don't want colors:

$ ./sometest | less

$ ./sometest > a.log

Signed-off-by: Christian Babeux <christian.babeux@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
tests/utils.h

index 7650865dd57c64411840db21541454ecf5774967..94891c0c64a3b2d8f29be1a922411f018466f677 100644 (file)
  */
 
 #include <stdio.h>
+#include <unistd.h>
 
 #define BRIGHT 1
 #define GREEN 32
 #define RED 31
 
-#define PRINT_OK() printf("%c[%d;%dmOK%c[%dm\n", 0x1B, BRIGHT, GREEN, 0x1B, 0);
-#define PRINT_FAIL() printf("%c[%d;%dmFAIL%c[%dm\n", 0x1B, BRIGHT, RED, 0x1B, 0);
+#define PRINT_OK()                                                     \
+do {                                                                   \
+       /* Check for color support */                                   \
+       if (isatty(STDOUT_FILENO)) {                                    \
+               printf("%c[%d;%dmOK%c[%dm\n", 0x1B, BRIGHT, GREEN, 0x1B, 0); \
+       } else {                                                        \
+               printf("OK\n");                                 \
+       }                                                               \
+} while (0)
+
+#define PRINT_FAIL() \
+do {                                                                   \
+       /* Check for color support */                                   \
+       if (isatty(STDOUT_FILENO)) {                                    \
+               printf("%c[%d;%dmFAIL%c[%dm\n", 0x1B, BRIGHT, RED, 0x1B, 0); \
+       } else {                                                        \
+               printf("FAIL\n");                                       \
+       }                                                               \
+} while (0)
This page took 0.02673 seconds and 4 git commands to generate.