Emit dedicated bright terminal color codes if supported
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 4 Nov 2019 18:49:04 +0000 (13:49 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 12 Nov 2019 14:33:04 +0000 (09:33 -0500)
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 <eeppeliteloop@gmail.com>
Change-Id: I3e0124942294fbe833d33aa59506c67e6ca85700
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2328
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
doc/man/common-common-env.txt
src/cli/babeltrace2.c
src/common/assert.c
src/common/common.c
src/common/common.h
src/plugins/text/details/colors.h
src/plugins/text/details/write.c
src/plugins/text/pretty/pretty.c
src/plugins/text/pretty/pretty.h
src/plugins/text/pretty/print.c

index fe1c2aa0ec8e4a1d67a818cfae791f6c54940e93..f8619af94dbd354d8d221f818f7b8c894d2d9800 100644 (file)
@@ -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).
index 29bd95a9bfa67bf832302839aff635513fb3f0c7..cc79340650732bc1a1e9a8c88e367e6cac8e3d8b 100644 (file)
@@ -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(),
index 1cc1dbaa2e5f9a9bfe520ad60afd569885672da1..99080f7ba208f21f2299563a3019d25e578114f3 100644 (file)
@@ -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(),
index 3acad47cfd88a2671293442f0f3f89894be5a967..86c703411497d96518dd586723844ee54f88a3d4 100644 (file)
@@ -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)
 {
index 3b15b28416039ed09b6cd3795f8c9842763a678f..96c09b6c520413180809732c85d5ae146467ea98 100644 (file)
 #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);
 
index 5b142f557bb6898700e855301546a245189801fd..304708052d31be5068a1b0ad5d9f5214df6f5761 100644 (file)
@@ -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;
index faf59f362d4729a7b02626ca21fd67e96b546981..f5562689035e5e975449e77d579b27e7d4261d1a 100644 (file)
@@ -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:
index 8cb3b472239e8211318a259da717c90fd1c76617..6cc8607e19f79244caa6d93870ff8d1cd59290f1 100644 (file)
@@ -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:
index b9d92ce0a132a4dd42dcf9d21950c17e1ce0dbb9..eb099efeb2c7eb27bc0070feb82391b0d16997d3 100644 (file)
@@ -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 */
index c2f202ae5872b2ffa3d29117eb8bd630c1b3900e..92258030bc79f5dae24009eba88c3f6d3c8e3ac5 100644 (file)
 
 #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, "<unknown>");
        }
        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, "<unknown>");
                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());
+}
This page took 0.040775 seconds and 4 git commands to generate.