copytrace: create empty stream classes by default
[babeltrace.git] / plugins / libctfcopytrace / ctfcopytrace.c
index bfd5112946a11ff40aafeeb6e2249b45f716a79b..5c78d6f8c968b89f7ed665e8d77eef620200c48e 100644 (file)
@@ -66,18 +66,14 @@ struct bt_ctf_clock_class *ctf_copy_clock_class(FILE *err,
        }
 
        description = bt_ctf_clock_class_get_description(clock_class);
-       if (!description) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto end_destroy;
-       }
-
-       int_ret = bt_ctf_clock_class_set_description(writer_clock_class,
-                       description);
-       if (int_ret != 0) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
-                               __LINE__);
-               goto end_destroy;
+       if (description) {
+               int_ret = bt_ctf_clock_class_set_description(writer_clock_class,
+                               description);
+               if (int_ret != 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
+                                       __LINE__);
+                       goto end_destroy;
+               }
        }
 
        u64_ret = bt_ctf_clock_class_get_frequency(clock_class);
@@ -214,9 +210,12 @@ struct bt_ctf_event_class *ctf_copy_event_class(FILE *err,
                struct bt_ctf_event_class *event_class)
 {
        struct bt_ctf_event_class *writer_event_class = NULL;
-       struct bt_ctf_field_type *context;
+       struct bt_ctf_field_type *context, *payload_type;
        const char *name;
-       int count, i, ret;
+       int ret;
+       int64_t id;
+       enum bt_ctf_event_class_log_level log_level;
+       const char *emf_uri;
 
        name = bt_ctf_event_class_get_name(event_class);
        if (!name) {
@@ -232,58 +231,55 @@ struct bt_ctf_event_class *ctf_copy_event_class(FILE *err,
                goto end;
        }
 
-       count = bt_ctf_event_class_get_attribute_count(event_class);
-       for (i = 0; i < count; i++) {
-               const char *attr_name;
-               struct bt_value *attr_value;
-               int ret;
+       id = bt_ctf_event_class_get_id(event_class);
+       if (id < 0) {
+               fprintf(err, "[error] %s in %s:%d\n", __func__,
+                               __FILE__, __LINE__);
+               goto error;
+       }
 
-               attr_name = bt_ctf_event_class_get_attribute_name_by_index(
-                       event_class, i);
-               if (!attr_name) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__,
-                                       __FILE__, __LINE__);
-                       goto error;
-               }
-               attr_value = bt_ctf_event_class_get_attribute_value_by_index(
-                       event_class, i);
-               if (!attr_value) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__,
-                                       __FILE__, __LINE__);
-                       goto error;
-               }
+       ret = bt_ctf_event_class_set_id(writer_event_class, id);
+       if (ret) {
+               fprintf(err, "[error] %s in %s:%d\n", __func__,
+                               __FILE__, __LINE__);
+               goto error;
+       }
 
-               ret = bt_ctf_event_class_set_attribute(writer_event_class,
-                               attr_name, attr_value);
-               BT_PUT(attr_value);
-               if (ret < 0) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__,
-                                       __FILE__, __LINE__);
-                       goto error;
-               }
+       log_level = bt_ctf_event_class_get_log_level(event_class);
+       if (log_level < 0) {
+               fprintf(err, "[error] %s in %s:%d\n", __func__,
+                               __FILE__, __LINE__);
+               goto error;
        }
 
