lib: make trace IR API const-correct
[babeltrace.git] / plugins / ctf / fs-sink / writer.c
index e233a87da5ffa4d89da912a567fa57081b3dcea4..b87d78d26ceddf6064aceeaee10a656561faaefc 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-ir/packet.h>
-#include <babeltrace/plugin/plugin-dev.h>
-#include <babeltrace/graph/component.h>
-#include <babeltrace/graph/private-component.h>
-#include <babeltrace/graph/component-sink.h>
-#include <babeltrace/graph/private-component-sink.h>
-#include <babeltrace/graph/private-port.h>
-#include <babeltrace/graph/private-connection.h>
-#include <babeltrace/graph/notification.h>
-#include <babeltrace/graph/notification-iterator.h>
-#include <babeltrace/graph/notification-event.h>
-#include <babeltrace/graph/notification-packet.h>
-#include <babeltrace/graph/notification-stream.h>
+#define BT_LOG_TAG "PLUGIN-CTF-FS-SINK-WRITER"
+#include "logging.h"
+
+#include <babeltrace/babeltrace.h>
 #include <plugins-common.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <glib.h>
 #include "writer.h"
-#include <assert.h>
+#include <babeltrace/assert-internal.h>
 
-gboolean empty_ht(gpointer key, gpointer value, gpointer user_data)
+static
+gboolean empty_trace_map(gpointer key, gpointer value, gpointer user_data)
 {
+       struct fs_writer *fs_writer = value;
+       struct writer_component *writer_component = user_data;
+
+       fs_writer->trace_static = 1;
+       writer_close(writer_component, fs_writer);
+
        return TRUE;
 }
 
 static
 void destroy_writer_component_data(struct writer_component *writer_component)
 {
-       bt_put(writer_component->input_iterator);
-
-       g_hash_table_foreach_remove(writer_component->stream_class_map,
-                       empty_ht, NULL);
-       g_hash_table_destroy(writer_component->stream_class_map);
-
-       g_hash_table_foreach_remove(writer_component->stream_map,
-                       empty_ht, NULL);
-       g_hash_table_destroy(writer_component->stream_map);
+       bt_object_put_ref(writer_component->input_iterator);
 
        g_hash_table_foreach_remove(writer_component->trace_map,
-                       empty_ht, NULL);
+                       empty_trace_map, writer_component);
        g_hash_table_destroy(writer_component->trace_map);
 
        g_string_free(writer_component->base_path, true);
@@ -73,31 +63,19 @@ void destroy_writer_component_data(struct writer_component *writer_component)
 }
 
 BT_HIDDEN
-void writer_component_finalize(struct bt_private_component *component)
+void writer_component_finalize(struct bt_self_component *component)
 {
        struct writer_component *writer_component = (struct writer_component *)
-               bt_private_component_get_user_data(component);
+               bt_self_component_get_user_data(component);
 
        destroy_writer_component_data(writer_component);
        g_free(writer_component);
 }
 
-static
-void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class)
-{
-       bt_put(writer_stream_class);
-}
-
-static
-void unref_stream(struct bt_ctf_stream_class *writer_stream)
-{
-       bt_put(writer_stream);
-}
-
 static
 void free_fs_writer(struct fs_writer *fs_writer)
 {
-       bt_put(fs_writer->writer);
+       bt_object_put_ref(fs_writer->writer);
        g_free(fs_writer);
 }
 
@@ -114,7 +92,6 @@ struct writer_component *create_writer_component(void)
        writer_component->err = stderr;
        writer_component->trace_id = 0;
        writer_component->trace_name_base = g_string_new("trace");
