1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
10 int perf_use_color_default
= -1;
12 static int __color_vsnprintf(char *bf
, size_t size
, const char *color
,
13 const char *fmt
, va_list args
, const char *trail
)
20 if (perf_use_color_default
< 0) {
21 if (isatty(1) || pager_in_use())
22 perf_use_color_default
= 1;
24 perf_use_color_default
= 0;
27 if (perf_use_color_default
&& *color
)
28 r
+= scnprintf(bf
, size
, "%s", color
);
29 r
+= vscnprintf(bf
+ r
, size
- r
, fmt
, args
);
30 if (perf_use_color_default
&& *color
)
31 r
+= scnprintf(bf
+ r
, size
- r
, "%s", PERF_COLOR_RESET
);
33 r
+= scnprintf(bf
+ r
, size
- r
, "%s", trail
);
37 /* Colors are not included in return value */
38 static int __color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
46 if (perf_use_color_default
< 0) {
47 if (isatty(fileno(fp
)) || pager_in_use())
48 perf_use_color_default
= 1;
50 perf_use_color_default
= 0;
53 if (perf_use_color_default
&& *color
)
54 fprintf(fp
, "%s", color
);
55 r
+= vfprintf(fp
, fmt
, args
);
56 if (perf_use_color_default
&& *color
)
57 fprintf(fp
, "%s", PERF_COLOR_RESET
);
61 int color_vsnprintf(char *bf
, size_t size
, const char *color
,
62 const char *fmt
, va_list args
)
64 return __color_vsnprintf(bf
, size
, color
, fmt
, args
, NULL
);
67 int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
, va_list args
)
69 return __color_vfprintf(fp
, color
, fmt
, args
);
72 int color_snprintf(char *bf
, size_t size
, const char *color
,
79 r
= color_vsnprintf(bf
, size
, color
, fmt
, args
);
84 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
90 r
= color_vfprintf(fp
, color
, fmt
, args
);
96 * This function splits the buffer by newlines and colors the lines individually.
98 * Returns 0 on success.
100 int color_fwrite_lines(FILE *fp
, const char *color
,
101 size_t count
, const char *buf
)
104 return fwrite(buf
, count
, 1, fp
) != 1;
107 char *p
= memchr(buf
, '\n', count
);
109 if (p
!= buf
&& (fputs(color
, fp
) < 0 ||
110 fwrite(buf
, p
? (size_t)(p
- buf
) : count
, 1, fp
) != 1 ||
111 fputs(PERF_COLOR_RESET
, fp
) < 0))
115 if (fputc('\n', fp
) < 0)
117 count
-= p
+ 1 - buf
;
123 const char *get_percent_color(double percent
)
125 const char *color
= PERF_COLOR_NORMAL
;
128 * We color high-overhead entries in red, mid-overhead
129 * entries in green - and keep the low overhead places
132 if (fabs(percent
) >= MIN_RED
)
133 color
= PERF_COLOR_RED
;
135 if (fabs(percent
) > MIN_GREEN
)
136 color
= PERF_COLOR_GREEN
;
141 int percent_color_fprintf(FILE *fp
, const char *fmt
, double percent
)
146 color
= get_percent_color(percent
);
147 r
= color_fprintf(fp
, color
, fmt
, percent
);
152 int value_color_snprintf(char *bf
, size_t size
, const char *fmt
, double value
)
154 const char *color
= get_percent_color(value
);
155 return color_snprintf(bf
, size
, color
, fmt
, value
);
158 int percent_color_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
164 percent
= va_arg(args
, double);
166 return value_color_snprintf(bf
, size
, fmt
, percent
);
169 int percent_color_len_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
177 len
= va_arg(args
, int);
178 percent
= va_arg(args
, double);
181 color
= get_percent_color(percent
);
182 return color_snprintf(bf
, size
, color
, fmt
, len
, percent
);