color.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <linux/kernel.h>
  2. #include "cache.h"
  3. #include "config.h"
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "color.h"
  7. #include <math.h>
  8. #include <unistd.h>
  9. int perf_use_color_default = -1;
  10. int perf_config_colorbool(const char *var, const char *value, int stdout_is_tty)
  11. {
  12. if (value) {
  13. if (!strcasecmp(value, "never"))
  14. return 0;
  15. if (!strcasecmp(value, "always"))
  16. return 1;
  17. if (!strcasecmp(value, "auto"))
  18. goto auto_color;
  19. }
  20. /* Missing or explicit false to turn off colorization */
  21. if (!perf_config_bool(var, value))
  22. return 0;
  23. /* any normal truth value defaults to 'auto' */
  24. auto_color:
  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"))
  30. return 1;
  31. }
  32. return 0;
  33. }
  34. int perf_color_default_config(const char *var, const char *value,
  35. void *cb __maybe_unused)
  36. {
  37. if (!strcmp(var, "color.ui")) {
  38. perf_use_color_default = perf_config_colorbool(var, value, -1);
  39. return 0;
  40. }
  41. return 0;
  42. }
  43. static int __color_vsnprintf(char *bf, size_t size, const char *color,
  44. const char *fmt, va_list args, const char *trail)
  45. {
  46. int r = 0;
  47. /*
  48. * Auto-detect:
  49. */
  50. if (perf_use_color_default < 0) {
  51. if (isatty(1) || pager_in_use())
  52. perf_use_color_default = 1;
  53. else
  54. perf_use_color_default = 0;
  55. }
  56. if (perf_use_color_default && *color)
  57. r += scnprintf(bf, size, "%s", color);
  58. r += vscnprintf(bf + r, size - r, fmt, args);
  59. if (perf_use_color_default && *color)
  60. r += scnprintf(bf + r, size - r, "%s", PERF_COLOR_RESET);
  61. if (trail)
  62. r += scnprintf(bf + r, size - r, "%s", trail);
  63. return r;
  64. }
  65. /* Colors are not included in return value */
  66. static int __color_vfprintf(FILE *fp, const char *color, const char *fmt,
  67. va_list args)
  68. {
  69. int r = 0;
  70. /*
  71. * Auto-detect:
  72. */
  73. if (perf_use_color_default < 0) {
  74. if (isatty(fileno(fp)) || pager_in_use())
  75. perf_use_color_default = 1;
  76. else
  77. perf_use_color_default = 0;
  78. }
  79. if (perf_use_color_default && *color)
  80. fprintf(fp, "%s", color);
  81. r += vfprintf(fp, fmt, args);
  82. if (perf_use_color_default && *color)
  83. fprintf(fp, "%s", PERF_COLOR_RESET);
  84. return r;
  85. }
  86. int color_vsnprintf(char *bf, size_t size, const char *color,
  87. const char *fmt, va_list args)
  88. {
  89. return __color_vsnprintf(bf, size, color, fmt, args, NULL);
  90. }
  91. int color_vfprintf(FILE *fp, const char *color, const char *fmt, va_list args)
  92. {
  93. return __color_vfprintf(fp, color, fmt, args);
  94. }
  95. int color_snprintf(char *bf, size_t size, const char *color,
  96. const char *fmt, ...)
  97. {
  98. va_list args;
  99. int r;
  100. va_start(args, fmt);
  101. r = color_vsnprintf(bf, size, color, fmt, args);
  102. va_end(args);
  103. return r;
  104. }
  105. int color_fprintf(FILE *fp, const char *color, const char *fmt, ...)
  106. {
  107. va_list args;
  108. int r;
  109. va_start(args, fmt);
  110. r = color_vfprintf(fp, color, fmt, args);
  111. va_end(args);
  112. return r;
  113. }
  114. /*
  115. * This function splits the buffer by newlines and colors the lines individually.
  116. *
  117. * Returns 0 on success.
  118. */
  119. int color_fwrite_lines(FILE *fp, const char *color,
  120. size_t count, const char *buf)
  121. {
  122. if (!*color)
  123. return fwrite(buf, count, 1, fp) != 1;
  124. while (count) {
  125. char *p = memchr(buf, '\n', count);
  126. if (p != buf && (fputs(color, fp) < 0 ||
  127. fwrite(buf, p ? (size_t)(p - buf) : count, 1, fp) != 1 ||
  128. fputs(PERF_COLOR_RESET, fp) < 0))
  129. return -1;
  130. if (!p)
  131. return 0;
  132. if (fputc('\n', fp) < 0)
  133. return -1;
  134. count -= p + 1 - buf;
  135. buf = p + 1;
  136. }
  137. return 0;
  138. }
  139. const char *get_percent_color(double percent)
  140. {
  141. const char *color = PERF_COLOR_NORMAL;
  142. /*
  143. * We color high-overhead entries in red, mid-overhead
  144. * entries in green - and keep the low overhead places
  145. * normal:
  146. */
  147. if (fabs(percent) >= MIN_RED)
  148. color = PERF_COLOR_RED;
  149. else {
  150. if (fabs(percent) > MIN_GREEN)
  151. color = PERF_COLOR_GREEN;
  152. }
  153. return color;
  154. }
  155. int percent_color_fprintf(FILE *fp, const char *fmt, double percent)
  156. {
  157. int r;
  158. const char *color;
  159. color = get_percent_color(percent);
  160. r = color_fprintf(fp, color, fmt, percent);
  161. return r;
  162. }
  163. int value_color_snprintf(char *bf, size_t size, const char *fmt, double value)
  164. {
  165. const char *color = get_percent_color(value);
  166. return color_snprintf(bf, size, color, fmt, value);
  167. }
  168. int percent_color_snprintf(char *bf, size_t size, const char *fmt, ...)
  169. {
  170. va_list args;
  171. double percent;
  172. va_start(args, fmt);
  173. percent = va_arg(args, double);
  174. va_end(args);
  175. return value_color_snprintf(bf, size, fmt, percent);
  176. }
  177. int percent_color_len_snprintf(char *bf, size_t size, const char *fmt, ...)
  178. {
  179. va_list args;
  180. int len;
  181. double percent;
  182. const char *color;
  183. va_start(args, fmt);
  184. len = va_arg(args, int);
  185. percent = va_arg(args, double);
  186. va_end(args);
  187. color = get_percent_color(percent);
  188. return color_snprintf(bf, size, color, fmt, len, percent);
  189. }