-       writer_component->processed_first_event = false;
        if (!writer_component->trace_name_base) {
                g_free(writer_component);
                writer_component = NULL;
@@ -126,10 +103,6 @@ struct writer_component *create_writer_component(void)
         */
        writer_component->trace_map = g_hash_table_new_full(g_direct_hash,
                        g_direct_equal, NULL, (GDestroyNotify) free_fs_writer);
-       writer_component->stream_class_map = g_hash_table_new_full(g_direct_hash,
-                       g_direct_equal, NULL, (GDestroyNotify) unref_stream_class);
-       writer_component->stream_map = g_hash_table_new_full(g_direct_hash,
-                       g_direct_equal, NULL, (GDestroyNotify) unref_stream);
 
 end:
        return writer_component;
@@ -150,7 +123,7 @@ enum bt_component_status handle_notification(
        switch (bt_notification_get_type(notification)) {
        case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
        {
-               struct bt_ctf_packet *packet =
+               const struct bt_packet *packet =
                        bt_notification_packet_begin_get_packet(notification);
 
                if (!packet) {
@@ -159,12 +132,12 @@ enum bt_component_status handle_notification(
                }
 
                ret = writer_new_packet(writer_component, packet);
-               bt_put(packet);
+               bt_object_put_ref(packet);
                break;
        }
        case BT_NOTIFICATION_TYPE_PACKET_END:
        {
-               struct bt_ctf_packet *packet =
+               const struct bt_packet *packet =
                        bt_notification_packet_end_get_packet(notification);
 
                if (!packet) {
@@ -172,12 +145,12 @@ enum bt_component_status handle_notification(
                        goto end;
                }
                ret = writer_close_packet(writer_component, packet);
-               bt_put(packet);
+               bt_object_put_ref(packet);
                break;
        }
        case BT_NOTIFICATION_TYPE_EVENT:
        {
-               struct bt_ctf_event *event = bt_notification_event_get_event(
+               const struct bt_event *event = bt_notification_event_get_event(
                                notification);
 
                if (!event) {
@@ -185,7 +158,7 @@ enum bt_component_status handle_notification(
                        goto end;
                }
                ret = writer_output_event(writer_component, event);
-               bt_put(event);
+               bt_object_put_ref(event);
                if (ret != BT_COMPONENT_STATUS_OK) {
                        goto end;
                }
@@ -193,7 +166,7 @@ enum bt_component_status handle_notification(
        }
        case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
        {
-               struct bt_ctf_stream *stream =
+               const struct bt_stream *stream =
                        bt_notification_stream_begin_get_stream(notification);
 
                if (!stream) {
@@ -201,12 +174,12 @@ enum bt_component_status handle_notification(
                        goto end;
                }
                ret = writer_stream_begin(writer_component, stream);
-               bt_put(stream);
+               bt_object_put_ref(stream);
                break;
        }
        case BT_NOTIFICATION_TYPE_STREAM_END:
        {
-               struct bt_ctf_stream *stream =
+               const struct bt_stream *stream =
                        bt_notification_stream_end_get_stream(notification);
 
                if (!stream) {
@@ -214,11 +187,11 @@ enum bt_component_status handle_notification(
                        goto end;
                }
                ret = writer_stream_end(writer_component, stream);
-               bt_put(stream);
+               bt_object_put_ref(stream);
                break;
        }
        default:
-               puts("Unhandled notification type");
+               break;
        }
 end:
        return ret;
@@ -226,45 +199,36 @@ end:
 
 BT_HIDDEN
 void writer_component_port_connected(
-               struct bt_private_component *component,
+               struct bt_self_component *component,
                struct bt_private_port *self_port,
                struct bt_port *other_port)
 {
        struct bt_private_connection *connection;
        struct writer_component *writer;
-       static const enum bt_notification_type notif_types[] = {
-               BT_NOTIFICATION_TYPE_EVENT,
-               BT_NOTIFICATION_TYPE_PACKET_BEGIN,
-               BT_NOTIFICATION_TYPE_PACKET_END,
-               BT_NOTIFICATION_TYPE_STREAM_BEGIN,
-               BT_NOTIFICATION_TYPE_STREAM_END,
-               BT_NOTIFICATION_TYPE_SENTINEL,
-       };
-
-       writer = bt_private_component_get_user_data(component);
-       assert(writer);
-       assert(!writer->input_iterator);
-       connection = bt_private_port_get_private_connection(self_port);
-       assert(connection);
-       writer->input_iterator =
-               bt_private_connection_create_notification_iterator(connection,
-                       notif_types);
-
-       if (!writer->input_iterator) {
+       enum bt_connection_status conn_status;
+
+       writer = bt_self_component_get_user_data(component);
+       BT_ASSERT(writer);
+       BT_ASSERT(!writer->input_iterator);
+       connection = bt_private_port_get_connection(self_port);
+       BT_ASSERT(connection);
+       conn_status = bt_private_connection_create_notification_iterator(
+               connection, &writer->input_iterator);
+       if (conn_status != BT_CONNECTION_STATUS_OK) {
                writer->error = true;
        }
 
-       bt_put(connection);
+       bt_object_put_ref(connection);
 }
 
 BT_HIDDEN
-enum bt_component_status writer_run(struct bt_private_component *component)
+enum bt_component_status writer_run(struct bt_self_component *component)
 {
        enum bt_component_status ret;
        struct bt_notification *notification = NULL;
        struct bt_notification_iterator *it;
        struct writer_component *writer_component =
-               bt_private_component_get_user_data(component);
+               bt_self_component_get_user_data(component);
        enum bt_notification_iterator_status it_ret;
 
        if (unlikely(writer_component->error)) {
@@ -273,13 +237,13 @@ enum bt_component_status writer_run(struct bt_private_component *component)
        }
 
        it = writer_component->input_iterator;
-       assert(it);
+       BT_ASSERT(it);
        it_ret = bt_notification_iterator_next(it);
 
        switch (it_ret) {
        case BT_NOTIFICATION_ITERATOR_STATUS_END:
                ret = BT_COMPONENT_STATUS_END;
-               BT_PUT(writer_component->input_iterator);
+               BT_OBJECT_PUT_REF_AND_RESET(writer_component->input_iterator);
                goto end;
        case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
                ret = BT_COMPONENT_STATUS_AGAIN;
@@ -292,16 +256,42 @@ enum bt_component_status writer_run(struct bt_private_component *component)
        }
 
        notification = bt_notification_iterator_get_notification(it);
-       assert(notification);
+       BT_ASSERT(notification);
        ret = handle_notification(writer_component, notification);
 end:
-       bt_put(notification);
+       bt_object_put_ref(notification);
+       return ret;
+}
+
+static
+enum bt_component_status apply_one_bool(const char *key,
+               struct bt_value *params,
+               bool *option,
+               bool *found)
+{
+       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
+       struct bt_value *value = NULL;
+       enum bt_value_status status;
+       bt_bool bool_val;
+
+       value = bt_value_map_get(params, key);
+       if (!value) {
+               goto end;
+       }
+       bool_val = bt_value_bool_get(value);
+
+       *option = (bool) bool_val;
+       if (found) {
+               *found = true;
+       }
+end:
+       bt_object_put_ref(value);
        return ret;
 }
 
 BT_HIDDEN
 enum bt_component_status writer_component_init(
-       struct bt_private_component *component, struct bt_value *params,
+       struct bt_self_component *component, struct bt_value *params,
        UNUSED_VAR void *init_method_data)
 {
        enum bt_component_status ret;
@@ -309,26 +299,21 @@ enum bt_component_status writer_component_init(
        struct writer_component *writer_component = create_writer_component();
        struct bt_value *value = NULL;
        const char *path;
-       void *priv_port;
 
        if (!writer_component) {
                ret = BT_COMPONENT_STATUS_NOMEM;
                goto end;
        }
 
-       priv_port = bt_private_component_sink_add_input_private_port(component,
-               "in", NULL);
-       if (!priv_port) {
-               ret = BT_COMPONENT_STATUS_NOMEM;
+       ret = bt_self_component_sink_add_input_port(component,
+               "in", NULL, NULL);
+       if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
        }
 
-       bt_put(priv_port);
-
        value = bt_value_map_get(params, "path");
        if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
-               fprintf(writer_component->err,
-                               "[error] output path parameter required\n");
+               BT_LOGE_STR("Missing mandatory \"path\" parameter.");
                ret = BT_COMPONENT_STATUS_INVALID;
                goto error;
        }
@@ -338,15 +323,22 @@ enum bt_component_status writer_component_init(
                ret = BT_COMPONENT_STATUS_INVALID;
                goto error;
        }
-       bt_put(value);
+       bt_object_put_ref(value);
 
        writer_component->base_path = g_string_new(path);
-       if (!writer_component) {
+       if (!writer_component->base_path) {
                ret = BT_COMPONENT_STATUS_ERROR;
                goto error;
        }
 
-       ret = bt_private_component_set_user_data(component, writer_component);
+       writer_component->single_trace = false;
+       ret = apply_one_bool("single-trace", params,
+                       &writer_component->single_trace, NULL);
+       if (ret != BT_COMPONENT_STATUS_OK) {
+               goto end;
+       }
+
+       ret = bt_self_component_set_user_data(component, writer_component);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto error;
        }
This page took 0.028268 seconds and 4 git commands to generate.