Skip to content

Commit

Permalink
perf_counter tools: Dont output in color on !tty
Browse files Browse the repository at this point in the history
Dont emit ASCII color characters if the terminal is not a tty,
such as when perf report gets redirected into a file.

Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
LKML-Reference: <new-submission>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
  • Loading branch information
Ingo Molnar committed Jun 4, 2009
1 parent 8fc0321 commit 13d0ab5
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Documentation/perf_counter/util/color.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "cache.h"
#include "color.h"

int perf_use_color_default = 0;
int perf_use_color_default = -1;

static int parse_color(const char *name, int len)
{
Expand Down Expand Up @@ -169,10 +169,20 @@ static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
{
int r = 0;

if (*color)
/*
* Auto-detect:
*/
if (perf_use_color_default < 0) {
if (isatty(1) || pager_in_use())
perf_use_color_default = 1;
else
perf_use_color_default = 0;
}

if (perf_use_color_default && *color)
r += fprintf(fp, "%s", color);
r += vfprintf(fp, fmt, args);
if (*color)
if (perf_use_color_default && *color)
r += fprintf(fp, "%s", PERF_COLOR_RESET);
if (trail)
r += fprintf(fp, "%s", trail);
Expand All @@ -185,6 +195,7 @@ int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
{
va_list args;
int r;

va_start(args, fmt);
r = color_vfprintf(fp, color, fmt, args, NULL);
va_end(args);
Expand Down

0 comments on commit 13d0ab5

Please sign in to comment.