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_integer_create(8);
81 ret
= bt_ctf_field_type_integer_set_is_signed(ft
, BT_TRUE
);
83 ret
= bt_ctf_event_class_add_field(cfg
->ec
, ft
, "first");
85 bt_ctf_object_put_ref(ft
);
86 ft
= bt_ctf_field_type_string_create();
88 ret
= bt_ctf_event_class_add_field(cfg
->ec
, ft
, "second");
90 bt_ctf_object_put_ref(ft
);
91 ret
= bt_ctf_stream_class_add_event_class(cfg
->sc
, cfg
->ec
);
93 cfg
->stream
= bt_ctf_writer_create_stream(cfg
->writer
, cfg
->sc
);
94 BT_ASSERT(cfg
->stream
);
98 void write_stream(struct config
*cfg
)
100 struct bt_ctf_event
*ev
;
101 struct bt_ctf_field
*field
;
105 for (i
= 0; i
< 25; i
++) {
106 ev
= bt_ctf_event_create(cfg
->ec
);
108 field
= bt_ctf_event_get_payload(ev
, "first");
110 ret
= bt_ctf_field_integer_signed_set_value(field
, -23 + i
);
112 bt_ctf_object_put_ref(field
);
113 field
= bt_ctf_event_get_payload(ev
, "second");
115 ret
= bt_ctf_field_string_set_value(field
, "saluuuut");
117 bt_ctf_object_put_ref(field
);
118 ret
= bt_ctf_clock_set_time(cfg
->clock
, 3600 + i
* 5000);
120 ret
= bt_ctf_stream_append_event(cfg
->stream
, ev
);
122 bt_ctf_object_put_ref(ev
);
125 ret
= bt_ctf_stream_flush(cfg
->stream
);
129 int main(int argc
, char **argv
)
131 struct config cfg
= {0};
133 BT_ASSERT(argc
>= 2);
134 configure_writer(&cfg
, argv
[1]);