Text output identical with babeltrace 1
[babeltrace.git] / plugins / text / print.c
index 2b1be1fc09f39414602c1a52fc8d0d414b772a55..e742f2184c19236c8ffce7ab9c582e67fb8b741f 100644 (file)
@@ -4,6 +4,7 @@
  * Babeltrace CTF Text Output Plugin Event Printing
  *
  * 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>
  *
 #include <babeltrace/ctf-ir/packet.h>
 #include <babeltrace/ctf-ir/stream.h>
 #include <babeltrace/ctf-ir/stream-class.h>
-#include <babeltrace/ctf-ir/clock.h>
+#include <babeltrace/ctf-ir/clock-class.h>
 #include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/ctf-ir/fields.h>
+#include <babeltrace/ctf-ir/trace.h>
 #include <babeltrace/bitfield.h>
+#include <babeltrace/common-internal.h>
 #include <inttypes.h>
 #include "text.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 inline
 const char *rem_(const char *str)
 {
@@ -54,45 +69,179 @@ struct timestamp {
 
 static
 enum bt_component_status print_field(struct text_component *text,
-               struct bt_ctf_field *field, bool print_names);
+               struct bt_ctf_field *field, bool print_names,
+               GQuark *filters_fields, int filter_array_len);
 
 static
-void print_timestamp_cycles(struct text_component *text,
-               struct bt_ctf_clock *clock,
-               struct bt_ctf_event *event)
+void print_name_equal(struct text_component *text, const char *name)
 {
-       fputs("00000000000000000000", text->out);
+       if (text->use_colors) {
+               fprintf(text->out, "%s%s%s = ", COLOR_NAME, name, COLOR_RST);
+       } else {
+               fprintf(text->out, "%s = ", name);
+       }
 }
 
 static
-void print_timestamp_wall(struct text_component *text,
-               struct bt_ctf_clock *clock,
+void print_field_name_equal(struct text_component *text, const char *name)
+{
+       if (text->use_colors) {
+               fprintf(text->out, "%s%s%s = ", COLOR_FIELD_NAME, name,
+                       COLOR_RST);
+       } else {
+               fprintf(text->out, "%s = ", name);
+       }
+}
+
+static
+void print_timestamp_cycles(struct text_component *text,
+               struct bt_ctf_clock_class *clock_class,
                struct bt_ctf_event *event)
 {
-       fputs("??:??:??.?????????", text->out);
+       int ret;
+       struct bt_ctf_clock_value *clock_value;
+       uint64_t cycles;
+
+       clock_value = bt_ctf_event_get_clock_value(event, clock_class);
+       if (!clock_value) {
+               fputs("????????????????????", text->out);
+               return;
+       }
+
+       ret = bt_ctf_clock_value_get_value(clock_value, &cycles);
+       bt_put(clock_value);
+       if (ret) {
+               fprintf(text->out, "Error");
+               return;
+       }
+       fprintf(text->out, "%020" PRIu64, cycles);
+
+       if (text->last_cycles_timestamp != -1ULL) {
+               text->delta_cycles = cycles - text->last_cycles_timestamp;
+       }
+       text->last_cycles_timestamp = cycles;
 }
 
 static
-enum bt_component_status get_event_timestamp(struct bt_ctf_event *event)
+void print_timestamp_wall(struct text_component *text,
+               struct bt_ctf_clock_class *clock_class,
+               struct bt_ctf_event *event)
 {
-/*     int ret;
-       uint64_t value, frequency;
-       int64_t offset_s, offset;
-*/
-       return BT_COMPONENT_STATUS_OK;
+       int ret;
+       struct bt_ctf_clock_value *clock_value;
+       int64_t ts_nsec = 0;    /* add configurable offset */
+       int64_t ts_sec = 0;     /* add configurable offset */
+       uint64_t ts_sec_abs, ts_nsec_abs;
+       bool is_negative;
+
+       clock_value = bt_ctf_event_get_clock_value(event, clock_class);
+       if (!clock_value) {
+               fputs("??:??:??.?????????", text->out);
+               return;
+       }
+
+       ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, &ts_nsec);
+       bt_put(clock_value);
+       if (ret) {
+               fprintf(text->out, "Error");
+               return;
+       }
+
+       if (text->last_real_timestamp != -1ULL) {
+               text->delta_real_timestamp = ts_nsec - text->last_real_timestamp;
+       }
+       text->last_real_timestamp = ts_nsec;
+
+       ts_sec += ts_nsec / NSEC_PER_SEC;
+       ts_nsec = ts_nsec % NSEC_PER_SEC;
+       if (ts_sec >= 0 && ts_nsec >= 0) {
+               is_negative = false;
+               ts_sec_abs = ts_sec;
+               ts_nsec_abs = ts_nsec;
+       } else if (ts_sec > 0 && ts_nsec < 0) {
+               is_negative = false;
+               ts_sec_abs = ts_sec - 1;
+               ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
+       } else if (ts_sec == 0 && ts_nsec < 0) {
+               is_negative = true;
+               ts_sec_abs = ts_sec;
+               ts_nsec_abs = -ts_nsec;
+       } else if (ts_sec < 0 && ts_nsec > 0) {
+               is_negative = true;
+               ts_sec_abs = -(ts_sec + 1);
+               ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
+       } else if (ts_sec < 0 && ts_nsec == 0) {
+               is_negative = true;
+               ts_sec_abs = -ts_sec;
+               ts_nsec_abs = ts_nsec;
+       } else {        /* (ts_sec < 0 && ts_nsec < 0) */
+               is_negative = true;
+               ts_sec_abs = -ts_sec;
+               ts_nsec_abs = -ts_nsec;
+       }
+
+       if (!text->options.clock_seconds) {
+               struct tm tm;
+               time_t time_s = (time_t) ts_sec_abs;
+
+               if (is_negative) {
+                       fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
+                       goto seconds;
+               }
+
+               if (!text->options.clock_gmt) {
+                       struct tm *res;
+
+                       res = localtime_r(&time_s, &tm);
+                       if (!res) {
+                               fprintf(stderr, "[warning] Unable to get localtime.\n");
+                               goto seconds;
+                       }
+               } else {
+                       struct tm *res;
+
+                       res = gmtime_r(&time_s, &tm);
+                       if (!res) {
+                               fprintf(stderr, "[warning] Unable to get gmtime.\n");
+                               goto seconds;
+                       }
+               }
+               if (text->options.clock_date) {
+                       char timestr[26];
+                       size_t res;
+
+                       /* Print date and time */
+                       res = strftime(timestr, sizeof(timestr),
+                                       "%F ", &tm);
+                       if (!res) {
+                               fprintf(stderr, "[warning] Unable to print ascii time.\n");
+                               goto seconds;
+                       }
+                       fprintf(text->out, "%s", timestr);
+               }
+               /* Print time in HH:MM:SS.ns */
+               fprintf(text->out, "%02d:%02d:%02d.%09" PRIu64,
+                               tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec_abs);
+               goto end;
+       }
+seconds:
+       fprintf(text->out, "%s%" PRId64 ".%09" PRIu64,
+                       is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
+end:
+       return;
 }
 
 static
 enum bt_component_status print_event_timestamp(struct text_component *text,
-               struct bt_ctf_event *event)
+               struct bt_ctf_event *event, bool *start_line)
 {
        bool print_names = text->options.print_header_field_names;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_stream *stream = NULL;
-       struct bt_ctf_clock *clock = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL;
+       struct bt_ctf_trace *trace = NULL;
+       struct bt_ctf_clock_class *clock_class = NULL;
        FILE *out = text->out;
-       FILE *err = text->err;
-       uint64_t real_timestamp;
 
        stream = bt_ctf_event_get_stream(event);
        if (!stream) {
@@ -100,27 +249,79 @@ enum bt_component_status print_event_timestamp(struct text_component *text,
                goto end;
        }
 
-       clock = bt_ctf_event_get_clock(event);
-       if (!clock) {
-               /* Stream has no timestamp. */
-               //puts("no_timestamp!");
-               //goto end;
+       stream_class = bt_ctf_stream_get_class(stream);
+       if (!stream_class) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       trace = bt_ctf_stream_class_get_trace(stream_class);
+       if (!trace) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       clock_class = bt_ctf_trace_get_clock_class(trace, 0);
+       if (!clock_class) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
        }
 
-       fputs(print_names ? "timestamp = " : "[", out);
+       if (print_names) {
+               print_name_equal(text, "timestamp");
+       } else {
+               fputs("[", out);
+       }
+       if (text->use_colors) {
+               fputs(COLOR_TIMESTAMP, text->out);
+       }
        if (text->options.print_timestamp_cycles) {
-               print_timestamp_cycles(text, clock, event);
+               print_timestamp_cycles(text, clock_class, event);
        } else {
-               print_timestamp_wall(text, clock, event);
+               print_timestamp_wall(text, clock_class, event);
+       }
+       if (text->use_colors) {
+               fputs(COLOR_RST, text->out);
        }
 
-       fputs(print_names ? ", " : "] ", out);
-       if (!text->options.print_delta_field) {
-               goto end;
+       if (!print_names)
+               fputs("] ", out);
+
+       if (text->options.print_delta_field) {
+               if (print_names) {
+                       fputs(", ", text->out);
+                       print_name_equal(text, "delta");
+               } else {
+                       fputs("(", text->out);
+               }
+               if (text->options.print_timestamp_cycles) {
+                       if (text->delta_cycles == -1ULL) {
+                               fputs("+??????????\?\?) ", text->out); /* Not a trigraph. */
+                       } else {
+                               fprintf(text->out, "+%012" PRIu64, text->delta_cycles);
+                       }
+               } else {
+                       if (text->delta_real_timestamp != -1ULL) {
+                               uint64_t delta_sec, delta_nsec, delta;
+
+                               delta = text->delta_real_timestamp;
+                               delta_sec = delta / NSEC_PER_SEC;
+                               delta_nsec = delta % NSEC_PER_SEC;
+                               fprintf(text->out, "+%" PRIu64 ".%09" PRIu64,
+                                       delta_sec, delta_nsec);
+                       } else {
+                               fputs("+?.?????????", text->out);
+                       }
+               }
+               if (!print_names) {
+                       fputs(") ", text->out);
+               }
        }
+       *start_line = !print_names;
+
 end:
        bt_put(stream);
-       bt_put(clock);
+       bt_put(clock_class);
+       bt_put(stream_class);
+       bt_put(trace);
        return ret;
 }
 
@@ -131,22 +332,232 @@ enum bt_component_status print_event_header(struct text_component *text,
        bool print_names = text->options.print_header_field_names;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_event_class *event_class = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL;
+       struct bt_ctf_trace *trace_class = NULL;
+       int dom_print = 0;
 
        event_class = bt_ctf_event_get_class(event);
        if (!event_class) {
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
-       ret = print_event_timestamp(text, event);
+       stream_class = bt_ctf_event_class_get_stream_class(event_class);
+       if (!stream_class) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       trace_class = bt_ctf_stream_class_get_trace(stream_class);
+       if (!trace_class) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       ret = print_event_timestamp(text, event, &text->start_line);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
        }
+       if (text->options.print_trace_field) {
+               const char *name;
+
+               name = bt_ctf_trace_get_name(trace_class);
+               if (name) {
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       if (print_names) {
+                               print_name_equal(text, "trace");
+                       }
+                       fprintf(text->out, "%s", name);
+                       if (!print_names) {
+                               fprintf(text->out, " ");
+                       }
+               }
+       }
+       if (text->options.print_trace_hostname_field) {
+               struct bt_value *hostname_str;
+
+               hostname_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
+                               "hostname");
+               if (hostname_str) {
+                       const char *str;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       if (print_names) {
+                               print_name_equal(text, "trace:hostname");
+                       }
+                       if (bt_value_string_get(hostname_str, &str)
+                                       == BT_VALUE_STATUS_OK) {
+                               fprintf(text->out, "%s", str);
+                       }
+                       bt_put(hostname_str);
+                       dom_print = 1;
+               }
+       }
+       if (text->options.print_trace_domain_field) {
+               struct bt_value *domain_str;
+
+               domain_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
+                               "domain");
+               if (domain_str) {
+                       const char *str;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       if (print_names) {
+                               print_name_equal(text, "trace:domain");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
+                       }
+                       if (bt_value_string_get(domain_str, &str)
+                                       == BT_VALUE_STATUS_OK) {
+                               fprintf(text->out, "%s", str);
+                       }
+                       bt_put(domain_str);
+                       dom_print = 1;
+               }
+       }
+       if (text->options.print_trace_procname_field) {
+               struct bt_value *procname_str;
+
+               procname_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
+                               "procname");
+               if (procname_str) {
+                       const char *str;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       if (print_names) {
+                               print_name_equal(text, "trace:procname");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
+                       }
+                       if (bt_value_string_get(procname_str, &str)
+                                       == BT_VALUE_STATUS_OK) {
+                               fprintf(text->out, "%s", str);
+                       }
+                       bt_put(procname_str);
+                       dom_print = 1;
+               }
+       }
+       if (text->options.print_trace_vpid_field) {
+               struct bt_value *vpid_value;
+
+               vpid_value = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
+                               "vpid");
+               if (vpid_value) {
+                       int64_t value;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       if (print_names) {
+                               print_name_equal(text, "trace:vpid");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
+                       }
+                       if (bt_value_integer_get(vpid_value, &value)
+                                       == BT_VALUE_STATUS_OK) {
+                               fprintf(text->out, "(%" PRId64 ")", value);
+                       }
+                       bt_put(vpid_value);
+                       dom_print = 1;
+               }
+       }
+       if (text->options.print_loglevel_field) {
+               struct bt_value *loglevel_str, *loglevel_value;
+
+               loglevel_str = bt_ctf_event_class_get_attribute_value_by_name(event_class,
+                               "loglevel_string");
+               loglevel_value = bt_ctf_event_class_get_attribute_value_by_name(event_class,
+                               "loglevel");
+               if (loglevel_str || loglevel_value) {
+                       bool has_str = false;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       if (print_names) {
+                               print_name_equal(text, "loglevel");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
+                       }
+                       if (loglevel_str) {
+                               const char *str;
+
+                               if (bt_value_string_get(loglevel_str, &str)
+                                               == BT_VALUE_STATUS_OK) {
+                                       fprintf(text->out, "%s", str);
+                                       has_str = true;
+                               }
+                       }
+                       if (loglevel_value) {
+                               int64_t value;
+
+                               if (bt_value_integer_get(loglevel_value, &value)
+                                               == BT_VALUE_STATUS_OK) {
+                                       fprintf(text->out, "%s(%" PRId64 ")",
+                                               has_str ? " " : "", value);
+                               }
+                       }
+                       bt_put(loglevel_str);
+                       bt_put(loglevel_value);
+                       dom_print = 1;
+               }
+       }
+       if (text->options.print_emf_field) {
+               struct bt_value *uri_str;
+
+               uri_str = bt_ctf_event_class_get_attribute_value_by_name(event_class,
+                               "model.emf.uri");
+               if (uri_str) {
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       if (print_names) {
+                               print_name_equal(text, "model.emf.uri");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
+                       }
+                       if (uri_str) {
+                               const char *str;
+
+                               if (bt_value_string_get(uri_str, &str)
+                                               == BT_VALUE_STATUS_OK) {
+                                       fprintf(text->out, "%s", str);
+                               }
+                       }
+                       bt_put(uri_str);
+                       dom_print = 1;
+               }
+       }
+       if (dom_print && !print_names) {
+               fputs(" ", text->out);
+       }
+       if (!text->start_line) {
+               fputs(", ", text->out);
+       }
+       text->start_line = true;
        if (print_names) {
-               fputs("name = ", text->out);
+               print_name_equal(text, "name");
+       }
+       if (text->use_colors) {
+               fputs(COLOR_EVENT_NAME, text->out);
        }
        fputs(bt_ctf_event_class_get_name(event_class), text->out);
