| 1 | /* |
| 2 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 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: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 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 |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdint.h> |
| 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> |
| 33 | |
| 34 | #include "common/assert.h" |
| 35 | |
| 36 | struct config { |
| 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; |
| 43 | }; |
| 44 | |
| 45 | static |
| 46 | void fini_config(struct config *cfg) |
| 47 | { |
| 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); |
| 54 | } |
| 55 | |
| 56 | static |
| 57 | void configure_writer(struct config *cfg, const char *path) |
| 58 | { |
| 59 | struct bt_ctf_field_type *ft; |
| 60 | int ret; |
| 61 | |
| 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); |
| 69 | BT_ASSERT(ret == 0); |
| 70 | ret = bt_ctf_writer_set_byte_order(cfg->writer, |
| 71 | BT_CTF_BYTE_ORDER_BIG_ENDIAN); |
| 72 | BT_ASSERT(ret == 0); |
| 73 | cfg->sc = bt_ctf_stream_class_create("hello"); |
| 74 | BT_ASSERT(cfg->sc); |
| 75 | ret = bt_ctf_stream_class_set_clock(cfg->sc, cfg->clock); |
| 76 | BT_ASSERT(ret == 0); |
| 77 | cfg->ec = bt_ctf_event_class_create("ev"); |
| 78 | BT_ASSERT(cfg->ec); |
| 79 | ft = bt_ctf_field_type_floating_point_create(); |
| 80 | BT_ASSERT(ft); |
| 81 | ret = bt_ctf_field_type_floating_point_set_exponent_digits(ft, 11); |
| 82 | BT_ASSERT(ret == 0); |
| 83 | ret = bt_ctf_field_type_floating_point_set_mantissa_digits(ft, 53); |
| 84 | BT_ASSERT(ret == 0); |
| 85 | ret = bt_ctf_event_class_add_field(cfg->ec, ft, "dbl"); |
| 86 | BT_ASSERT(ret == 0); |
| 87 | bt_ctf_object_put_ref(ft); |
| 88 | ret = bt_ctf_stream_class_add_event_class(cfg->sc, cfg->ec); |
| 89 | BT_ASSERT(ret == 0); |
| 90 | cfg->stream = bt_ctf_writer_create_stream(cfg->writer, cfg->sc); |
| 91 | BT_ASSERT(cfg->stream); |
| 92 | } |
| 93 | |
| 94 | static |
| 95 | void write_stream(struct config *cfg) |
| 96 | { |
| 97 | struct bt_ctf_event *ev; |
| 98 | struct bt_ctf_field *field; |
| 99 | int ret; |
| 100 | |
| 101 | /* Create event and fill fields */ |
| 102 | ev = bt_ctf_event_create(cfg->ec); |
| 103 | BT_ASSERT(ev); |
| 104 | field = bt_ctf_event_get_payload(ev, "dbl"); |
| 105 | BT_ASSERT(field); |
| 106 | ret = bt_ctf_field_floating_point_set_value(field, 17283.3881); |
| 107 | BT_ASSERT(ret == 0); |
| 108 | bt_ctf_object_put_ref(field); |
| 109 | ret = bt_ctf_clock_set_time(cfg->clock, 0); |
| 110 | BT_ASSERT(ret == 0); |
| 111 | |
| 112 | /* Append event */ |
| 113 | ret = bt_ctf_stream_append_event(cfg->stream, ev); |
| 114 | BT_ASSERT(ret == 0); |
| 115 | bt_ctf_object_put_ref(ev); |
| 116 | |
| 117 | /* Create packet */ |
| 118 | ret = bt_ctf_stream_flush(cfg->stream); |
| 119 | BT_ASSERT(ret == 0); |
| 120 | } |
| 121 | |
| 122 | int main(int argc, char **argv) |
| 123 | { |
| 124 | struct config cfg = {0}; |
| 125 | |
| 126 | BT_ASSERT(argc >= 2); |
| 127 | configure_writer(&cfg, argv[1]); |
| 128 | write_stream(&cfg); |
| 129 | fini_config(&cfg); |
| 130 | return 0; |
| 131 | } |