Implement output text plugin (basic)
[babeltrace.git] / plugins / text / text.c
index 50d5a4bc5b696b4fca3ae442bb69536d45df7462..eca7ed73cfe05792df6fe05bc09314f14fe35544 100644 (file)
 #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>
-
-static
-enum bt_component_status ctf_text_init(struct bt_component *,
-               struct bt_value *params);
-
-/* Initialize plug-in entry points. */
-BT_PLUGIN_NAME("ctf-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("text", "Formats CTF-IR to text. Formerly known as ctf-text.", ctf_text_init)
-BT_PLUGIN_COMPONENT_CLASSES_END
-
-enum loglevel {
-        LOGLEVEL_EMERG                  = 0,
-        LOGLEVEL_ALERT                  = 1,
-        LOGLEVEL_CRIT                   = 2,
-        LOGLEVEL_ERR                    = 3,
-        LOGLEVEL_WARNING                = 4,
-        LOGLEVEL_NOTICE                 = 5,
-        LOGLEVEL_INFO                   = 6,
-        LOGLEVEL_DEBUG_SYSTEM           = 7,
-        LOGLEVEL_DEBUG_PROGRAM          = 8,
-        LOGLEVEL_DEBUG_PROCESS          = 9,
-        LOGLEVEL_DEBUG_MODULE           = 10,
-        LOGLEVEL_DEBUG_UNIT             = 11,
-        LOGLEVEL_DEBUG_FUNCTION         = 12,
-        LOGLEVEL_DEBUG_LINE             = 13,
-        LOGLEVEL_DEBUG                  = 14,
-};
+#include <glib.h>
+#include "text.h"
 
 static
 const char *loglevel_str [] = {
@@ -84,28 +56,167 @@ const char *loglevel_str [] = {
        [LOGLEVEL_DEBUG] =              "TRACE_DEBUG",
 };
 
-struct ctf_text_component {
-       bool opt_print_all_field_names : 1;
-       bool opt_print_scope_field_names : 1;
-       bool opt_print_header_field_names : 1;
-       bool opt_print_context_field_names : 1;
-       bool opt_print_payload_field_names : 1;
-       bool opt_print_all_fields : 1;
-       bool opt_print_trace_field : 1;
-       bool opt_print_trace_domain_field : 1;
-       bool opt_print_trace_procname_field : 1;
-       bool opt_print_trace_vpid_field : 1;
-       bool opt_print_trace_hostname_field : 1;
-       bool opt_print_trace_default_fields : 1;
-       bool opt_print_loglevel_field : 1;
-       bool opt_print_emf_field : 1;
-       bool opt_print_delta_field : 1;
-};
+static
+void destroy_text_data(struct text_component *text)
+{
+       (void) g_string_free(text->string, TRUE);
+       g_free(text);
+}
+
+static
+struct text_component *create_text(void)
+{
+       struct text_component *text;
+
+       text = g_new0(struct text_component, 1);
+       if (!text) {
+               goto end;
+       }
+       text->string = g_string_new("");
+       if (!text->string) {
+               goto error;
+       }
+end:
+       return text;
+
+error:
+       g_free(text);
+       return NULL;
+}
 
 static
-enum bt_component_status ctf_text_init(
-               struct bt_component *component, struct bt_value *params)
+void destroy_text(struct bt_component *component)
 {
-       printf("ctf_text_init\n");
-       return BT_COMPONENT_STATUS_OK;
+       void *data = bt_component_get_private_data(component);
+
+       destroy_text_data(data);
 }
+
+static
+enum bt_component_status handle_notification(struct text_component *text,
+       struct bt_notification *notification)
+{
+       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+
+       if (!text) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+
+       switch (bt_notification_get_type(notification)) {
+       case BT_NOTIFICATION_TYPE_PACKET_START:
+               puts("<packet>");
+               break;
+       case BT_NOTIFICATION_TYPE_PACKET_END:
+               puts("</packet>");
+               break;
+       case BT_NOTIFICATION_TYPE_EVENT:
+       {
+               struct bt_ctf_event *event = bt_notification_event_get_event(
+                               notification);
+
+               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;
+               }
+               break;
+       }
+       case BT_NOTIFICATION_TYPE_STREAM_END:
+               puts("</stream>");
+               break;
+       default:
+               puts("Unhandled notification type");
+       }
+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)
+{
+       enum bt_component_status ret;
+       struct text_component *text = create_text();
+
+       if (!text) {
+               ret = BT_COMPONENT_STATUS_NOMEM;
+               goto end;
+       }
+
+       ret = bt_component_set_destroy_cb(component,
+                       destroy_text);
+       if (ret != BT_COMPONENT_STATUS_OK) {
+               goto error;
+       }
+
+       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;
+       }
+
+       text->out = stdout;
+       text->err = stderr;
+end:
+       return ret;
+error:
+       destroy_text_data(text);
+       return ret;
+}
+
+/* Initialize plug-in entry points. */
+BT_PLUGIN_NAME("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("text",
+               "Formats CTF-IR to text. Formerly known as ctf-text.",
+               text_component_init)
+BT_PLUGIN_COMPONENT_CLASSES_END
This page took 0.025756 seconds and 4 git commands to generate.