Visibility hidden by default
[babeltrace.git] / src / plugins / text / pretty / print.c
index 1fd168c519868b45b416077d16dc0df41eea2d3b..4a2fee57aecc85ecb79e5dce571de9d93a4abce0 100644 (file)
@@ -1,26 +1,8 @@
 /*
+ * SPDX-License-Identifier: MIT
+ *
  * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
  */
 
 #include <babeltrace2/babeltrace.h>
 #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 +42,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 +55,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 +222,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 +230,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 +423,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 +472,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 +484,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,17 +511,17 @@ 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 (ft_type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
-                       ft_type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION) {
+       if (bt_field_class_type_is(ft_type,
+                       BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
                v.u = bt_field_integer_unsigned_get_value(field);
        } else {
                v.s = bt_field_integer_signed_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);
                rst_color = true;
        }
 
@@ -560,8 +543,8 @@ int print_integer(struct pretty_component *pretty,
        }
        case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
        {
-               if (ft_type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
-                               ft_type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION) {
+               if (bt_field_class_type_is(ft_type,
+                               BT_FIELD_CLASS_TYPE_SIGNED_INTEGER)) {
                        int len;
 
                        len = bt_field_class_integer_get_field_value_range(
@@ -569,7 +552,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;
@@ -580,8 +563,8 @@ int print_integer(struct pretty_component *pretty,
                break;
        }
        case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
-               if (ft_type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
-                               ft_type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION) {
+               if (bt_field_class_type_is(ft_type,
+                               BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
                        bt_common_g_string_append_printf(pretty->string, "%" PRIu64, v.u);
                } else {
                        bt_common_g_string_append_printf(pretty->string, "%" PRId64, v.s);
@@ -608,7 +591,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;
 }
@@ -638,7 +621,7 @@ void print_escape_string(struct pretty_component *pretty, const char *str)
                }
 
                /* Standard characters. */
-               if (!iscntrl(str[i])) {
+               if (!iscntrl((unsigned char) str[i])) {
                        bt_common_g_string_append_c(pretty->string, str[i]);
                        continue;
                }
@@ -681,22 +664,309 @@ void print_escape_string(struct pretty_component *pretty, const char *str)
        bt_common_g_string_append_c(pretty->string, '"');
 }
 
+/*
+ * Print the unknown label.
+ */
 static
-int print_enum(struct pretty_component *pretty,
+void print_enum_value_label_unknown(struct pretty_component *pretty)
+{
+       if (pretty->use_colors) {
+               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);
+       }
+}
+
+/*
+ * Print labels for a value. If there are more than one label for the
+ * value, they will be printed in "{ }".
+ */
+static
+void print_enum_value_label_array(struct pretty_component *pretty,
+               uint64_t label_count,
+               bt_field_class_enumeration_mapping_label_array label_array)
+{
+       uint64_t i;
+
+       if (label_count > 1) {
+               bt_common_g_string_append(pretty->string, "{ ");
+       }
+
+       for (i = 0; i < label_count; i++) {
+               const char *mapping_name = label_array[i];
+
+               if (i != 0) {
+                       bt_common_g_string_append(pretty->string, ", ");
+               }
+               if (pretty->use_colors) {
+                       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);
+               }
+       }
+
+       if (label_count > 1) {
+               bt_common_g_string_append(pretty->string, " }");
+       }
+}
+
+/*
+ * Print arrays of labels and counts are ORed bit flags.
+ */
+static
+void print_enum_value_bit_flag_label_arrays(struct pretty_component *pretty)
+{
+       uint64_t i;
+       bool first_label = true;
+
+       /* For each bit with a label count > 0, print the labels. */
+       for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
+               uint64_t label_count = pretty->enum_bit_labels[i]->len;
+
+               if (label_count > 0) {
+                       if (!first_label) {
+                               bt_common_g_string_append(pretty->string, " | ");
+                       }
+                       print_enum_value_label_array(pretty, label_count,
+                               (void *) pretty->enum_bit_labels[i]->pdata);
+                       first_label = false;
+               }
+       }
+}
+
+/*
+ * Get the labels mapping to an unsigned value.
+ *
+ * This function will set the count to the count of mapping labels that match
+ * the value. If count == 0, the caller has nothing to do, if count > 0,
+ * the label_array contains the labels to print and the caller is reponsible
+ * to call g_free on it once the values have been used.
+ */
+static
+void print_enum_unsigned_get_mapping_labels_for_value(const bt_field_class *fc,
+               uint64_t value, GPtrArray *labels)
+{
+       uint64_t mapping_count = bt_field_class_enumeration_get_mapping_count(fc);
+       uint64_t i;
+
+       for (i = 0; i < mapping_count; i++) {
+               uint64_t range_i;
+               const struct bt_field_class_enumeration_unsigned_mapping *mapping =
+                       bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(fc, i);
+               const bt_integer_range_set_unsigned *ranges =
+                       bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(mapping);
+               uint64_t range_count = bt_integer_range_set_get_range_count(
+                               bt_integer_range_set_unsigned_as_range_set_const(ranges));
+
+               for (range_i = 0; range_i < range_count; range_i++) {
+                       const bt_integer_range_unsigned *range =
+                               bt_integer_range_set_unsigned_borrow_range_by_index_const(ranges, range_i);
+                       uint64_t lower = bt_integer_range_unsigned_get_lower(range);
+                       uint64_t upper = bt_integer_range_unsigned_get_upper(range);
+
+                       /*
+                        * Flag is active if this range represents a single value
+                        * (lower == upper) and the lower is the same as the bit
+                        * value to test against.
+                        */
+                       if ((lower == upper) && (lower == value)) {
+                               g_ptr_array_add(labels, (void *) bt_field_class_enumeration_mapping_get_label(
+                                       bt_field_class_enumeration_unsigned_mapping_as_mapping_const(mapping)));
+                               break;
+                       }
+               }
+       }
+}
+
+/*
+ * Splits an unsigned enum value into its bit and for each bit set,
+ * try to find a corresponding label.
+ *
+ * If any bit set does not have a corresponding label, then it prints
+ * an unknown value, otherwise, it prints the labels, separated by '|'
+ */
+static
+void print_enum_unsigned_try_bit_flags(struct pretty_component *pretty,
                const bt_field *field)
 {
-       int ret = 0;
-       const bt_field_class *enumeration_field_class = NULL;
-       bt_field_class_enumeration_mapping_label_array label_array;
-       uint64_t label_count;
        uint64_t i;
+       uint64_t value = bt_field_integer_unsigned_get_value(field);
+       const bt_field_class *fc = bt_field_borrow_class_const(field);
 
-       enumeration_field_class = bt_field_borrow_class_const(field);
-       if (!enumeration_field_class) {
-               ret = -1;
+       /* Value is 0, if there was a label for it, we would know by now. */
+       if (value == 0) {
+               print_enum_value_label_unknown(pretty);
+               goto end;
+       }
+
+       for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
+               uint64_t bit_value = UINT64_C(1) << i;
+
+               if ((value & bit_value) != 0) {
+                       print_enum_unsigned_get_mapping_labels_for_value(
+                               fc, bit_value, pretty->enum_bit_labels[i]);
+
+                       if (pretty->enum_bit_labels[i]->len == 0) {
+                               /*
+                                * This bit has no matching label, so this
+                                * field is not a bit flag field, print
+                                * unknown and return.
+                                */
+                               print_enum_value_label_unknown(pretty);
+                               goto end;
+                       }
+               }
+       }
+
+       print_enum_value_bit_flag_label_arrays(pretty);
+
+end:
+       return;
+}
+
+/*
+ * Get the labels mapping to a signed value
+ *
+ * This function will set the count to the count of mapping labels that match
+ * the value. If count == 0, the caller has nothing to do, if count > 0,
+ * the label_array contains the labels to print and the caller is reponsible
+ * to call g_free on it once the values have been used.
+ */
+static
+void print_enum_signed_get_mapping_labels_for_value(const bt_field_class *fc,
+               int64_t value, GPtrArray *labels)
+{
+       uint64_t mapping_count = bt_field_class_enumeration_get_mapping_count(fc);
+       uint64_t i;
+
+       for (i = 0; i < mapping_count; i++) {
+               uint64_t range_i;
+               const struct bt_field_class_enumeration_signed_mapping *mapping =
+                       bt_field_class_enumeration_signed_borrow_mapping_by_index_const(fc, i);
+               const bt_integer_range_set_signed *ranges =
+                       bt_field_class_enumeration_signed_mapping_borrow_ranges_const(mapping);
+               uint64_t range_count = bt_integer_range_set_get_range_count(
+                               bt_integer_range_set_signed_as_range_set_const(ranges));
+
+               for (range_i = 0; range_i < range_count; range_i++) {
+                       const bt_integer_range_signed *range =
+                               bt_integer_range_set_signed_borrow_range_by_index_const(ranges, range_i);
+                       uint64_t lower = bt_integer_range_signed_get_lower(range);
+                       uint64_t upper = bt_integer_range_signed_get_upper(range);
+
+                       /*
+                        * Flag is active if this range represents a single value
+                        * (lower == upper) and the lower is the same as the bit
+                        * value to test against.
+                        */
+                       if ((lower == upper) && (lower == value)) {
+                               g_ptr_array_add(labels, (void*) bt_field_class_enumeration_mapping_get_label(
+                                       bt_field_class_enumeration_signed_mapping_as_mapping_const(mapping)));
+                               break;
+                       }
+               }
+       }
+}
+
+/*
+ * Splits a signed enum value into its bit and for each bit set,
+ * try to find a corresponding label.
+ *
+ * If any bit set does not have a corresponding label, then it prints
+ * an unknown value, otherwise, it prints the labels, separated by '|'.
+ */
+static
+void print_enum_signed_try_bit_flags(struct pretty_component *pretty,
+               const bt_field *field)
+{
+       uint64_t i;
+       int64_t value = bt_field_integer_signed_get_value(field);
+       const bt_field_class *fc = bt_field_borrow_class_const(field);
+
+       /*
+        * Negative value, not a bit flag enum
+        * For 0, if there was a value, we would know by now.
+        */
+       if (value <= 0) {
+               print_enum_value_label_unknown(pretty);
                goto end;
        }
 
+       for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
+               uint64_t bit_value = 1ULL << i;
+
+               if ((value & bit_value) != 0) {
+                       print_enum_signed_get_mapping_labels_for_value(
+                               fc, bit_value, pretty->enum_bit_labels[i]);
+
+                       if (pretty->enum_bit_labels[i]->len == 0) {
+                               /*
+                                * This bit has no matching label, so this
+                                * field is not a bit flag field, print
+                                * unknown and return.
+                                */
+                               print_enum_value_label_unknown(pretty);
+                               goto end;
+                       }
+               }
+       }
+
+       print_enum_value_bit_flag_label_arrays(pretty);
+
+end:
+       return;
+}
+
+/*
+ * Main function to try to print the value of the enum field as a
+ * bit flag. It calls the appropriate function depending on the
+ * field type's signedness and prints the label for each bit set,
+ * if it's a bit flag field.
+ */
+static
+void print_enum_try_bit_flags(struct pretty_component *pretty,
+               const bt_field *field)
+{
+       const bt_field_class *fc = bt_field_borrow_class_const(field);
+       uint64_t int_range = bt_field_class_integer_get_field_value_range(fc);
+       int i;
+
+       BT_ASSERT(int_range <= ENUMERATION_MAX_BITFLAGS_COUNT);
+
+       /*
+        * Remove all labels from the previous enumeration field.
+        */
+       for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
+               g_ptr_array_set_size(pretty->enum_bit_labels[i], 0);
+       }
+
+       /* Get the mapping labels for the bit value. */
+       switch (bt_field_class_get_type(fc)) {
+       case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
+               print_enum_unsigned_try_bit_flags(pretty, field);
+               break;
+       case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
+               print_enum_signed_try_bit_flags(pretty, field);
+               break;
+       default:
+               bt_common_abort();
+       }
+}
+
+static
+int print_enum(struct pretty_component *pretty,
+               const bt_field *field)
+{
+       int ret = 0;
+       bt_field_class_enumeration_mapping_label_array label_array;
+       uint64_t label_count;
+
        switch (bt_field_get_class_type(field)) {
        case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
                ret = bt_field_enumeration_unsigned_get_mapping_labels(field,
@@ -707,7 +977,7 @@ int print_enum(struct pretty_component *pretty,
                        &label_array, &label_count);
                break;
        default:
-               abort();
+               bt_common_abort();
        }
 
        if (ret) {
@@ -716,31 +986,22 @@ 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, "<unknown>");
-               if (pretty->use_colors) {
-                       bt_common_g_string_append(pretty->string, COLOR_RST);
-               }
-               goto skip_loop;
+       if (label_count != 0) {
+               /* The numerical value matches some labels, print them. */
+               print_enum_value_label_array(pretty, label_count, label_array);
+       } else if (pretty->options.print_enum_flags) {
+               /*
+                * The numerical value of the enumeration does not match any
+                * label but the `print-enum-flags` parameter is TRUE so try to
+                * decompose the value of the enum into bits and print it as a
+                * bit flag enum.
+                */
+               print_enum_try_bit_flags(pretty, field);
+       } else {
+               print_enum_value_label_unknown(pretty);
        }
-       for (i = 0; i < label_count; i++) {
-               const char *mapping_name = label_array[i];
 
-               if (i != 0) {
-                       bt_common_g_string_append(pretty->string, ", ");
-               }
-               if (pretty->use_colors) {
-                       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);
-               }
-       }
-skip_loop:
+       /* Print the actual value of the enum. */
        bt_common_g_string_append(pretty->string, " : container = ");
        ret = print_integer(pretty, field);
        if (ret != 0) {
@@ -796,11 +1057,6 @@ int print_struct(struct pretty_component *pretty,
        uint64_t nr_fields, i, nr_printed_fields;
 
        struct_class = bt_field_borrow_class_const(_struct);
-       if (!struct_class) {
-               ret = -1;
-               goto end;
-       }
-
        nr_fields = bt_field_class_structure_get_member_count(struct_class);
 
        bt_common_g_string_append(pretty->string, "{");
@@ -836,7 +1092,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);
 }
 
@@ -845,15 +1101,9 @@ int print_array(struct pretty_component *pretty,
                const bt_field *array, bool print_names)
 {
        int ret = 0;
-       const bt_field_class *array_class = NULL;
        uint64_t len;
        uint64_t i;
 
-       array_class = bt_field_borrow_class_const(array);
-       if (!array_class) {
-               ret = -1;
-               goto end;
-       }
        len = bt_field_array_get_length(array);
        bt_common_g_string_append(pretty->string, "[");
        pretty->depth++;
@@ -886,7 +1136,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);
 }
 
@@ -915,6 +1165,35 @@ end:
        return ret;
 }
 
+static
+int print_option(struct pretty_component *pretty,
+               const bt_field *option, bool print_names)
+{
+       int ret = 0;
+       const bt_field *field = NULL;
+
+       field = bt_field_option_borrow_field_const(option);
+       if (field) {
+               bt_common_g_string_append(pretty->string, "{ ");
+               pretty->depth++;
+               if (print_names) {
+                       // TODO: find tag's name using field path
+                       // print_field_name_equal(pretty, tag_choice);
+               }
+               ret = print_field(pretty, field, print_names);
+               if (ret != 0) {
+                       goto end;
+               }
+               pretty->depth--;
+               bt_common_g_string_append(pretty->string, " }");
+       } else {
+               bt_common_g_string_append(pretty->string, "<none>");
+       }
+
+end:
+       return ret;
+}
+
 static
 int print_variant(struct pretty_component *pretty,
                const bt_field *variant, bool print_names)
@@ -923,7 +1202,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) {
@@ -948,15 +1227,13 @@ int print_field(struct pretty_component *pretty,
        bt_field_class_type class_id;
 
        class_id = bt_field_get_class_type(field);
-       switch (class_id) {
-       case BT_FIELD_CLASS_TYPE_BOOL:
-       {
+       if (class_id == BT_FIELD_CLASS_TYPE_BOOL) {
                bt_bool v;
                const char *text;
 
                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";
@@ -965,32 +1242,47 @@ 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;
-       }
-       case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
-       case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
+       } else if (class_id == BT_FIELD_CLASS_TYPE_BIT_ARRAY) {
+               uint64_t v = bt_field_bit_array_get_value_as_integer(field);
+
+               if (pretty->use_colors) {
+                       bt_common_g_string_append(pretty->string,
+                               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);
+               }
+               return 0;
+       } else if (bt_field_class_type_is(class_id,
+                       BT_FIELD_CLASS_TYPE_ENUMERATION)) {
+               return print_enum(pretty, field);
+       } else if (bt_field_class_type_is(class_id,
+                       BT_FIELD_CLASS_TYPE_INTEGER)) {
                return print_integer(pretty, field);
-       case BT_FIELD_CLASS_TYPE_REAL:
-       {
+       } else if (bt_field_class_type_is(class_id,
+                       BT_FIELD_CLASS_TYPE_REAL)) {
                double v;
 
-               v = bt_field_real_get_value(field);
+               if (class_id == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
+                       v = bt_field_real_single_precision_get_value(field);
+               } else {
+                       v = bt_field_real_double_precision_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);
                }
                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;
-       }
-       case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
-       case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
-               return print_enum(pretty, field);
-       case BT_FIELD_CLASS_TYPE_STRING:
-       {
+       } else if (class_id == BT_FIELD_CLASS_TYPE_STRING) {
                const char *str;
 
                str = bt_field_string_get_value(field);
@@ -999,25 +1291,27 @@ 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;
-       }
-       case BT_FIELD_CLASS_TYPE_STRUCTURE:
+       } else if (class_id == BT_FIELD_CLASS_TYPE_STRUCTURE) {
                return print_struct(pretty, field, print_names);
-       case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
-       case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
-       case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
+       } else if (bt_field_class_type_is(class_id,
+                       BT_FIELD_CLASS_TYPE_OPTION)) {
+               return print_option(pretty, field, print_names);
+       } else if (bt_field_class_type_is(class_id,
+                       BT_FIELD_CLASS_TYPE_VARIANT)) {
                return print_variant(pretty, field, print_names);
-       case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
+       } else if (class_id == BT_FIELD_CLASS_TYPE_STATIC_ARRAY) {
                return print_array(pretty, field, print_names);
-       case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
+       } else if (bt_field_class_type_is(class_id,
+                       BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)) {
                return print_sequence(pretty, field, print_names);
-       default:
+       } else {
                // TODO: log instead
                fprintf(pretty->err, "[error] Unknown type id: %d\n", (int) class_id);
                return -1;
@@ -1146,7 +1440,6 @@ end:
        return ret;
 }
 
-BT_HIDDEN
 int pretty_print_event(struct pretty_component *pretty,
                const bt_message *event_msg)
 {
@@ -1154,7 +1447,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);
@@ -1305,7 +1598,6 @@ int print_discarded_elements_msg(struct pretty_component *pretty,
        return ret;
 }
 
-BT_HIDDEN
 int pretty_print_discarded_items(struct pretty_component *pretty,
                const bt_message *msg)
 {
@@ -1338,7 +1630,7 @@ int pretty_print_discarded_items(struct pretty_component *pretty,
                elem_type = "packet";
                break;
        default:
-               abort();
+               bt_common_abort();
        }
 
        BT_ASSERT(stream);
@@ -1366,10 +1658,26 @@ 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;
 }
+
+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.048602 seconds and 4 git commands to generate.