2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include <babeltrace2-ctf-writer/writer.h>
25 #include <babeltrace2-ctf-writer/clock.h>
26 #include <babeltrace2-ctf-writer/clock-class.h>
27 #include <babeltrace2-ctf-writer/stream.h>
28 #include <babeltrace2-ctf-writer/event.h>
29 #include <babeltrace2-ctf-writer/event-types.h>
30 #include <babeltrace2-ctf-writer/event-fields.h>
31 #include <babeltrace2-ctf-writer/stream-class.h>
32 #include <babeltrace2-ctf-writer/trace.h>
34 #include "common/assert.h"
37 struct bt_ctf_writer
*writer
;
38 struct bt_ctf_trace
*trace
;
39 struct bt_ctf_clock
*clock
;
40 struct bt_ctf_stream_class
*sc
;
41 struct bt_ctf_stream
*stream
;
42 struct bt_ctf_event_class
*ec
;
46 void fini_config(struct config
*cfg
)
48 bt_ctf_object_put_ref(cfg
->stream
);
49 bt_ctf_object_put_ref(cfg
->sc
);
50 bt_ctf_object_put_ref(cfg
->ec
);
51 bt_ctf_object_put_ref(cfg
->clock
);
52 bt_ctf_object_put_ref(cfg
->trace
);
53 bt_ctf_object_put_ref(cfg
->writer
);
57 void configure_writer(struct config
*cfg
, const char *path
)
59 struct bt_ctf_field_type
*ft
;
62 cfg
->writer
= bt_ctf_writer_create(path
);
63 BT_ASSERT(cfg
->writer
);
64 cfg
->trace
= bt_ctf_writer_get_trace(cfg
->writer
);
65 BT_ASSERT(cfg
->trace
);
66 cfg
->clock
= bt_ctf_clock_create("default");
67 BT_ASSERT(cfg
->clock
);
68 ret
= bt_ctf_writer_add_clock(cfg
->writer
, cfg
->clock
);
70 ret
= bt_ctf_writer_set_byte_order(cfg
->writer
,
71 BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
73 cfg
->sc
= bt_ctf_stream_class_create("hello");
75 ret
= bt_ctf_stream_class_set_clock(cfg
->sc
, cfg
->clock
);
77 cfg
->ec
= bt_ctf_event_class_create("ev");
79 ft
= bt_ctf_field_type_floating_point_create();
81 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(ft
, 8);
83 ret
= bt_ctf_field_type_floating_point_set_mantissa_digits(ft
, 24);
85 ret
= bt_ctf_event_class_add_field(cfg
->ec
, ft
, "flt");
87 bt_ctf_object_put_ref(ft
);
88 ret
= bt_ctf_stream_class_add_event_class(cfg
->sc
, cfg
->ec
);
90 cfg
->stream
= bt_ctf_writer_create_stream(cfg
->writer
, cfg
->sc
);
91 BT_ASSERT(cfg
->stream
);
95 void write_stream(struct config
*cfg
)
97 struct bt_ctf_event
*ev
;
98 struct bt_ctf_field
*field
;
101 /* Create event and fill fields */
102 ev
= bt_ctf_event_create(cfg
->ec
);
104 field
= bt_ctf_event_get_payload(ev
, "flt");
106 ret
= bt_ctf_field_floating_point_set_value(field
, -28.4751);
108 bt_ctf_object_put_ref(field
);
109 ret
= bt_ctf_clock_set_time(cfg
->clock
, 0);
113 ret
= bt_ctf_stream_append_event(cfg
->stream
, ev
);
115 bt_ctf_object_put_ref(ev
);
118 ret
= bt_ctf_stream_flush(cfg
->stream
);
122 int main(int argc
, char **argv
)
124 struct config cfg
= {0};
126 BT_ASSERT(argc
>= 2);
127 configure_writer(&cfg
, argv
[1]);