1 #include <linux/kernel.h>
6 int perf_use_color_default
= -1;
8 static int parse_color(const char *name
, int len
)
10 static const char * const color_names
[] = {
11 "normal", "black", "red", "green", "yellow",
12 "blue", "magenta", "cyan", "white"
17 for (i
= 0; i
< (int)ARRAY_SIZE(color_names
); i
++) {
18 const char *str
= color_names
[i
];
19 if (!strncasecmp(name
, str
, len
) && !str
[len
])
22 i
= strtol(name
, &end
, 10);
23 if (end
- name
== len
&& i
>= -1 && i
<= 255)
28 static int parse_attr(const char *name
, int len
)
30 static const int attr_values
[] = { 1, 2, 4, 5, 7 };
31 static const char * const attr_names
[] = {
32 "bold", "dim", "ul", "blink", "reverse"
36 for (i
= 0; i
< ARRAY_SIZE(attr_names
); i
++) {
37 const char *str
= attr_names
[i
];
38 if (!strncasecmp(name
, str
, len
) && !str
[len
])
39 return attr_values
[i
];
44 void color_parse(const char *value
, const char *var
, char *dst
)
46 color_parse_mem(value
, strlen(value
), var
, dst
);
49 void color_parse_mem(const char *value
, int value_len
, const char *var
,
52 const char *ptr
= value
;
58 if (!strncasecmp(value
, "reset", len
)) {
59 strcpy(dst
, PERF_COLOR_RESET
);
63 /* [fg [bg]] [attr] */
65 const char *word
= ptr
;
68 while (len
> 0 && !isspace(word
[wordlen
])) {
74 while (len
> 0 && isspace(*ptr
)) {
79 val
= parse_color(word
, wordlen
);
91 val
= parse_attr(word
, wordlen
);
92 if (val
< 0 || attr
!= -1)
97 if (attr
>= 0 || fg
>= 0 || bg
>= 0) {
113 dst
+= sprintf(dst
, "38;5;%d", fg
);
123 dst
+= sprintf(dst
, "48;5;%d", bg
);
131 die("bad color value '%.*s' for variable '%s'", value_len
, value
, var
);
134 int perf_config_colorbool(const char *var
, const char *value
, int stdout_is_tty
)
137 if (!strcasecmp(value
, "never"))
139 if (!strcasecmp(value
, "always"))
141 if (!strcasecmp(value
, "auto"))
145 /* Missing or explicit false to turn off colorization */
146 if (!perf_config_bool(var
, value
))
149 /* any normal truth value defaults to 'auto' */
151 if (stdout_is_tty
< 0)
152 stdout_is_tty
= isatty(1);
153 if (stdout_is_tty
|| (pager_in_use() && pager_use_color
)) {
154 char *term
= getenv("TERM");
155 if (term
&& strcmp(term
, "dumb"))
161 int perf_color_default_config(const char *var
, const char *value
, void *cb
)
163 if (!strcmp(var
, "color.ui")) {
164 perf_use_color_default
= perf_config_colorbool(var
, value
, -1);
168 return perf_default_config(var
, value
, cb
);
171 static int __color_vsnprintf(char *bf
, size_t size
, const char *color
,
172 const char *fmt
, va_list args
, const char *trail
)
179 if (perf_use_color_default
< 0) {
180 if (isatty(1) || pager_in_use())
181 perf_use_color_default
= 1;
183 perf_use_color_default
= 0;
186 if (perf_use_color_default
&& *color
)
187 r
+= scnprintf(bf
, size
, "%s", color
);
188 r
+= vscnprintf(bf
+ r
, size
- r
, fmt
, args
);
189 if (perf_use_color_default
&& *color
)
190 r
+= scnprintf(bf
+ r
, size
- r
, "%s", PERF_COLOR_RESET
);
192 r
+= scnprintf(bf
+ r
, size
- r
, "%s", trail
);
196 static int __color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
197 va_list args
, const char *trail
)
204 if (perf_use_color_default
< 0) {
205 if (isatty(fileno(fp
)) || pager_in_use())
206 perf_use_color_default
= 1;
208 perf_use_color_default
= 0;
211 if (perf_use_color_default
&& *color
)
212 r
+= fprintf(fp
, "%s", color
);
213 r
+= vfprintf(fp
, fmt
, args
);
214 if (perf_use_color_default
&& *color
)
215 r
+= fprintf(fp
, "%s", PERF_COLOR_RESET
);
217 r
+= fprintf(fp
, "%s", trail
);
221 int color_vsnprintf(char *bf
, size_t size
, const char *color
,
222 const char *fmt
, va_list args
)
224 return __color_vsnprintf(bf
, size
, color
, fmt
, args
, NULL
);
227 int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
, va_list args
)
229 return __color_vfprintf(fp
, color
, fmt
, args
, NULL
);
232 int color_snprintf(char *bf
, size_t size
, const char *color
,
233 const char *fmt
, ...)
239 r
= color_vsnprintf(bf
, size
, color
, fmt
, args
);
244 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
250 r
= color_vfprintf(fp
, color
, fmt
, args
);
255 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
260 r
= __color_vfprintf(fp
, color
, fmt
, args
, "\n");
266 * This function splits the buffer by newlines and colors the lines individually.
268 * Returns 0 on success.
270 int color_fwrite_lines(FILE *fp
, const char *color
,
271 size_t count
, const char *buf
)
274 return fwrite(buf
, count
, 1, fp
) != 1;
277 char *p
= memchr(buf
, '\n', count
);
279 if (p
!= buf
&& (fputs(color
, fp
) < 0 ||
280 fwrite(buf
, p
? (size_t)(p
- buf
) : count
, 1, fp
) != 1 ||
281 fputs(PERF_COLOR_RESET
, fp
) < 0))
285 if (fputc('\n', fp
) < 0)
287 count
-= p
+ 1 - buf
;
293 const char *get_percent_color(double percent
)
295 const char *color
= PERF_COLOR_NORMAL
;
298 * We color high-overhead entries in red, mid-overhead
299 * entries in green - and keep the low overhead places
302 if (fabs(percent
) >= MIN_RED
)
303 color
= PERF_COLOR_RED
;
305 if (fabs(percent
) > MIN_GREEN
)
306 color
= PERF_COLOR_GREEN
;
311 int percent_color_fprintf(FILE *fp
, const char *fmt
, double percent
)
316 color
= get_percent_color(percent
);
317 r
= color_fprintf(fp
, color
, fmt
, percent
);
322 int value_color_snprintf(char *bf
, size_t size
, const char *fmt
, double value
)
324 const char *color
= get_percent_color(value
);
325 return color_snprintf(bf
, size
, color
, fmt
, value
);
328 int percent_color_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
334 percent
= va_arg(args
, double);
336 return value_color_snprintf(bf
, size
, fmt
, percent
);
339 int percent_color_len_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
347 len
= va_arg(args
, int);
348 percent
= va_arg(args
, double);
351 color
= get_percent_color(percent
);
352 return color_snprintf(bf
, size
, color
, fmt
, len
, percent
);