sink.text.pretty: add optional `print-enum-flags` parameter
[babeltrace.git] / src / plugins / text / pretty / pretty.c
index 1a5a759d17603887d6c5fb092f95609155efebb8..cad275145392b9391d74667d3abc1f6907e72ce0 100644 (file)
@@ -5,8 +5,8 @@
  * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  */
 
-#define BT_COMP_LOG_SELF_COMP self_comp
-#define BT_LOG_OUTPUT_LEVEL log_level
+#define BT_COMP_LOG_SELF_COMP (pretty->self_comp)
+#define BT_LOG_OUTPUT_LEVEL (pretty->log_level)
 #define BT_LOG_TAG "PLUGIN/SINK.TEXT.PRETTY"
 #include "logging/comp-logging.h"
 
@@ -28,6 +28,7 @@ const char * const in_port_name = "in";
 static
 void destroy_pretty_data(struct pretty_component *pretty)
 {
+       uint64_t i;
        if (!pretty) {
                goto end;
        }
@@ -50,6 +51,13 @@ void destroy_pretty_data(struct pretty_component *pretty)
                        perror("close output file");
                }
        }
+
+       for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
+               if (pretty->enum_bit_labels[i]) {
+                       g_ptr_array_free(pretty->enum_bit_labels[i], true);
+               }
+       }
+
        g_free(pretty->options.output_path);
        g_free(pretty);
 
@@ -103,12 +111,16 @@ bt_message_iterator_class_next_method_status handle_message(
        switch (bt_message_get_type(message)) {
        case BT_MESSAGE_TYPE_EVENT:
                if (pretty_print_event(pretty, message)) {
+                       BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp,
+                               "Failed to print one event.");
                        ret = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
                }
                break;
        case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
        case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
                if (pretty_print_discarded_items(pretty, message)) {
+                       BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp,
+                               "Failed to print discarded items.");
                        ret = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
                }
                break;
@@ -129,9 +141,7 @@ pretty_graph_is_configured(bt_self_component_sink *self_comp_sink)
        struct pretty_component *pretty;
        bt_self_component *self_comp =
                bt_self_component_sink_as_self_component(self_comp_sink);
-       const bt_component *comp = bt_self_component_as_component(self_comp);
        bt_self_component_port_input *in_port;
-       bt_logging_level log_level = bt_component_get_logging_level(comp);
 
        pretty = bt_self_component_get_data(self_comp);
        BT_ASSERT(pretty);
@@ -164,8 +174,7 @@ BT_HIDDEN
 bt_component_class_sink_consume_method_status pretty_consume(
                bt_self_component_sink *comp)
 {
-       bt_component_class_sink_consume_method_status ret =
-               BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
+       bt_component_class_sink_consume_method_status status;
        bt_message_array_const msgs;
        bt_message_iterator *it;
        struct pretty_component *pretty = bt_self_component_get_data(
@@ -177,41 +186,28 @@ bt_component_class_sink_consume_method_status pretty_consume(
        it = pretty->iterator;
        next_status = bt_message_iterator_next(it,
                &msgs, &count);
-
-       switch (next_status) {
-       case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
-               break;
-       case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
-       case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
-               ret = (int) next_status;
-               goto end;
-       case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
-               ret = (int) next_status;
-               BT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
-                       pretty->iterator);
-               goto end;
-       default:
-               ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
+       if (next_status != BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) {
+               status = (int) next_status;
                goto end;
        }
 
-       BT_ASSERT_DBG(next_status == BT_MESSAGE_ITERATOR_NEXT_STATUS_OK);
-
        for (i = 0; i < count; i++) {
-               ret = (int) handle_message(pretty, msgs[i]);
-               if (ret) {
+               status = (int) handle_message(pretty, msgs[i]);
+               if (status) {
                        goto end;
                }
 
                bt_message_put_ref(msgs[i]);
        }
 
+       status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
+
 end:
        for (; i < count; i++) {
                bt_message_put_ref(msgs[i]);
        }
 
-       return ret;
+       return status;
 }
 
 static
@@ -324,13 +320,13 @@ struct bt_param_validation_map_value_entry_descr pretty_params[] = {
        { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
        { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
        { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
+       { "print-enum-flags", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
        BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
 };
 
 static
 bt_component_class_initialize_method_status apply_params(
-               struct pretty_component *pretty, const bt_value *params,
-               bt_self_component *self_comp, bt_logging_level log_level)
+               struct pretty_component *pretty, const bt_value *params)
 {
        int ret;
        const bt_value *value;
@@ -345,7 +341,7 @@ bt_component_class_initialize_method_status apply_params(
                goto end;
        } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
                status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
-               BT_COMP_LOGE_APPEND_CAUSE(self_comp, "%s", validate_error);
+               BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, "%s", validate_error);
                goto end;
        }
 
@@ -368,6 +364,8 @@ bt_component_class_initialize_method_status apply_params(
        apply_one_string("path", params, &pretty->options.output_path);
        ret = open_output_file(pretty);
        if (ret) {
+               BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp,
+                       "Failed to open output file: %s", validate_error);
                status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
                goto end;
        }
@@ -392,6 +390,9 @@ bt_component_class_initialize_method_status apply_params(
        apply_one_bool_with_default("verbose", params,
                &pretty->options.verbose, false);
 
+       apply_one_bool_with_default("print-enum-flags", params,
+               &pretty->options.print_enum_flags, false);
+
        /* Names. */
        value = bt_value_map_borrow_entry_value_const(params, "name-default");
        if (value) {
@@ -558,10 +559,21 @@ bt_component_class_initialize_method_status pretty_init(
        bt_logging_level log_level = bt_component_get_logging_level(comp);
 
        if (!pretty) {
+               /*
+                * Don't use BT_COMP_LOGE_APPEND_CAUSE, as `pretty` is not
+                * initialized yet.
+                */
+               BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
+                       "Failed to allocate component.");
+               BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
+                       self_comp, "Failed to allocate component.");
                status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto error;
        }
 
+       pretty->self_comp = self_comp;
+       pretty->log_level = log_level;
+
        add_port_status = bt_self_component_sink_add_input_port(self_comp_sink,
                in_port_name, NULL, NULL);
        if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
@@ -578,12 +590,24 @@ bt_component_class_initialize_method_status pretty_init(
        pretty->delta_real_timestamp = -1ULL;
        pretty->last_real_timestamp = -1ULL;
 
-       status = apply_params(pretty, params, self_comp, log_level);
+       status = apply_params(pretty, params);
        if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
                goto error;
        }
 
        set_use_colors(pretty);
+
+       if (pretty->options.print_enum_flags) {
+               uint64_t i;
+               /*
+                * Allocate all label arrays during the initialization of the
+                * component and reuse the same set of arrays for all
+                * enumerations.
+                */
+               for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
+                       pretty->enum_bit_labels[i] = g_ptr_array_new();
+               }
+       }
        bt_self_component_set_data(self_comp, pretty);
 
        status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
This page took 0.026275 seconds and 4 git commands to generate.