From 046094875e3f03883c14ee084f9582c0c15f3c50 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 4 Nov 2019 13:49:04 -0500 Subject: [PATCH] Emit dedicated bright terminal color codes if supported Some terminals support having a bold normal foreground color which is _not_ bright. kitty is one of them, as well as GNOME Terminal with the fairly recent "Show bold text in bright colors" option turned off (or when calling the underlying vte_terminal_set_bold_is_bright() function with the appropriate value). An easy test is to execute: $ echo -e "\033[31mTHIS\n\033[1mTHAT\033[0m" and compare the colors of both lines: if they are the same, then the terminal supports non-bright bold foreground colors. For those terminals, the way to have a non-bold bright color is to emit the dedicated SGR codes 90 to 97: $ echo -e "\033[91mHELLO\033[0m" Some terminals can print non-bold bright colors, but cannot print non-bright bold colors. This patch makes the Babeltrace project emit different color codes depending on the connected terminal and the new `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` environment variable's value: kitty or `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` is `0`: Output bright colors using dedicated SGR codes 90 to 97. Otherwise: Output bright colors with bold + SGR codes 30 to 37. This patch effectively makes the Babeltrace modules emit correct bright color codes for kitty. To change as little as possible, I updated all the sites which emit a bold code followed by a foreground color code to emit instead a bold color code followed by a bright foreground color code. The only drawback with the current approach is that, for non-kitty terminals, two bold color codes are emitted for those cases (the real bold and the brightening bold). This means all bold colored text is also bright currently. Eventually we can start decoupling the bold attribute from bright colors if need be, although this means users must have a supporting terminal. Signed-off-by: Philippe Proulx Change-Id: I3e0124942294fbe833d33aa59506c67e6ca85700 Reviewed-on: https://review.lttng.org/c/babeltrace/+/2328 CI-Build: Simon Marchi Tested-by: jenkins Reviewed-by: Simon Marchi Reviewed-by: Francis Deslauriers --- doc/man/common-common-env.txt | 6 ++ src/cli/babeltrace2.c | 20 ++--- src/common/assert.c | 7 +- src/common/common.c | 125 ++++++++++++++++++++++++++++++ src/common/common.h | 71 ++++++++++++----- src/plugins/text/details/colors.h | 120 +++++++++++++++++++++++----- src/plugins/text/details/write.c | 37 +++++---- src/plugins/text/pretty/pretty.c | 1 + src/plugins/text/pretty/pretty.h | 3 + src/plugins/text/pretty/print.c | 81 +++++++++++-------- 10 files changed, 374 insertions(+), 97 deletions(-) diff --git a/doc/man/common-common-env.txt b/doc/man/common-common-env.txt index fe1c2aa0..f8619af9 100644 --- a/doc/man/common-common-env.txt +++ b/doc/man/common-common-env.txt @@ -25,3 +25,9 @@ The available values are: `ALWAYS`:: Always emit terminal color codes. -- + +`BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD`=`0`:: + Set to `0` to emit + https://en.wikipedia.org/wiki/ANSI_escape_code[SGR] + codes 90 to 97 for bright colors instead of + bold (SGR code~1) and standard color codes (SGR codes 30 to 37). diff --git a/src/cli/babeltrace2.c b/src/cli/babeltrace2.c index 29bd95a9..cc793406 100644 --- a/src/cli/babeltrace2.c +++ b/src/cli/babeltrace2.c @@ -266,7 +266,7 @@ void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name, fprintf(fh, "'%s%s%s%s", bt_common_color_bold(), - bt_common_color_fg_cyan(), + bt_common_color_fg_bright_cyan(), component_type_str(type), bt_common_color_fg_default()); @@ -367,31 +367,31 @@ void print_value_rec(FILE *fp, const bt_value *value, size_t indent) case BT_VALUE_TYPE_BOOL: bool_val = bt_value_bool_get(value); fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), - bt_common_color_fg_cyan(), bool_val ? "yes" : "no", + bt_common_color_fg_bright_cyan(), bool_val ? "yes" : "no", bt_common_color_reset()); break; case BT_VALUE_TYPE_UNSIGNED_INTEGER: uint_val = bt_value_integer_unsigned_get(value); fprintf(fp, "%s%s%" PRIu64 "%s\n", bt_common_color_bold(), - bt_common_color_fg_red(), uint_val, + bt_common_color_fg_bright_red(), uint_val, bt_common_color_reset()); break; case BT_VALUE_TYPE_SIGNED_INTEGER: int_val = bt_value_integer_signed_get(value); fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(), - bt_common_color_fg_red(), int_val, + bt_common_color_fg_bright_red(), int_val, bt_common_color_reset()); break; case BT_VALUE_TYPE_REAL: dbl_val = bt_value_real_get(value); fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(), - bt_common_color_fg_red(), dbl_val, + bt_common_color_fg_bright_red(), dbl_val, bt_common_color_reset()); break; case BT_VALUE_TYPE_STRING: str_val = bt_value_string_get(value); fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), - bt_common_color_fg_green(), str_val, + bt_common_color_fg_bright_green(), str_val, bt_common_color_reset()); break; case BT_VALUE_TYPE_ARRAY: @@ -659,7 +659,7 @@ void print_plugin_info(const bt_plugin *plugin) version_avail = bt_plugin_get_version(plugin, &major, &minor, &patch, &extra); printf("%s%s%s%s:\n", bt_common_color_bold(), - bt_common_color_fg_blue(), plugin_name, + bt_common_color_fg_bright_blue(), plugin_name, bt_common_color_reset()); if (path) { printf(" %sPath%s: %s\n", bt_common_color_bold(), @@ -2642,7 +2642,7 @@ void print_error_causes(void) if (!error || bt_error_get_cause_count(error) == 0) { fprintf(stderr, "%s%sUnknown command-line error.%s\n", - bt_common_color_bold(), bt_common_color_fg_red(), + bt_common_color_bold(), bt_common_color_fg_bright_red(), bt_common_color_reset()); goto end; } @@ -2669,7 +2669,7 @@ void print_error_causes(void) /* Print prefix */ fprintf(stderr, prefix_fmt, - bt_common_color_bold(), bt_common_color_fg_red(), + bt_common_color_bold(), bt_common_color_fg_bright_red(), bt_common_color_reset()); /* Print actor name */ @@ -2717,7 +2717,7 @@ void print_error_causes(void) /* Print file name and line number */ fprintf(stderr, "] (%s%s%s%s:%s%" PRIu64 "%s)\n", bt_common_color_bold(), - bt_common_color_fg_magenta(), + bt_common_color_fg_bright_magenta(), bt_error_cause_get_file_name(cause), bt_common_color_reset(), bt_common_color_fg_green(), diff --git a/src/common/assert.c b/src/common/assert.c index 1cc1dbaa..99080f7b 100644 --- a/src/common/assert.c +++ b/src/common/assert.c @@ -30,14 +30,14 @@ void bt_common_assert_failed(const char *file, int line, const char *func, { fprintf(stderr, "%s\n%s%s%s (╯°□°)╯︵ ┻━┻ %s %s%s%s%s:%s%d%s: %s%s()%s: " - "%sAssertion %s`%s`%s%s failed.%s\n", + "%sAssertion %s%s`%s`%s%s failed.%s\n", bt_common_color_reset(), bt_common_color_bold(), bt_common_color_bg_yellow(), - bt_common_color_fg_red(), + bt_common_color_fg_bright_red(), bt_common_color_reset(), bt_common_color_bold(), - bt_common_color_fg_magenta(), + bt_common_color_fg_bright_magenta(), file, bt_common_color_reset(), bt_common_color_fg_green(), @@ -48,6 +48,7 @@ void bt_common_assert_failed(const char *file, int line, const char *func, bt_common_color_reset(), bt_common_color_fg_red(), bt_common_color_bold(), + bt_common_color_fg_bright_red(), assertion, bt_common_color_reset(), bt_common_color_fg_red(), diff --git a/src/common/common.c b/src/common/common.c index 3acad47c..86c70341 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -63,6 +63,13 @@ static const char *bt_common_color_code_fg_blue = ""; static const char *bt_common_color_code_fg_magenta = ""; static const char *bt_common_color_code_fg_cyan = ""; static const char *bt_common_color_code_fg_light_gray = ""; +static const char *bt_common_color_code_fg_bright_red = ""; +static const char *bt_common_color_code_fg_bright_green = ""; +static const char *bt_common_color_code_fg_bright_yellow = ""; +static const char *bt_common_color_code_fg_bright_blue = ""; +static const char *bt_common_color_code_fg_bright_magenta = ""; +static const char *bt_common_color_code_fg_bright_cyan = ""; +static const char *bt_common_color_code_fg_bright_light_gray = ""; static const char *bt_common_color_code_bg_default = ""; static const char *bt_common_color_code_bg_red = ""; static const char *bt_common_color_code_bg_green = ""; @@ -76,6 +83,10 @@ static void __attribute__((constructor)) bt_common_color_ctor(void) { if (bt_common_colors_supported()) { + const char *term_env_var; + const char *bright_means_bold_env_var; + bool bright_means_bold = true; + bt_common_color_code_reset = BT_COMMON_COLOR_RESET; bt_common_color_code_bold = BT_COMMON_COLOR_BOLD; bt_common_color_code_fg_default = BT_COMMON_COLOR_FG_DEFAULT; @@ -86,6 +97,78 @@ void __attribute__((constructor)) bt_common_color_ctor(void) bt_common_color_code_fg_magenta = BT_COMMON_COLOR_FG_MAGENTA; bt_common_color_code_fg_cyan = BT_COMMON_COLOR_FG_CYAN; bt_common_color_code_fg_light_gray = BT_COMMON_COLOR_FG_LIGHT_GRAY; + + /* + * Check whether or not the terminal supports having + * bold foreground colors which do _not_ become bright + * colors, that is, the lines + * + * $ echo -e "\033[31mTHIS\n\033[1mTHAT\033[0m" + * + * have the _same_ color, but `THAT` uses a bold font. + * + * This is the case of the kitty terminal emulator. + * + * It's also possible with GNOME Terminal since 3.27.2 + * and xfce4-terminal since 0.8.7 (and GNOME VTE since + * 0.51.2), but it's user-configurable. Since we don't + * have this configuration value here, assume it's not + * the case to support old versions of GNOME Terminal. + * + * Any user can set the + * `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` environment + * variable to `0` to use the bright foreground color + * codes instead of making the normal foreground color + * codes bold. + * + * Summary: + * + * With kitty or when + * `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` is `0`: + * Output bright colors using dedicated SGR codes + * 90 to 97. + * + * Otherwise: + * Output bright colors with bold + SGR codes 30 to + * 37. + */ + term_env_var = getenv("TERM"); + BT_ASSERT(term_env_var); + + if (strcmp(term_env_var, "xterm-kitty") == 0) { + /* + * The kitty terminal emulator supports + * non-bright bold foreground colors. + */ + bright_means_bold = false; + } + + bright_means_bold_env_var = + getenv("BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD"); + + if (bright_means_bold_env_var) { + bright_means_bold = + !(strcmp(bright_means_bold_env_var, "0") == 0); + } + + if (bright_means_bold) { + bt_common_color_code_fg_bright_red = BT_COMMON_COLOR_FG_BOLD_RED; + bt_common_color_code_fg_bright_green = BT_COMMON_COLOR_FG_BOLD_GREEN; + bt_common_color_code_fg_bright_yellow = BT_COMMON_COLOR_FG_BOLD_YELLOW; + bt_common_color_code_fg_bright_blue = BT_COMMON_COLOR_FG_BOLD_BLUE; + bt_common_color_code_fg_bright_magenta = BT_COMMON_COLOR_FG_BOLD_MAGENTA; + bt_common_color_code_fg_bright_cyan = BT_COMMON_COLOR_FG_BOLD_CYAN; + bt_common_color_code_fg_bright_light_gray = BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY; + } else { + bt_common_color_code_fg_bright_red = BT_COMMON_COLOR_FG_BRIGHT_RED; + bt_common_color_code_fg_bright_green = BT_COMMON_COLOR_FG_BRIGHT_GREEN; + bt_common_color_code_fg_bright_yellow = BT_COMMON_COLOR_FG_BRIGHT_YELLOW; + bt_common_color_code_fg_bright_blue = BT_COMMON_COLOR_FG_BRIGHT_BLUE; + bt_common_color_code_fg_bright_magenta = BT_COMMON_COLOR_FG_BRIGHT_MAGENTA; + bt_common_color_code_fg_bright_cyan = BT_COMMON_COLOR_FG_BRIGHT_CYAN; + bt_common_color_code_fg_bright_light_gray = BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY; + } + bt_common_color_code_bg_default = BT_COMMON_COLOR_BG_DEFAULT; bt_common_color_code_bg_red = BT_COMMON_COLOR_BG_RED; bt_common_color_code_bg_green = BT_COMMON_COLOR_BG_GREEN; @@ -388,6 +471,48 @@ const char *bt_common_color_fg_light_gray(void) return bt_common_color_code_fg_light_gray; } +BT_HIDDEN +const char *bt_common_color_fg_bright_red(void) +{ + return bt_common_color_code_fg_bright_red; +} + +BT_HIDDEN +const char *bt_common_color_fg_bright_green(void) +{ + return bt_common_color_code_fg_bright_green; +} + +BT_HIDDEN +const char *bt_common_color_fg_bright_yellow(void) +{ + return bt_common_color_code_fg_bright_yellow; +} + +BT_HIDDEN +const char *bt_common_color_fg_bright_blue(void) +{ + return bt_common_color_code_fg_bright_blue; +} + +BT_HIDDEN +const char *bt_common_color_fg_bright_magenta(void) +{ + return bt_common_color_code_fg_bright_magenta; +} + +BT_HIDDEN +const char *bt_common_color_fg_bright_cyan(void) +{ + return bt_common_color_code_fg_bright_cyan; +} + +BT_HIDDEN +const char *bt_common_color_fg_bright_light_gray(void) +{ + return bt_common_color_code_fg_bright_light_gray; +} + BT_HIDDEN const char *bt_common_color_bg_default(void) { diff --git a/src/common/common.h b/src/common/common.h index 3b15b284..96c09b6c 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -44,24 +44,38 @@ #include "common/macros.h" #include "common/safe.h" -#define BT_COMMON_COLOR_RESET "\033[0m" -#define BT_COMMON_COLOR_BOLD "\033[1m" -#define BT_COMMON_COLOR_FG_DEFAULT "\033[39m" -#define BT_COMMON_COLOR_FG_RED "\033[31m" -#define BT_COMMON_COLOR_FG_GREEN "\033[32m" -#define BT_COMMON_COLOR_FG_YELLOW "\033[33m" -#define BT_COMMON_COLOR_FG_BLUE "\033[34m" -#define BT_COMMON_COLOR_FG_MAGENTA "\033[35m" -#define BT_COMMON_COLOR_FG_CYAN "\033[36m" -#define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m" -#define BT_COMMON_COLOR_BG_DEFAULT "\033[49m" -#define BT_COMMON_COLOR_BG_RED "\033[41m" -#define BT_COMMON_COLOR_BG_GREEN "\033[42m" -#define BT_COMMON_COLOR_BG_YELLOW "\033[43m" -#define BT_COMMON_COLOR_BG_BLUE "\033[44m" -#define BT_COMMON_COLOR_BG_MAGENTA "\033[45m" -#define BT_COMMON_COLOR_BG_CYAN "\033[46m" -#define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m" +#define BT_COMMON_COLOR_RESET "\033[0m" +#define BT_COMMON_COLOR_BOLD "\033[1m" +#define BT_COMMON_COLOR_FG_DEFAULT "\033[39m" +#define BT_COMMON_COLOR_FG_RED "\033[31m" +#define BT_COMMON_COLOR_FG_GREEN "\033[32m" +#define BT_COMMON_COLOR_FG_YELLOW "\033[33m" +#define BT_COMMON_COLOR_FG_BLUE "\033[34m" +#define BT_COMMON_COLOR_FG_MAGENTA "\033[35m" +#define BT_COMMON_COLOR_FG_CYAN "\033[36m" +#define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m" +#define BT_COMMON_COLOR_FG_BOLD_RED "\033[1m\033[31m" +#define BT_COMMON_COLOR_FG_BOLD_GREEN "\033[1m\033[32m" +#define BT_COMMON_COLOR_FG_BOLD_YELLOW "\033[1m\033[33m" +#define BT_COMMON_COLOR_FG_BOLD_BLUE "\033[1m\033[34m" +#define BT_COMMON_COLOR_FG_BOLD_MAGENTA "\033[1m\033[35m" +#define BT_COMMON_COLOR_FG_BOLD_CYAN "\033[1m\033[36m" +#define BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY "\033[1m\033[37m" +#define BT_COMMON_COLOR_FG_BRIGHT_RED "\033[91m" +#define BT_COMMON_COLOR_FG_BRIGHT_GREEN "\033[92m" +#define BT_COMMON_COLOR_FG_BRIGHT_YELLOW "\033[93m" +#define BT_COMMON_COLOR_FG_BRIGHT_BLUE "\033[94m" +#define BT_COMMON_COLOR_FG_BRIGHT_MAGENTA "\033[95m" +#define BT_COMMON_COLOR_FG_BRIGHT_CYAN "\033[96m" +#define BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY "\033[97m" +#define BT_COMMON_COLOR_BG_DEFAULT "\033[49m" +#define BT_COMMON_COLOR_BG_RED "\033[41m" +#define BT_COMMON_COLOR_BG_GREEN "\033[42m" +#define BT_COMMON_COLOR_BG_YELLOW "\033[43m" +#define BT_COMMON_COLOR_BG_BLUE "\033[44m" +#define BT_COMMON_COLOR_BG_MAGENTA "\033[45m" +#define BT_COMMON_COLOR_BG_CYAN "\033[46m" +#define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m" struct bt_common_lttng_live_url_parts { GString *proto; @@ -140,6 +154,27 @@ const char *bt_common_color_fg_cyan(void); BT_HIDDEN const char *bt_common_color_fg_light_gray(void); +BT_HIDDEN +const char *bt_common_color_fg_bright_red(void); + +BT_HIDDEN +const char *bt_common_color_fg_bright_green(void); + +BT_HIDDEN +const char *bt_common_color_fg_bright_yellow(void); + +BT_HIDDEN +const char *bt_common_color_fg_bright_blue(void); + +BT_HIDDEN +const char *bt_common_color_fg_bright_magenta(void); + +BT_HIDDEN +const char *bt_common_color_fg_bright_cyan(void); + +BT_HIDDEN +const char *bt_common_color_fg_bright_light_gray(void); + BT_HIDDEN const char *bt_common_color_bg_default(void); diff --git a/src/plugins/text/details/colors.h b/src/plugins/text/details/colors.h index 5b142f55..30470805 100644 --- a/src/plugins/text/details/colors.h +++ b/src/plugins/text/details/colors.h @@ -33,7 +33,7 @@ const char *color_reset(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_RESET; + code = bt_common_color_reset(); } return code; @@ -45,7 +45,7 @@ const char *color_bold(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_BOLD; + code = bt_common_color_bold(); } return code; @@ -57,7 +57,7 @@ const char *color_fg_default(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_FG_DEFAULT; + code = bt_common_color_fg_default(); } return code; @@ -69,7 +69,7 @@ const char *color_fg_red(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_FG_RED; + code = bt_common_color_fg_red(); } return code; @@ -81,7 +81,7 @@ const char *color_fg_green(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_FG_GREEN; + code = bt_common_color_fg_green(); } return code; @@ -93,7 +93,7 @@ const char *color_fg_yellow(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_FG_YELLOW; + code = bt_common_color_fg_yellow(); } return code; @@ -105,7 +105,7 @@ const char *color_fg_blue(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_FG_BLUE; + code = bt_common_color_fg_blue(); } return code; @@ -117,7 +117,7 @@ const char *color_fg_magenta(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_FG_MAGENTA; + code = bt_common_color_fg_magenta(); } return code; @@ -129,7 +129,7 @@ const char *color_fg_cyan(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_FG_CYAN; + code = bt_common_color_fg_cyan(); } return code; @@ -141,7 +141,91 @@ const char *color_fg_light_gray(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_FG_LIGHT_GRAY; + code = bt_common_color_fg_light_gray(); + } + + return code; +} + +static inline +const char *color_fg_bright_red(struct details_write_ctx *ctx) +{ + const char *code = ""; + + if (ctx->details_comp->cfg.with_color) { + code = bt_common_color_fg_bright_red(); + } + + return code; +} + +static inline +const char *color_fg_bright_green(struct details_write_ctx *ctx) +{ + const char *code = ""; + + if (ctx->details_comp->cfg.with_color) { + code = bt_common_color_fg_bright_green(); + } + + return code; +} + +static inline +const char *color_fg_bright_yellow(struct details_write_ctx *ctx) +{ + const char *code = ""; + + if (ctx->details_comp->cfg.with_color) { + code = bt_common_color_fg_bright_yellow(); + } + + return code; +} + +static inline +const char *color_fg_bright_blue(struct details_write_ctx *ctx) +{ + const char *code = ""; + + if (ctx->details_comp->cfg.with_color) { + code = bt_common_color_fg_bright_blue(); + } + + return code; +} + +static inline +const char *color_fg_bright_magenta(struct details_write_ctx *ctx) +{ + const char *code = ""; + + if (ctx->details_comp->cfg.with_color) { + code = bt_common_color_fg_bright_magenta(); + } + + return code; +} + +static inline +const char *color_fg_bright_cyan(struct details_write_ctx *ctx) +{ + const char *code = ""; + + if (ctx->details_comp->cfg.with_color) { + code = bt_common_color_fg_bright_cyan(); + } + + return code; +} + +static inline +const char *color_fg_bright_light_gray(struct details_write_ctx *ctx) +{ + const char *code = ""; + + if (ctx->details_comp->cfg.with_color) { + code = bt_common_color_fg_bright_light_gray(); } return code; @@ -153,7 +237,7 @@ const char *color_bg_default(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_BG_DEFAULT; + code = bt_common_color_bg_default(); } return code; @@ -165,7 +249,7 @@ const char *color_bg_red(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_BG_RED; + code = bt_common_color_bg_red(); } return code; @@ -177,7 +261,7 @@ const char *color_bg_green(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_BG_GREEN; + code = bt_common_color_bg_green(); } return code; @@ -189,7 +273,7 @@ const char *color_bg_yellow(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_BG_YELLOW; + code = bt_common_color_bg_yellow(); } return code; @@ -201,7 +285,7 @@ const char *color_bg_blue(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_BG_BLUE; + code = bt_common_color_bg_blue(); } return code; @@ -213,7 +297,7 @@ const char *color_bg_magenta(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_BG_MAGENTA; + code = bt_common_color_bg_magenta(); } return code; @@ -225,7 +309,7 @@ const char *color_bg_cyan(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_BG_CYAN; + code = bt_common_color_bg_cyan(); } return code; @@ -237,7 +321,7 @@ const char *color_bg_light_gray(struct details_write_ctx *ctx) const char *code = ""; if (ctx->details_comp->cfg.with_color) { - code = BT_COMMON_COLOR_BG_LIGHT_GRAY; + code = bt_common_color_bg_light_gray(); } return code; diff --git a/src/plugins/text/details/write.c b/src/plugins/text/details/write.c index faf59f36..f5562689 100644 --- a/src/plugins/text/details/write.c +++ b/src/plugins/text/details/write.c @@ -215,7 +215,8 @@ static inline void write_obj_type_name(struct details_write_ctx *ctx, const char *name) { g_string_append_printf(ctx->str, "%s%s%s%s", - color_fg_yellow(ctx), color_bold(ctx), name, color_reset(ctx)); + color_bold(ctx), color_fg_bright_yellow(ctx), name, + color_reset(ctx)); } static inline @@ -244,7 +245,7 @@ static inline void write_none_prop_value(struct details_write_ctx *ctx, const char *value) { g_string_append_printf(ctx->str, "%s%s%s%s", - color_bold(ctx), color_fg_magenta(ctx), + color_bold(ctx), color_fg_bright_magenta(ctx), value, color_reset(ctx)); } @@ -327,10 +328,10 @@ void write_bool_prop_value(struct details_write_ctx *ctx, bt_bool prop_value) g_string_append(ctx->str, color_bold(ctx)); if (prop_value) { - g_string_append(ctx->str, color_fg_green(ctx)); + g_string_append(ctx->str, color_fg_bright_green(ctx)); str = "Yes"; } else { - g_string_append(ctx->str, color_fg_red(ctx)); + g_string_append(ctx->str, color_fg_bright_red(ctx)); str = "No"; } @@ -1690,7 +1691,8 @@ void write_time_str(struct details_write_ctx *ctx, const char *str) } g_string_append_printf(ctx->str, "[%s%s%s%s]", - color_bold(ctx), color_fg_blue(ctx), str, color_reset(ctx)); + color_bold(ctx), color_fg_bright_blue(ctx), str, + color_reset(ctx)); if (ctx->details_comp->cfg.compact) { write_sp(ctx); @@ -1715,7 +1717,7 @@ void write_time(struct details_write_ctx *ctx, const bt_clock_snapshot *cs) format_uint(buf, bt_clock_snapshot_get_value(cs), 10); g_string_append_printf(ctx->str, "[%s%s%s%s%s", - color_bold(ctx), color_fg_blue(ctx), buf, + color_bold(ctx), color_fg_bright_blue(ctx), buf, color_reset(ctx), ctx->details_comp->cfg.compact ? "" : " cycles"); cs_status = bt_clock_snapshot_get_ns_from_origin(cs, &ns_from_origin); @@ -1723,7 +1725,7 @@ void write_time(struct details_write_ctx *ctx, const bt_clock_snapshot *cs) format_int(buf, ns_from_origin, 10); g_string_append_printf(ctx->str, "%s %s%s%s%s%s", ctx->details_comp->cfg.compact ? "" : ",", - color_bold(ctx), color_fg_blue(ctx), buf, + color_bold(ctx), color_fg_bright_blue(ctx), buf, color_reset(ctx), ctx->details_comp->cfg.compact ? "" : " ns from origin"); } @@ -1756,22 +1758,25 @@ int write_message_follow_tag(struct details_write_ctx *ctx, if (ctx->details_comp->cfg.compact) { g_string_append_printf(ctx->str, - "%s{%s%" PRIu64 " %" PRIu64 " %" PRIu64 "%s%s}%s ", + "%s{%s%s%" PRIu64 " %" PRIu64 " %" PRIu64 "%s%s}%s ", color_fg_cyan(ctx), color_bold(ctx), + color_fg_bright_cyan(ctx), unique_trace_id, bt_stream_class_get_id(sc), bt_stream_get_id(stream), color_reset(ctx), color_fg_cyan(ctx), color_reset(ctx)); } else { g_string_append_printf(ctx->str, - "%s{Trace %s%" PRIu64 "%s%s, Stream class ID %s%" PRIu64 "%s%s, Stream ID %s%" PRIu64 "%s%s}%s\n", + "%s{Trace %s%s%" PRIu64 "%s%s, Stream class ID %s%s%" PRIu64 "%s%s, Stream ID %s%s%" PRIu64 "%s%s}%s\n", + color_fg_cyan(ctx), + color_bold(ctx), color_fg_bright_cyan(ctx), + unique_trace_id, color_reset(ctx), + color_fg_cyan(ctx), + color_bold(ctx), color_fg_bright_cyan(ctx), + bt_stream_class_get_id(sc), color_reset(ctx), color_fg_cyan(ctx), - color_bold(ctx), unique_trace_id, - color_reset(ctx), color_fg_cyan(ctx), - color_bold(ctx), bt_stream_class_get_id(sc), - color_reset(ctx), color_fg_cyan(ctx), - color_bold(ctx), bt_stream_get_id(stream), - color_reset(ctx), color_fg_cyan(ctx), - color_reset(ctx)); + color_bold(ctx), color_fg_bright_cyan(ctx), + bt_stream_get_id(stream), color_reset(ctx), + color_fg_cyan(ctx), color_reset(ctx)); } end: diff --git a/src/plugins/text/pretty/pretty.c b/src/plugins/text/pretty/pretty.c index 8cb3b472..6cc8607e 100644 --- a/src/plugins/text/pretty/pretty.c +++ b/src/plugins/text/pretty/pretty.c @@ -519,6 +519,7 @@ bt_component_class_initialize_method_status apply_params( apply_one_bool_if_specified("field-callsite", params, &pretty->options.print_callsite_field); + pretty_print_init(); status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; end: diff --git a/src/plugins/text/pretty/pretty.h b/src/plugins/text/pretty/pretty.h index b9d92ce0..eb099efe 100644 --- a/src/plugins/text/pretty/pretty.h +++ b/src/plugins/text/pretty/pretty.h @@ -117,4 +117,7 @@ BT_HIDDEN int pretty_print_discarded_items(struct pretty_component *pretty, const bt_message *msg); +BT_HIDDEN +void pretty_print_init(void); + #endif /* BABELTRACE_PLUGIN_TEXT_PRETTY_PRETTY_H */ diff --git a/src/plugins/text/pretty/print.c b/src/plugins/text/pretty/print.c index c2f202ae..92258030 100644 --- a/src/plugins/text/pretty/print.c +++ b/src/plugins/text/pretty/print.c @@ -37,15 +37,15 @@ #define NSEC_PER_SEC 1000000000LL -#define COLOR_NAME BT_COMMON_COLOR_BOLD -#define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN -#define COLOR_RST BT_COMMON_COLOR_RESET -#define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD -#define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD -#define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD -#define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED -#define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA -#define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW +static char color_name[32]; +static char color_field_name[32]; +static char color_rst[32]; +static char color_string_value[32]; +static char color_number_value[32]; +static char color_enum_mapping_name[32]; +static char color_unknown[32]; +static char color_event_name[32]; +static char color_timestamp[32]; struct timestamp { int64_t real_timestamp; /* Relative to UNIX epoch. */ @@ -60,9 +60,9 @@ static void print_name_equal(struct pretty_component *pretty, const char *name) { if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_NAME); + bt_common_g_string_append(pretty->string, color_name); bt_common_g_string_append(pretty->string, name); - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } else { bt_common_g_string_append(pretty->string, name); } @@ -73,9 +73,9 @@ static void print_field_name_equal(struct pretty_component *pretty, const char *name) { if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_FIELD_NAME); + bt_common_g_string_append(pretty->string, color_field_name); bt_common_g_string_append(pretty->string, name); - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } else { bt_common_g_string_append(pretty->string, name); } @@ -240,7 +240,7 @@ int print_event_timestamp(struct pretty_component *pretty, bt_common_g_string_append(pretty->string, "["); } if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_TIMESTAMP); + bt_common_g_string_append(pretty->string, color_timestamp); } if (pretty->options.print_timestamp_cycles) { print_timestamp_cycles(pretty, clock_snapshot, true); @@ -248,7 +248,7 @@ int print_event_timestamp(struct pretty_component *pretty, print_timestamp_wall(pretty, clock_snapshot, true); } if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } if (!print_names) @@ -490,10 +490,10 @@ int print_event_header(struct pretty_component *pretty, if (pretty->use_colors) { if (ev_name) { bt_common_g_string_append(pretty->string, - COLOR_EVENT_NAME); + color_event_name); } else { bt_common_g_string_append(pretty->string, - COLOR_UNKNOWN); + color_unknown); } } if (ev_name) { @@ -502,7 +502,7 @@ int print_event_header(struct pretty_component *pretty, bt_common_g_string_append(pretty->string, ""); } if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } if (!print_names) { bt_common_g_string_append(pretty->string, ": "); @@ -539,7 +539,7 @@ int print_integer(struct pretty_component *pretty, } if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_NUMBER_VALUE); + bt_common_g_string_append(pretty->string, color_number_value); rst_color = true; } @@ -609,7 +609,7 @@ int print_integer(struct pretty_component *pretty, } end: if (rst_color) { - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } return ret; } @@ -719,11 +719,11 @@ int print_enum(struct pretty_component *pretty, bt_common_g_string_append(pretty->string, "( "); if (label_count == 0) { if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_UNKNOWN); + bt_common_g_string_append(pretty->string, color_unknown); } bt_common_g_string_append(pretty->string, ""); if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } goto skip_loop; } @@ -734,11 +734,11 @@ int print_enum(struct pretty_component *pretty, bt_common_g_string_append(pretty->string, ", "); } if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_ENUM_MAPPING_NAME); + bt_common_g_string_append(pretty->string, color_enum_mapping_name); } print_escape_string(pretty, mapping_name); if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } } skip_loop: @@ -984,7 +984,7 @@ int print_field(struct pretty_component *pretty, v = bt_field_bool_get_value(field); if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_NUMBER_VALUE); + bt_common_g_string_append(pretty->string, color_number_value); } if (v) { text = "true"; @@ -993,7 +993,7 @@ int print_field(struct pretty_component *pretty, } bt_common_g_string_append(pretty->string, text); if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } return 0; } else if (class_id == BT_FIELD_CLASS_TYPE_BIT_ARRAY) { @@ -1001,12 +1001,12 @@ int print_field(struct pretty_component *pretty, if (pretty->use_colors) { bt_common_g_string_append(pretty->string, - COLOR_NUMBER_VALUE); + color_number_value); } bt_common_g_string_append_printf(pretty->string, "0x%" PRIX64, v); if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } return 0; } else if (bt_field_class_type_is(class_id, @@ -1026,11 +1026,11 @@ int print_field(struct pretty_component *pretty, } if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_NUMBER_VALUE); + bt_common_g_string_append(pretty->string, color_number_value); } bt_common_g_string_append_printf(pretty->string, "%g", v); if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } return 0; } else if (class_id == BT_FIELD_CLASS_TYPE_STRING) { @@ -1042,11 +1042,11 @@ int print_field(struct pretty_component *pretty, } if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_STRING_VALUE); + bt_common_g_string_append(pretty->string, color_string_value); } print_escape_string(pretty, str); if (pretty->use_colors) { - bt_common_g_string_append(pretty->string, COLOR_RST); + bt_common_g_string_append(pretty->string, color_rst); } return 0; } else if (class_id == BT_FIELD_CLASS_TYPE_STRUCTURE) { @@ -1418,3 +1418,20 @@ int pretty_print_discarded_items(struct pretty_component *pretty, count, elem_type); return 0; } + +BT_HIDDEN +void pretty_print_init(void) +{ + strcpy(color_name, bt_common_color_bold()); + strcpy(color_field_name, bt_common_color_fg_cyan()); + strcpy(color_rst, bt_common_color_reset()); + strcpy(color_string_value, bt_common_color_bold()); + strcpy(color_number_value, bt_common_color_bold()); + strcpy(color_enum_mapping_name, bt_common_color_bold()); + strcpy(color_unknown, bt_common_color_bold()); + strcat(color_unknown, bt_common_color_fg_bright_red()); + strcpy(color_event_name, bt_common_color_bold()); + strcat(color_event_name, bt_common_color_fg_bright_magenta()); + strcpy(color_timestamp, bt_common_color_bold()); + strcat(color_timestamp, bt_common_color_fg_bright_yellow()); +} -- 2.34.1