Use graph facilities in text component class
[babeltrace.git] / plugins / text / text.c
index 89fa59901d63cac78c253175822b8e155d253017..8a916fc35705a4feeda57b891b22198d0e0c9589 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/plugin/plugin-macros.h>
-#include <babeltrace/plugin/component.h>
-#include <babeltrace/plugin/sink.h>
-#include <babeltrace/plugin/notification/notification.h>
-#include <babeltrace/plugin/notification/iterator.h>
-#include <babeltrace/plugin/notification/event.h>
+#include <babeltrace/plugin/plugin-dev.h>
+#include <babeltrace/component/component.h>
+#include <babeltrace/component/component-sink.h>
+#include <babeltrace/component/component-port.h>
+#include <babeltrace/component/component-connection.h>
+#include <babeltrace/component/notification/notification.h>
+#include <babeltrace/component/notification/iterator.h>
+#include <babeltrace/component/notification/event.h>
 #include <babeltrace/values.h>
 #include <babeltrace/compiler.h>
+#include <babeltrace/common-internal.h>
+#include <plugins-common.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <glib.h>
 #include "text.h"
-
-#define PLUGIN_NAME    "text"
+#include <assert.h>
 
 static
 const char *plugin_options[] = {
+       "color",
        "output-path",
        "debug-info-dir",
        "debug-info-target-prefix",
@@ -53,6 +57,7 @@ const char *plugin_options[] = {
        "clock-seconds",
        "clock-date",
        "clock-gmt",
+       "verbose",
        "name-default",         /* show/hide */
        "name-payload",
        "name-context",
@@ -71,6 +76,7 @@ const char *plugin_options[] = {
 static
 void destroy_text_data(struct text_component *text)
 {
+       bt_put(text->input_iterator);
        (void) g_string_free(text->string, TRUE);
        g_free(text->options.output_path);
        g_free(text->options.debug_info_dir);
@@ -119,7 +125,7 @@ enum bt_component_status handle_notification(struct text_component *text,
        }
 
        switch (bt_notification_get_type(notification)) {
-       case BT_NOTIFICATION_TYPE_PACKET_START:
+       case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
                puts("<packet>");
                break;
        case BT_NOTIFICATION_TYPE_PACKET_END:
@@ -151,6 +157,29 @@ end:
        return ret;
 }
 
+static
+enum bt_component_status text_new_connection(struct bt_port *own_port,
+               struct bt_connection *connection)
+{
+       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+       struct bt_component *component;
+       struct text_component *text;
+
+       component = bt_port_get_component(own_port);
+       assert(component);
+       text = bt_component_get_private_data(component);
+       assert(text);
+       assert(!text->input_iterator);
+       text->input_iterator = bt_connection_create_notification_iterator(
+                       connection);
+
+       if (!text->input_iterator) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+       }
+       bt_put(component);
+       return ret;
+}
+
 static
 enum bt_component_status run(struct bt_component *component)
 {
@@ -159,20 +188,24 @@ enum bt_component_status run(struct bt_component *component)
        struct bt_notification_iterator *it;
        struct text_component *text = bt_component_get_private_data(component);
 
-       ret = bt_component_sink_get_input_iterator(component, 0, &it);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto end;
-       }
+       it = text->input_iterator;
 
-       if (!text->processed_first_event) {
-               ret = bt_notification_iterator_next(it);
-               if (ret != BT_COMPONENT_STATUS_OK) {
+       if (likely(text->processed_first_event)) {
+               enum bt_notification_iterator_status it_ret;
+
+               it_ret = bt_notification_iterator_next(it);
+               switch (it_ret) {
+               case BT_NOTIFICATION_ITERATOR_STATUS_ERROR:
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               case BT_NOTIFICATION_ITERATOR_STATUS_END:
+                       ret = BT_COMPONENT_STATUS_END;
+                       BT_PUT(text->input_iterator);
                        goto end;
+               default:
+                       break;
                }
-       } else {
-               text->processed_first_event = true;
        }
-
        notification = bt_notification_iterator_get_notification(it);
        if (!notification) {
                ret = BT_COMPONENT_STATUS_ERROR;
@@ -180,8 +213,8 @@ enum bt_component_status run(struct bt_component *component)
        }
 
        ret = handle_notification(text, notification);
+       text->processed_first_event = true;
 end:
-       bt_put(it);
        bt_put(notification);
        return ret;
 }
@@ -217,8 +250,7 @@ bool check_param_exists(const char *key, struct bt_value *object, void *data)
 
        if (!bt_value_map_get(plugin_opt_map, key)) {
                fprintf(text->err,
-                       "[warning] Parameter \"%s\" unknown to \"%s\" plugin\n",
-                       key, PLUGIN_NAME);
+                       "[warning] Parameter \"%s\" unknown to \"text\" plugin\n", key);
        }
        return true;
 }
@@ -284,6 +316,13 @@ end:
        return ret;
 }
 
+static
+void warn_wrong_color_param(struct text_component *text)
+{
+       fprintf(text->err,
+               "[warning] Accepted values for the \"color\" parameter are:\n    \"always\", \"auto\", \"never\"\n");
+}
+
 static
 enum bt_component_status apply_params(struct text_component *text,
                struct bt_value *params)
@@ -312,6 +351,34 @@ enum bt_component_status apply_params(struct text_component *text,
                goto end;
        }
        /* Known parameters. */
+       text->options.color = TEXT_COLOR_OPT_AUTO;
+       if (bt_value_map_has_key(params, "color")) {
+               struct bt_value *color_value;
+               const char *color;
+
+               color_value = bt_value_map_get(params, "color");
+               if (!color_value) {
+                       goto end;
+               }
+
+               ret = bt_value_string_get(color_value, &color);
+               if (ret) {
+                       warn_wrong_color_param(text);
+               } else {
+                       if (strcmp(color, "never") == 0) {
+                               text->options.color = TEXT_COLOR_OPT_NEVER;
+                       } else if (strcmp(color, "auto") == 0) {
+                               text->options.color = TEXT_COLOR_OPT_AUTO;
+                       } else if (strcmp(color, "always") == 0) {
+                               text->options.color = TEXT_COLOR_OPT_ALWAYS;
+                       } else {
+                               warn_wrong_color_param(text);
+                       }
+               }
+
+               bt_put(color_value);
+       }
+
        ret = apply_one_string("output-path",
                        params,
                        &text->options.output_path);
@@ -375,6 +442,13 @@ enum bt_component_status apply_params(struct text_component *text,
        }
        text->options.clock_gmt = value;
 
+       value = false;          /* Default. */
+       ret = apply_one_bool("verbose", params, &value, NULL);
+       if (ret != BT_COMPONENT_STATUS_OK) {
+               goto end;
+       }
+       text->options.verbose = value;
+
        /* Names. */
        ret = apply_one_string("name-default", params, &str);
        if (ret != BT_COMPONENT_STATUS_OK) {
@@ -598,9 +672,46 @@ end:
        return ret;
 }
 
+static
+void set_use_colors(struct text_component *text)
+{
+       switch (text->options.color) {
+       case TEXT_COLOR_OPT_ALWAYS:
+               text->use_colors = true;
+               break;
+       case TEXT_COLOR_OPT_AUTO:
+               text->use_colors = text->out == stdout &&
+                       bt_common_colors_supported();
+               break;
+       case TEXT_COLOR_OPT_NEVER:
+               text->use_colors = false;
+               break;
+       }
+}
+
+static
+void init_stream_packet_context_quarks(void)
+{
+       stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
+               g_quark_from_string("timestamp_begin");
+       stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
+               g_quark_from_string("timestamp_begin");
+       stream_packet_context_quarks[Q_TIMESTAMP_END] =
+               g_quark_from_string("timestamp_end");
+       stream_packet_context_quarks[Q_EVENTS_DISCARDED] =
+               g_quark_from_string("events_discarded");
+       stream_packet_context_quarks[Q_CONTENT_SIZE] =
+               g_quark_from_string("content_size");
+       stream_packet_context_quarks[Q_PACKET_SIZE] =
+               g_quark_from_string("packet_size");
+       stream_packet_context_quarks[Q_PACKET_SEQ_NUM] =
+               g_quark_from_string("packet_seq_num");
+}
+
 static
 enum bt_component_status text_component_init(
-               struct bt_component *component, struct bt_value *params)
+               struct bt_component *component, struct bt_value *params,
+               UNUSED_VAR void *init_method_data)
 {
        enum bt_component_status ret;
        struct text_component *text = create_text();
@@ -624,22 +735,15 @@ enum bt_component_status text_component_init(
                goto error;
        }
 
-       ret = bt_component_set_destroy_cb(component,
-                       destroy_text);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto error;
-       }
+       set_use_colors(text);
 
        ret = bt_component_set_private_data(component, text);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto error;
        }
 
-       ret = bt_component_sink_set_consume_cb(component,
-                       run);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto error;
-       }
+       init_stream_packet_context_quarks();
+
 end:
        return ret;
 error:
@@ -648,13 +752,13 @@ error:
 }
 
 /* Initialize plug-in entry points. */
-BT_PLUGIN_NAME("text");
+BT_PLUGIN(text);
 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
 BT_PLUGIN_LICENSE("MIT");
-
-BT_PLUGIN_COMPONENT_CLASSES_BEGIN
-BT_PLUGIN_SINK_COMPONENT_CLASS_ENTRY(PLUGIN_NAME,
-               "Formats CTF-IR to text. Formerly known as ctf-text.",
-               text_component_init)
-BT_PLUGIN_COMPONENT_CLASSES_END
+BT_PLUGIN_SINK_COMPONENT_CLASS(text, run);
+BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(text, text_component_init);
+BT_PLUGIN_SINK_COMPONENT_CLASS_NEW_CONNECTION_METHOD(text, text_new_connection);
+BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(text, destroy_text);
+BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(text,
+       "Formats CTF-IR to text. Formerly known as ctf-text.");
This page took 0.026653 seconds and 4 git commands to generate.