[lttng-dev] [PATCH lttng-tools 1/4] Tests: Add a check for color support when printing status

Mathieu Desnoyers mathieu.desnoyers at efficios.com
Thu Sep 20 22:16:39 EDT 2012


* Christian Babeux (christian.babeux at efficios.com) wrote:
> 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 at efficios.com>
> ---
>  tests/utils.h | 22 ++++++++++++++++++++--
>  1 file changed, 20 insertions(+), 2 deletions(-)
> 
> diff --git a/tests/utils.h b/tests/utils.h
> index 7650865..34c8bd6 100644
> --- a/tests/utils.h
> +++ b/tests/utils.h
> @@ -16,10 +16,28 @@
>   */
>  
>  #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(fileno(stdout))) {					\

I'm 100% ok with this approach, just a small optimisation to consider:

change fileno(stdout) for STDOUT_FILENO , as defined by unistd.h.

(same below)

Thanks!

Mathieu

> +		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(fileno(stdout))) {					\
> +		printf("%c[%d;%dmFAIL%c[%dm\n", 0x1B, BRIGHT, RED, 0x1B, 0); \
> +	} else {							\
> +		printf("FAIL\n");					\
> +	}								\
> +} while (0)
> -- 
> 1.7.12
> 
> 
> _______________________________________________
> lttng-dev mailing list
> lttng-dev at lists.lttng.org
> http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

-- 
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com



More information about the lttng-dev mailing list