Emit dedicated bright terminal color codes if supported
[babeltrace.git] / src / plugins / text / pretty / print.c
index b4800f3c5993fa228e2453ecaf5ab2fd43296da7..92258030bc79f5dae24009eba88c3f6d3c8e3ac5 100644 (file)
 #include "common/assert.h"
 #include <inttypes.h>
 #include <ctype.h>
+#include <stdbool.h>
 #include <string.h>
 #include "pretty.h"
 
 #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. */
@@ -59,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);
        }
@@ -72,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);
        }
@@ -239,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);
@@ -247,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)
@@ -440,7 +441,7 @@ int print_event_header(struct pretty_component *pretty,
                        &log_level);
                if (prop_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
                        log_level_str = log_level_names[log_level];
-                       BT_ASSERT(log_level_str);
+                       BT_ASSERT_DBG(log_level_str);
 
                        if (!pretty->start_line) {
                                bt_common_g_string_append(pretty->string, ", ");
@@ -489,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) {
@@ -501,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, ": ");
@@ -528,7 +529,7 @@ int print_integer(struct pretty_component *pretty,
        bt_field_class_type ft_type;
 
        int_fc = bt_field_borrow_class_const(field);
-       BT_ASSERT(int_fc);
+       BT_ASSERT_DBG(int_fc);
        ft_type = bt_field_get_class_type(field);
        if (bt_field_class_type_is(ft_type,
                        BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
@@ -538,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;
        }
 
@@ -569,7 +570,7 @@ int print_integer(struct pretty_component *pretty,
                        if (len < 64) {
                                size_t rounded_len;
 
-                               BT_ASSERT(len != 0);
+                               BT_ASSERT_DBG(len != 0);
                                /* Round length to the nearest 3-bit */
                                rounded_len = (((len - 1) / 3) + 1) * 3;
                                v.u &= ((uint64_t) 1 << rounded_len) - 1;
@@ -608,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;
 }
@@ -707,7 +708,7 @@ int print_enum(struct pretty_component *pretty,
                        &label_array, &label_count);
                break;
        default:
-               abort();
+               bt_common_abort();
        }
 
        if (ret) {
@@ -718,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;
        }
@@ -733,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:
@@ -836,7 +837,7 @@ int print_array_field(struct pretty_component *pretty,
        }
 
        field = bt_field_array_borrow_element_field_by_index_const(array, i);
-       BT_ASSERT(field);
+       BT_ASSERT_DBG(field);
        return print_field(pretty, field, print_names);
 }
 
@@ -886,7 +887,7 @@ int print_sequence_field(struct pretty_component *pretty,
        }
 
        field = bt_field_array_borrow_element_field_by_index_const(seq, i);
-       BT_ASSERT(field);
+       BT_ASSERT_DBG(field);
        return print_field(pretty, field, print_names);
 }
 
@@ -952,7 +953,7 @@ int print_variant(struct pretty_component *pretty,
        const bt_field *field = NULL;
 
        field = bt_field_variant_borrow_selected_option_field_const(variant);
-       BT_ASSERT(field);
+       BT_ASSERT_DBG(field);
        bt_common_g_string_append(pretty->string, "{ ");
        pretty->depth++;
        if (print_names) {
@@ -983,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";
@@ -992,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) {
@@ -1000,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,
@@ -1025,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) {
@@ -1041,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) {
@@ -1198,7 +1199,7 @@ int pretty_print_event(struct pretty_component *pretty,
        const bt_event *event =
                bt_message_event_borrow_event_const(event_msg);
 
-       BT_ASSERT(event);
+       BT_ASSERT_DBG(event);
        pretty->start_line = true;
        g_string_assign(pretty->string, "");
        ret = print_event_header(pretty, event_msg);
@@ -1382,7 +1383,7 @@ int pretty_print_discarded_items(struct pretty_component *pretty,
                elem_type = "packet";
                break;
        default:
-               abort();
+               bt_common_abort();
        }
 
        BT_ASSERT(stream);
@@ -1410,10 +1411,27 @@ int pretty_print_discarded_items(struct pretty_component *pretty,
 
                break;
        default:
-               abort();
+               bt_common_abort();
        }
 
        print_discarded_elements_msg(pretty, stream, begin, end,
                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.028358 seconds and 4 git commands to generate.