1 #include <linux/kernel.h>
6 int perf_use_color_default
= -1;
8 int perf_config_colorbool(const char *var
, const char *value
, int stdout_is_tty
)
11 if (!strcasecmp(value
, "never"))
13 if (!strcasecmp(value
, "always"))
15 if (!strcasecmp(value
, "auto"))
19 /* Missing or explicit false to turn off colorization */
20 if (!perf_config_bool(var
, value
))
23 /* any normal truth value defaults to 'auto' */
25 if (stdout_is_tty
< 0)
26 stdout_is_tty
= isatty(1);
27 if (stdout_is_tty
|| pager_in_use()) {
28 char *term
= getenv("TERM");
29 if (term
&& strcmp(term
, "dumb"))
35 int perf_color_default_config(const char *var
, const char *value
, void *cb
)
37 if (!strcmp(var
, "color.ui")) {
38 perf_use_color_default
= perf_config_colorbool(var
, value
, -1);
42 return perf_default_config(var
, value
, cb
);
45 static int __color_vsnprintf(char *bf
, size_t size
, const char *color
,
46 const char *fmt
, va_list args
, const char *trail
)
53 if (perf_use_color_default
< 0) {
54 if (isatty(1) || pager_in_use())
55 perf_use_color_default
= 1;
57 perf_use_color_default
= 0;
60 if (perf_use_color_default
&& *color
)
61 r
+= scnprintf(bf
, size
, "%s", color
);
62 r
+= vscnprintf(bf
+ r
, size
- r
, fmt
, args
);
63 if (perf_use_color_default
&& *color
)
64 r
+= scnprintf(bf
+ r
, size
- r
, "%s", PERF_COLOR_RESET
);
66 r
+= scnprintf(bf
+ r
, size
- r
, "%s", trail
);
70 /* Colors are not included in return value */
71 static int __color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
79 if (perf_use_color_default
< 0) {
80 if (isatty(fileno(fp
)) || pager_in_use())
81 perf_use_color_default
= 1;
83 perf_use_color_default
= 0;
86 if (perf_use_color_default
&& *color
)
87 fprintf(fp
, "%s", color
);
88 r
+= vfprintf(fp
, fmt
, args
);
89 if (perf_use_color_default
&& *color
)
90 fprintf(fp
, "%s", PERF_COLOR_RESET
);
94 int color_vsnprintf(char *bf
, size_t size
, const char *color
,
95 const char *fmt
, va_list args
)
97 return __color_vsnprintf(bf
, size
, color
, fmt
, args
, NULL
);
100 int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
, va_list args
)
102 return __color_vfprintf(fp
, color
, fmt
, args
);
105 int color_snprintf(char *bf
, size_t size
, const char *color
,
106 const char *fmt
, ...)
112 r
= color_vsnprintf(bf
, size
, color
, fmt
, args
);
117 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
123 r
= color_vfprintf(fp
, color
, fmt
, args
);
129 * This function splits the buffer by newlines and colors the lines individually.
131 * Returns 0 on success.
133 int color_fwrite_lines(FILE *fp
, const char *color
,
134 size_t count
, const char *buf
)
137 return fwrite(buf
, count
, 1, fp
) != 1;
140 char *p
= memchr(buf
, '\n', count
);
142 if (p
!= buf
&& (fputs(color
, fp
) < 0 ||
143 fwrite(buf
, p
? (size_t)(p
- buf
) : count
, 1, fp
) != 1 ||
144 fputs(PERF_COLOR_RESET
, fp
) < 0))
148 if (fputc('\n', fp
) < 0)
150 count
-= p
+ 1 - buf
;
156 const char *get_percent_color(double percent
)
158 const char *color
= PERF_COLOR_NORMAL
;
161 * We color high-overhead entries in red, mid-overhead
162 * entries in green - and keep the low overhead places
165 if (fabs(percent
) >= MIN_RED
)
166 color
= PERF_COLOR_RED
;
168 if (fabs(percent
) > MIN_GREEN
)
169 color
= PERF_COLOR_GREEN
;
174 int percent_color_fprintf(FILE *fp
, const char *fmt
, double percent
)
179 color
= get_percent_color(percent
);
180 r
= color_fprintf(fp
, color
, fmt
, percent
);
185 int value_color_snprintf(char *bf
, size_t size
, const char *fmt
, double value
)
187 const char *color
= get_percent_color(value
);
188 return color_snprintf(bf
, size
, color
, fmt
, value
);
191 int percent_color_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
197 percent
= va_arg(args
, double);
199 return value_color_snprintf(bf
, size
, fmt
, percent
);
202 int percent_color_len_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
210 len
= va_arg(args
, int);
211 percent
= va_arg(args
, double);
214 color
= get_percent_color(percent
);
215 return color_snprintf(bf
, size
, color
, fmt
, len
, percent
);