Implement output text plugin (basic)
[babeltrace.git] / plugins / text / text.c
index fc32c75e90d57e027249c7411fb1469c6ff9cd3c..eca7ed73cfe05792df6fe05bc09314f14fe35544 100644 (file)
@@ -30,6 +30,7 @@
 #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 <stdio.h>
 #include <stdbool.h>
@@ -55,18 +56,10 @@ const char *loglevel_str [] = {
        [LOGLEVEL_DEBUG] =              "TRACE_DEBUG",
 };
 
-static
-void destroy_stream_timestamp(void *stream_timestamp)
-{
-       g_free(stream_timestamp);
-}
-
 static
 void destroy_text_data(struct text_component *text)
 {
-       if (text->stream_timestamps) {
-               g_hash_table_destroy(text->stream_timestamps);
-       }
+       (void) g_string_free(text->string, TRUE);
        g_free(text);
 }
 
@@ -79,20 +72,20 @@ struct text_component *create_text(void)
        if (!text) {
                goto end;
        }
-
-       text->stream_timestamps = g_hash_table_new_full(
-                       NULL, NULL, NULL, destroy_stream_timestamp);
-       if (!text->stream_timestamps) {
+       text->string = g_string_new("");
+       if (!text->string) {
                goto error;
        }
 end:
        return text;
+
 error:
-       destroy_text_data(text);
+       g_free(text);
        return NULL;
 }
 
-static void destroy_text(struct bt_component *component)
+static
+void destroy_text(struct bt_component *component)
 {
        void *data = bt_component_get_private_data(component);
 
@@ -100,11 +93,10 @@ static void destroy_text(struct bt_component *component)
 }
 
 static
-enum bt_component_status handle_notification(struct bt_component *component,
+enum bt_component_status handle_notification(struct text_component *text,
        struct bt_notification *notification)
 {
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
-       struct text_component *text = bt_component_get_private_data(component);
 
        if (!text) {
                ret = BT_COMPONENT_STATUS_ERROR;
@@ -123,12 +115,12 @@ enum bt_component_status handle_notification(struct bt_component *component,
                struct bt_ctf_event *event = bt_notification_event_get_event(
                                notification);
 
-               puts("\t<event>");
                if (!event) {
                        ret = BT_COMPONENT_STATUS_ERROR;
                        goto end;
                }
                ret = text_print_event(text, event);
+               bt_put(event);
                if (ret != BT_COMPONENT_STATUS_OK) {
                        goto end;
                }
@@ -144,6 +136,41 @@ end:
        return ret;
 }
 
+static
+enum bt_component_status run(struct bt_component *component)
+{
+       enum bt_component_status ret;
+       struct bt_notification *notification = NULL;
+       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;
+       }
+
+       if (!text->processed_first_event) {
+               ret = bt_notification_iterator_next(it);
+               if (ret != BT_COMPONENT_STATUS_OK) {
+                       goto end;
+               }
+       } else {
+               text->processed_first_event = true;
+       }
+
+       notification = bt_notification_iterator_get_notification(it);
+       if (!notification) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+
+       ret = handle_notification(text, notification);
+end:
+       bt_put(it);
+       bt_put(notification);
+       return ret;
+}
+
 static
 enum bt_component_status text_component_init(
        struct bt_component *component, struct bt_value *params)
@@ -167,8 +194,8 @@ enum bt_component_status text_component_init(
                goto error;
        }
 
-       ret = bt_component_sink_set_handle_notification_cb(component,
-                       handle_notification);
+       ret = bt_component_sink_set_consume_cb(component,
+                       run);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto error;
        }
This page took 0.025514 seconds and 4 git commands to generate.