From: Christian Babeux Date: Mon, 24 Sep 2012 16:11:44 +0000 (-0400) Subject: Tests: Add a check for color support when printing status X-Git-Tag: v2.1.0-rc4~15 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=2d7e012dc1d47b55bf37e05d9c2a76bad7620e31;p=lttng-tools.git Tests: Add a check for color support when printing status 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 Signed-off-by: David Goulet --- diff --git a/tests/utils.h b/tests/utils.h index 7650865dd..94891c0c6 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -16,10 +16,28 @@ */ #include +#include #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)