-       count = bt_ctf_event_class_get_payload_type_field_count(event_class);
-       for (i = 0; i < count; i++) {
-               const char *field_name;
-               struct bt_ctf_field_type *field_type;
-               int ret;
+       ret = bt_ctf_event_class_set_log_level(writer_event_class, log_level);
+       if (ret) {
+               fprintf(err, "[error] %s in %s:%d\n", __func__,
+                               __FILE__, __LINE__);
+               goto error;
+       }
 
-               ret = bt_ctf_event_class_get_payload_type_field_by_index(
-                               event_class, &field_name, &field_type, i);
-               if (ret < 0) {
-                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+       emf_uri = bt_ctf_event_class_get_emf_uri(event_class);
+       if (emf_uri) {
+               ret = bt_ctf_event_class_set_emf_uri(writer_event_class,
+                       emf_uri);
+               if (ret) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
                        goto error;
                }
+       }
 
-               ret = bt_ctf_event_class_add_field(writer_event_class, field_type,
-                               field_name);
-               BT_PUT(field_type);
+       payload_type = bt_ctf_event_class_get_payload_type(event_class);
+       if (payload_type) {
+               ret = bt_ctf_event_class_set_payload_type(writer_event_class,
+                               payload_type);
                if (ret < 0) {
-                       fprintf(err, "[error] Cannot add field %s\n", field_name);
-                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       fprintf(err, "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
                        goto error;
                }
+               BT_PUT(payload_type);
        }
 
        context = bt_ctf_event_class_get_context_type(event_class);
@@ -333,6 +329,22 @@ enum bt_component_status ctf_copy_event_classes(FILE *err,
                        ret = BT_COMPONENT_STATUS_ERROR;
                        goto error;
                }
+               if (i < bt_ctf_stream_class_get_event_class_count(writer_stream_class)) {
+                       writer_event_class = bt_ctf_stream_class_get_event_class_by_index(
+                                       writer_stream_class, i);
+                       if (writer_event_class) {
+                               /*
+                                * If the writer_event_class already exists,
+                                * just skip it. It can be used to resync the
+                                * event_classes after a trace has become
+                                * static.
+                                */
+                               BT_PUT(writer_event_class);
+                               BT_PUT(event_class);
+                               continue;
+                       }
+               }
+
                writer_event_class = ctf_copy_event_class(err, event_class);
                if (!writer_event_class) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
@@ -378,7 +390,7 @@ struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err,
                name = NULL;
        }
 
-       writer_stream_class = bt_ctf_stream_class_create(name);
+       writer_stream_class = bt_ctf_stream_class_create_empty(name);
        if (!writer_stream_class) {
                fprintf(err, "[error] %s in %s:%d\n",
                                __func__, __FILE__, __LINE__);
@@ -527,8 +539,7 @@ end:
 BT_HIDDEN
 struct bt_ctf_field *ctf_copy_packet_context(FILE *err,
                struct bt_ctf_packet *packet,
-               struct bt_ctf_stream *writer_stream,
-               int skip_content_size)
+               struct bt_ctf_stream *writer_stream)
 {
        enum bt_component_status ret;
        struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
@@ -590,13 +601,6 @@ struct bt_ctf_field *ctf_copy_packet_context(FILE *err,
                                        __FILE__, __LINE__);
                        goto error;
                }
-               if (skip_content_size &&
-                               (!strncmp(field_name, "content_size", strlen("content_size")) ||
-                               !strncmp(field_name, "packet_size", strlen("packet_size")))) {
-                       BT_PUT(field_type);
-                       BT_PUT(field);
-                       continue;
-               }
 
                if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
                        fprintf(err, "[error] Unexpected packet context field type\n");
@@ -768,8 +772,13 @@ struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event,
 
        /* Optional field, so it can fail silently. */
        field = bt_ctf_event_get_stream_event_context(event);
-       copy_field = bt_ctf_field_copy(field);
-       if (copy_field) {
+       if (field) {
+               copy_field = bt_ctf_field_copy(field);
+               if (!copy_field) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
+                       goto error;
+               }
                ret = bt_ctf_event_set_stream_event_context(writer_event,
                                copy_field);
                if (ret < 0) {
@@ -777,41 +786,46 @@ struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event,
                                        __FILE__, __LINE__);
                        goto error;
                }
+               BT_PUT(field);
+               BT_PUT(copy_field);
        }
-       BT_PUT(field);
-       BT_PUT(copy_field);
 
        /* Optional field, so it can fail silently. */
        field = bt_ctf_event_get_event_context(event);
-       copy_field = bt_ctf_field_copy(field);
-       if (copy_field) {
+       if (field) {
+               copy_field = bt_ctf_field_copy(field);
+               if (!copy_field) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
+                       goto error;
+               }
                ret = bt_ctf_event_set_event_context(writer_event, copy_field);
                if (ret < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
                        goto error;
                }
+               BT_PUT(field);
+               BT_PUT(copy_field);
        }
-       BT_PUT(field);
-       BT_PUT(copy_field);
 
        field = bt_ctf_event_get_event_payload(event);
-       if (!field) {
-               fprintf(err, "[error] %s in %s:%d\n", __func__,
-                               __FILE__, __LINE__);
-               goto error;
-       }
-       copy_field = bt_ctf_field_copy(field);
-       if (copy_field) {
+       if (field) {
+               copy_field = bt_ctf_field_copy(field);
+               if (!copy_field) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__,
+                                       __FILE__, __LINE__);
+                       goto error;
+               }
                ret = bt_ctf_event_set_event_payload(writer_event, copy_field);
                if (ret < 0) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__,
                                        __FILE__, __LINE__);
                        goto error;
                }
+               BT_PUT(field);
+               BT_PUT(copy_field);
        }
-       BT_PUT(field);
-       BT_PUT(copy_field);
 
        goto end;
 
@@ -831,6 +845,7 @@ enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace,
        int field_count, i, int_ret;
        struct bt_ctf_field_type *header_type = NULL;
        enum bt_ctf_byte_order order;
+       const char *trace_name;
 
        field_count = bt_ctf_trace_get_environment_field_count(trace);
        for (i = 0; i < field_count; i++) {
@@ -881,7 +896,7 @@ enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace,
         * the CTF writer object chooses, which is the machine's native
         * byte order.
         */
-       if (order != BT_CTF_BYTE_ORDER_NONE) {
+       if (order != BT_CTF_BYTE_ORDER_UNSPECIFIED) {
                ret = bt_ctf_trace_set_native_byte_order(writer_trace, order);
                if (ret) {
                        fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
@@ -901,6 +916,16 @@ enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace,
                }
        }
 
+       trace_name = bt_ctf_trace_get_name(trace);
+       if (trace_name) {
+               int_ret = bt_ctf_trace_set_name(writer_trace, trace_name);
+               if (int_ret < 0) {
+                       fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+       }
+
 end:
        return ret;
 }
This page took 0.029409 seconds and 4 git commands to generate.