-       bt_put(event_class);
+       if (text->use_colors) {
+               fputs(COLOR_RST, text->out);
+       }
+       if (!print_names) {
+               fputs(": ", text->out);
+       } else {
+               fputs(", ", text->out);
+       }
 end:
+       bt_put(trace_class);
+       bt_put(stream_class);
        bt_put(event_class);
        return ret;
 }
@@ -164,6 +575,7 @@ enum bt_component_status print_integer(struct text_component *text,
                uint64_t u;
                int64_t s;
        } v;
+       bool rst_color = false;
 
        field_type = bt_ctf_field_get_type(field);
        if (!field_type) {
@@ -201,6 +613,11 @@ enum bt_component_status print_integer(struct text_component *text,
                goto end;
        }
 
+       if (text->use_colors) {
+               fputs(COLOR_NUMBER_VALUE, text->out);
+               rst_color = true;
+       }
+
        base = bt_ctf_field_type_integer_get_base(field_type);
        switch (base) {
        case BT_CTF_INTEGER_BASE_BINARY:
@@ -274,6 +691,9 @@ enum bt_component_status print_integer(struct text_component *text,
                goto end;
        }
 end:
+       if (rst_color) {
+               fputs(COLOR_RST, text->out);
+       }
        bt_put(field_type);
        return ret;
 }
@@ -284,18 +704,88 @@ enum bt_component_status print_enum(struct text_component *text,
 {
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_field *container_field = NULL;
-       const char *mapping_name;
-
+       struct bt_ctf_field_type *enumeration_field_type = NULL;
+       struct bt_ctf_field_type *container_field_type = NULL;
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
+       int nr_mappings = 0;
+       int is_signed;
+
+       enumeration_field_type = bt_ctf_field_get_type(field);
+       if (!enumeration_field_type) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
        container_field = bt_ctf_field_enumeration_get_container(field);
        if (!container_field) {
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
-       mapping_name = bt_ctf_field_enumeration_get_mapping_name(field);
-       if (mapping_name) {
-               fprintf(text->out, "( \"%s\"", mapping_name);
+       container_field_type = bt_ctf_field_get_type(container_field);
+       if (!container_field_type) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       is_signed = bt_ctf_field_type_integer_get_signed(container_field_type);
+       if (is_signed < 0) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       if (is_signed) {
+               int64_t value;
+
+               if (bt_ctf_field_signed_integer_get_value(container_field,
+                               &value)) {
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+               iter = bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
+                               enumeration_field_type, value);
        } else {
-               fprintf(text->out, "( <unknown>");
+               uint64_t value;
+
+               if (bt_ctf_field_unsigned_integer_get_value(container_field,
+                               &value)) {
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+               iter = bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
+                               enumeration_field_type, value);
+       }
+       if (!iter) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       fprintf(text->out, "( ");
+       for (;;) {
+               const char *mapping_name;
+
+               if (bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
+                               iter, &mapping_name, NULL, NULL) < 0) {
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+               if (nr_mappings++)
+                       fprintf(text->out, ", ");
+               if (text->use_colors) {
+                       fputs(COLOR_ENUM_MAPPING_NAME, text->out);
+               }
+               // TODO: escape string
+               fprintf(text->out, "\"%s\"", mapping_name);
+               if (text->use_colors) {
+                       fputs(COLOR_RST, text->out);
+               }
+               if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter) < 0) {
+                       break;
+               }
+       }
+       if (!nr_mappings) {
+               if (text->use_colors) {
+                       fputs(COLOR_UNKNOWN, text->out);
+               }
+               fprintf(text->out, "<unknown>");
+               if (text->use_colors) {
+                       fputs(COLOR_RST, text->out);
+               }
        }
        fprintf(text->out, " : container = ");
        ret = print_integer(text, container_field);
@@ -304,15 +794,38 @@ enum bt_component_status print_enum(struct text_component *text,
        }
        fprintf(text->out, " )");
 end:
+       bt_put(iter);
+       bt_put(container_field_type);
        bt_put(container_field);
+       bt_put(enumeration_field_type);
        return ret;
 }
 
+static
+int filter_field_name(struct text_component *text, const char *field_name,
+               GQuark *filter_fields, int filter_array_len)
+{
+       int i;
+       GQuark field_quark = g_quark_try_string(field_name);
+
+       if (!field_quark || text->options.verbose) {
+               return 1;
+       }
+
+       for (i = 0; i < filter_array_len; i++) {
+               if (field_quark == filter_fields[i]) {
+                       return 0;
+               }
+       }
+       return 1;
+}
+
 static
 enum bt_component_status print_struct_field(struct text_component *text,
                struct bt_ctf_field *_struct,
                struct bt_ctf_field_type *struct_type,
-               int i, bool print_names)
+               int i, bool print_names, int *nr_printed_fields,
+               GQuark *filter_fields, int filter_array_len)
 {
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        const char *field_name;
@@ -330,15 +843,22 @@ enum bt_component_status print_struct_field(struct text_component *text,
                goto end;
        }
 
-       if (i != 0) {
+       if (filter_fields && !filter_field_name(text, field_name,
+                               filter_fields, filter_array_len)) {
+               ret = BT_COMPONENT_STATUS_OK;
+               goto end;
+       }
+
+       if (*nr_printed_fields > 0) {
                fprintf(text->out, ", ");
        } else {
                fprintf(text->out, " ");
        }
        if (print_names) {
-               fprintf(text->out, "%s = ", rem_(field_name));
+               print_field_name_equal(text, rem_(field_name));
        }
-       ret = print_field(text, field, print_names);
+       ret = print_field(text, field, print_names, NULL, 0);
+       *nr_printed_fields += 1;
 end:
        bt_put(field_type);
        bt_put(field);
@@ -347,11 +867,12 @@ end:
 
 static
 enum bt_component_status print_struct(struct text_component *text,
-               struct bt_ctf_field *_struct, bool print_names)
+               struct bt_ctf_field *_struct, bool print_names,
+               GQuark *filter_fields, int filter_array_len)
 {
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_field_type *struct_type = NULL;
-       int nr_fields, i;
+       int nr_fields, i, nr_printed_fields;
 
        struct_type = bt_ctf_field_get_type(_struct);
        if (!struct_type) {
@@ -365,9 +886,11 @@ enum bt_component_status print_struct(struct text_component *text,
        }
        fprintf(text->out, "{");
        text->depth++;
+       nr_printed_fields = 0;
        for (i = 0; i < nr_fields; i++) {
                ret = print_struct_field(text, _struct, struct_type, i,
-                               print_names);
+                               print_names, &nr_printed_fields, filter_fields,
+                               filter_array_len);
                if (ret != BT_COMPONENT_STATUS_OK) {
                        goto end;
                }
@@ -393,13 +916,16 @@ enum bt_component_status print_array_field(struct text_component *text,
                } else {
                        fprintf(text->out, " ");
                }
+               if (print_names) {
+                       fprintf(text->out, "[%" PRIu64 "] = ", i);
+               }
        }
        field = bt_ctf_field_array_get_field(array, i);
        if (!field) {
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
-       ret = print_field(text, field, print_names);
+       ret = print_field(text, field, print_names, NULL, 0);
 end:
        bt_put(field);
        return ret;
@@ -471,7 +997,14 @@ enum bt_component_status print_array(struct text_component *text,
        text->depth--;
 
        if (is_string) {
+               if (text->use_colors) {
+                       fputs(COLOR_STRING_VALUE, text->out);
+               }
+               // TODO: escape string
                fprintf(text->out, "\"%s\"", text->string->str);
+               if (text->use_colors) {
+                       fputs(COLOR_RST, text->out);
+               }
        } else {
                fprintf(text->out, " ]");
        }
@@ -495,13 +1028,16 @@ enum bt_component_status print_sequence_field(struct text_component *text,
                } else {
                        fprintf(text->out, " ");
                }
+               if (print_names) {
+                       fprintf(text->out, "[%" PRIu64 "] = ", i);
+               }
        }
        field = bt_ctf_field_sequence_get_field(seq, i);
        if (!field) {
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
-       ret = print_field(text, field, print_names);
+       ret = print_field(text, field, print_names, NULL, 0);
 end:
        bt_put(field);
        return ret;
@@ -581,7 +1117,14 @@ enum bt_component_status print_sequence(struct text_component *text,
        text->depth--;
 
        if (is_string) {
+               if (text->use_colors) {
+                       fputs(COLOR_STRING_VALUE, text->out);
+               }
+               // TODO: escape string
                fprintf(text->out, "\"%s\"", text->string->str);
+               if (text->use_colors) {
+                       fputs(COLOR_RST, text->out);
+               }
        } else {
                fprintf(text->out, " ]");
        }
@@ -607,24 +1150,38 @@ enum bt_component_status print_variant(struct text_component *text,
        fprintf(text->out, "{ ");
        text->depth++;
        if (print_names) {
+               int iter_ret;
                struct bt_ctf_field *tag_field = NULL;
                const char *tag_choice;
+               struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
 
                tag_field = bt_ctf_field_variant_get_tag(variant);
                if (!tag_field) {
                        ret = BT_COMPONENT_STATUS_ERROR;
                        goto end;
                }
-               tag_choice = bt_ctf_field_enumeration_get_mapping_name(tag_field);
-               if (!tag_choice) {
+
+               iter = bt_ctf_field_enumeration_get_mappings(tag_field);
+               if (!iter) {
+                       bt_put(tag_field);
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+
+               iter_ret =
+                       bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
+                               iter, &tag_choice, NULL, NULL);
+               if (iter_ret) {
+                       bt_put(iter);
                        bt_put(tag_field);
                        ret = BT_COMPONENT_STATUS_ERROR;
                        goto end;
                }
-               fprintf(text->out, "%s = ", rem_(tag_choice));
+               print_field_name_equal(text, rem_(tag_choice));
                bt_put(tag_field);
+               bt_put(iter);
        }
-       ret = print_field(text, field, print_names);
+       ret = print_field(text, field, print_names, NULL, 0);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
        }
@@ -637,7 +1194,8 @@ end:
 
 static
 enum bt_component_status print_field(struct text_component *text,
-               struct bt_ctf_field *field, bool print_names)
+               struct bt_ctf_field *field, bool print_names,
+               GQuark *filter_fields, int filter_array_len)
 {
        enum bt_ctf_type_id type_id;
 
@@ -652,16 +1210,31 @@ enum bt_component_status print_field(struct text_component *text,
                if (bt_ctf_field_floating_point_get_value(field, &v)) {
                        return BT_COMPONENT_STATUS_ERROR;
                }
+               if (text->use_colors) {
+                       fputs(COLOR_NUMBER_VALUE, text->out);
+               }
                fprintf(text->out, "%g", v);
+               if (text->use_colors) {
+                       fputs(COLOR_RST, text->out);
+               }
                return BT_COMPONENT_STATUS_OK;
        }
        case CTF_TYPE_ENUM:
                return print_enum(text, field);
        case CTF_TYPE_STRING:
-               fprintf(text->out, "\"%s\"", bt_ctf_field_string_get_value(field));
+               if (text->use_colors) {
+                       fputs(COLOR_STRING_VALUE, text->out);
+               }
+               // TODO: escape the string value
+               fprintf(text->out, "\"%s\"",
+                       bt_ctf_field_string_get_value(field));
+               if (text->use_colors) {
+                       fputs(COLOR_RST, text->out);
+               }
                return BT_COMPONENT_STATUS_OK;
        case CTF_TYPE_STRUCT:
-               return print_struct(text, field, print_names);
+               return print_struct(text, field, print_names, filter_fields,
+                               filter_array_len);
        case CTF_TYPE_UNTAGGED_VARIANT:
        case CTF_TYPE_VARIANT:
                return print_variant(text, field, print_names);
@@ -690,14 +1263,19 @@ enum bt_component_status print_stream_packet_context(struct text_component *text
        }
        main_field = bt_ctf_packet_get_context(packet);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
+       if (!text->start_line) {
+               fputs(", ", text->out);
+       }
+       text->start_line = false;
        if (text->options.print_scope_field_names) {
-               fputs(" stream.packet.context = ", text->out);
+               print_name_equal(text, "stream.packet.context");
        }
        ret = print_field(text, main_field,
-                       text->options.print_context_field_names);
+                       text->options.print_context_field_names,
+                       stream_packet_context_quarks,
+                       STREAM_PACKET_CONTEXT_QUARKS_LEN);
 end:
        bt_put(main_field);
        bt_put(packet);
@@ -713,14 +1291,17 @@ enum bt_component_status print_event_header_raw(struct text_component *text,
 
        main_field = bt_ctf_event_get_header(event);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
+       if (!text->start_line) {
+               fputs(", ", text->out);
+       }
+       text->start_line = false;
        if (text->options.print_scope_field_names) {
-               fputs(" stream.event.header = ", text->out);
+               print_name_equal(text, "stream.event.header");
        }
        ret = print_field(text, main_field,
-                       text->options.print_context_field_names);
+                       text->options.print_header_field_names, NULL, 0);
 end:
        bt_put(main_field);
        return ret;
@@ -735,14 +1316,17 @@ enum bt_component_status print_stream_event_context(struct text_component *text,
 
        main_field = bt_ctf_event_get_stream_event_context(event);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
+       if (!text->start_line) {
+               fputs(", ", text->out);
+       }
+       text->start_line = false;
        if (text->options.print_scope_field_names) {
-               fputs(" stream.event.context = ", text->out);
+               print_name_equal(text, "stream.event.context");
        }
        ret = print_field(text, main_field,
-                       text->options.print_context_field_names);
+                       text->options.print_context_field_names, NULL, 0);
 end:
        bt_put(main_field);
        return ret;
@@ -757,14 +1341,17 @@ enum bt_component_status print_event_context(struct text_component *text,
 
        main_field = bt_ctf_event_get_event_context(event);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
+       if (!text->start_line) {
+               fputs(", ", text->out);
+       }
+       text->start_line = false;
        if (text->options.print_scope_field_names) {
-               fputs(" event.context = ", text->out);
+               print_name_equal(text, "event.context");
        }
        ret = print_field(text, main_field,
-                       text->options.print_context_field_names);
+                       text->options.print_context_field_names, NULL, 0);
 end:
        bt_put(main_field);
        return ret;
@@ -779,14 +1366,17 @@ enum bt_component_status print_event_payload(struct text_component *text,
 
        main_field = bt_ctf_event_get_payload_field(event);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
+       if (!text->start_line) {
+               fputs(", ", text->out);
+       }
+       text->start_line = false;
        if (text->options.print_scope_field_names) {
-               fputs(" event.fields = ", text->out);
+               print_name_equal(text, "event.fields");
        }
        ret = print_field(text, main_field,
-                       text->options.print_payload_field_names);
+                       text->options.print_payload_field_names, NULL, 0);
 end:
        bt_put(main_field);
        return ret;
@@ -798,40 +1388,33 @@ enum bt_component_status text_print_event(struct text_component *text,
 {
        enum bt_component_status ret;
 
-       //TEST XXX
-       text->options.print_scope_field_names = true;
-       text->options.print_payload_field_names = true;
-       text->options.print_context_field_names = true;
-
+       text->start_line = true;
        ret = print_event_header(text, event);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
        }
-       fputs(",", text->out);
 
        ret = print_stream_packet_context(text, event);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
        }
-       fputs(",", text->out);
 
-       ret = print_event_header_raw(text, event);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto end;
+       if (text->options.verbose) {
+               ret = print_event_header_raw(text, event);
+               if (ret != BT_COMPONENT_STATUS_OK) {
+                       goto end;
+               }
        }
-       fputs(",", text->out);
 
        ret = print_stream_event_context(text, event);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
        }
-       fputs(",", text->out);
 
        ret = print_event_context(text, event);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
        }
-       fputs(",", text->out);
 
        ret = print_event_payload(text, event);
        if (ret != BT_COMPONENT_STATUS_OK) {
This page took 0.035545 seconds and 4 git commands to generate.