1 #include <linux/kernel.h>
5 int perf_use_color_default
= -1;
7 static int parse_color(const char *name
, int len
)
9 static const char * const color_names
[] = {
10 "normal", "black", "red", "green", "yellow",
11 "blue", "magenta", "cyan", "white"
16 for (i
= 0; i
< (int)ARRAY_SIZE(color_names
); i
++) {
17 const char *str
= color_names
[i
];
18 if (!strncasecmp(name
, str
, len
) && !str
[len
])
21 i
= strtol(name
, &end
, 10);
22 if (end
- name
== len
&& i
>= -1 && i
<= 255)
27 static int parse_attr(const char *name
, int len
)
29 static const int attr_values
[] = { 1, 2, 4, 5, 7 };
30 static const char * const attr_names
[] = {
31 "bold", "dim", "ul", "blink", "reverse"
35 for (i
= 0; i
< ARRAY_SIZE(attr_names
); i
++) {
36 const char *str
= attr_names
[i
];
37 if (!strncasecmp(name
, str
, len
) && !str
[len
])
38 return attr_values
[i
];
43 void color_parse(const char *value
, const char *var
, char *dst
)
45 color_parse_mem(value
, strlen(value
), var
, dst
);
48 void color_parse_mem(const char *value
, int value_len
, const char *var
,
51 const char *ptr
= value
;
57 if (!strncasecmp(value
, "reset", len
)) {
58 strcpy(dst
, PERF_COLOR_RESET
);
62 /* [fg [bg]] [attr] */
64 const char *word
= ptr
;
67 while (len
> 0 && !isspace(word
[wordlen
])) {
73 while (len
> 0 && isspace(*ptr
)) {
78 val
= parse_color(word
, wordlen
);
90 val
= parse_attr(word
, wordlen
);
91 if (val
< 0 || attr
!= -1)
96 if (attr
>= 0 || fg
>= 0 || bg
>= 0) {
112 dst
+= sprintf(dst
, "38;5;%d", fg
);
122 dst
+= sprintf(dst
, "48;5;%d", bg
);
130 die("bad color value '%.*s' for variable '%s'", value_len
, value
, var
);
133 int perf_config_colorbool(const char *var
, const char *value
, int stdout_is_tty
)
136 if (!strcasecmp(value
, "never"))
138 if (!strcasecmp(value
, "always"))
140 if (!strcasecmp(value
, "auto"))
144 /* Missing or explicit false to turn off colorization */
145 if (!perf_config_bool(var
, value
))
148 /* any normal truth value defaults to 'auto' */
150 if (stdout_is_tty
< 0)
151 stdout_is_tty
= isatty(1);
152 if (stdout_is_tty
|| (pager_in_use() && pager_use_color
)) {
153 char *term
= getenv("TERM");
154 if (term
&& strcmp(term
, "dumb"))
160 int perf_color_default_config(const char *var
, const char *value
, void *cb
)
162 if (!strcmp(var
, "color.ui")) {
163 perf_use_color_default
= perf_config_colorbool(var
, value
, -1);
167 return perf_default_config(var
, value
, cb
);
170 static int __color_vsnprintf(char *bf
, size_t size
, const char *color
,
171 const char *fmt
, va_list args
, const char *trail
)
178 if (perf_use_color_default
< 0) {
179 if (isatty(1) || pager_in_use())
180 perf_use_color_default
= 1;
182 perf_use_color_default
= 0;
185 if (perf_use_color_default
&& *color
)
186 r
+= scnprintf(bf
, size
, "%s", color
);
187 r
+= vscnprintf(bf
+ r
, size
- r
, fmt
, args
);
188 if (perf_use_color_default
&& *color
)
189 r
+= scnprintf(bf
+ r
, size
- r
, "%s", PERF_COLOR_RESET
);
191 r
+= scnprintf(bf
+ r
, size
- r
, "%s", trail
);
195 static int __color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
196 va_list args
, const char *trail
)
203 if (perf_use_color_default
< 0) {
204 if (isatty(fileno(fp
)) || pager_in_use())
205 perf_use_color_default
= 1;
207 perf_use_color_default
= 0;
210 if (perf_use_color_default
&& *color
)
211 r
+= fprintf(fp
, "%s", color
);
212 r
+= vfprintf(fp
, fmt
, args
);
213 if (perf_use_color_default
&& *color
)
214 r
+= fprintf(fp
, "%s", PERF_COLOR_RESET
);
216 r
+= fprintf(fp
, "%s", trail
);
220 int color_vsnprintf(char *bf
, size_t size
, const char *color
,
221 const char *fmt
, va_list args
)
223 return __color_vsnprintf(bf
, size
, color
, fmt
, args
, NULL
);
226 int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
, va_list args
)
228 return __color_vfprintf(fp
, color
, fmt
, args
, NULL
);
231 int color_snprintf(char *bf
, size_t size
, const char *color
,
232 const char *fmt
, ...)
238 r
= color_vsnprintf(bf
, size
, color
, fmt
, args
);
243 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
249 r
= color_vfprintf(fp
, color
, fmt
, args
);
254 int color_fprintf_ln(FILE *fp
, const char *color
, const char *fmt
, ...)
259 r
= __color_vfprintf(fp
, color
, fmt
, args
, "\n");
265 * This function splits the buffer by newlines and colors the lines individually.
267 * Returns 0 on success.
269 int color_fwrite_lines(FILE *fp
, const char *color
,
270 size_t count
, const char *buf
)
273 return fwrite(buf
, count
, 1, fp
) != 1;
276 char *p
= memchr(buf
, '\n', count
);
278 if (p
!= buf
&& (fputs(color
, fp
) < 0 ||
279 fwrite(buf
, p
? (size_t)(p
- buf
) : count
, 1, fp
) != 1 ||
280 fputs(PERF_COLOR_RESET
, fp
) < 0))
284 if (fputc('\n', fp
) < 0)
286 count
-= p
+ 1 - buf
;
292 const char *get_percent_color(double percent
)
294 const char *color
= PERF_COLOR_NORMAL
;
297 * We color high-overhead entries in red, mid-overhead
298 * entries in green - and keep the low overhead places
301 if (percent
>= MIN_RED
)
302 color
= PERF_COLOR_RED
;
304 if (percent
> MIN_GREEN
)
305 color
= PERF_COLOR_GREEN
;
310 int percent_color_fprintf(FILE *fp
, const char *fmt
, double percent
)
315 color
= get_percent_color(percent
);
316 r
= color_fprintf(fp
, color
, fmt
, percent
);
321 int percent_color_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
328 percent
= va_arg(args
, double);
330 color
= get_percent_color(percent
);
331 return color_snprintf(bf
, size
, color
, fmt
, percent
);