lib: add internal BT_LIB_LOG*_APPEND_CAUSE() macros
[babeltrace.git] / src / lib / trace-ir / event.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/EVENT"
25 #include "lib/logging.h"
26
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/event-const.h>
29 #include <babeltrace2/trace-ir/event-class.h>
30 #include <babeltrace2/trace-ir/stream-class.h>
31 #include <babeltrace2/trace-ir/stream-class-const.h>
32 #include <babeltrace2/trace-ir/packet.h>
33 #include <babeltrace2/trace-ir/trace.h>
34 #include "common/assert.h"
35 #include "compat/compiler.h"
36 #include <inttypes.h>
37
38 #include "field.h"
39 #include "field-class.h"
40 #include "event.h"
41 #include "stream-class.h"
42 #include "stream.h"
43 #include "packet.h"
44 #include "trace.h"
45 #include "packet.h"
46 #include "attributes.h"
47 #include "event-class.h"
48
49 BT_HIDDEN
50 void _bt_event_set_is_frozen(const struct bt_event *event, bool is_frozen)
51 {
52 BT_ASSERT(event);
53 BT_LIB_LOGD("Setting event's frozen state: %!+e, is-frozen=%d",
54 event, is_frozen);
55
56 if (event->common_context_field) {
57 BT_LOGD_STR("Setting event's common context field's frozen state.");
58 bt_field_set_is_frozen(
59 event->common_context_field, is_frozen);
60 }
61
62 if (event->specific_context_field) {
63 BT_LOGD_STR("Setting event's specific context field's frozen state.");
64 bt_field_set_is_frozen(event->specific_context_field,
65 is_frozen);
66 }
67
68 if (event->payload_field) {
69 BT_LOGD_STR("Setting event's payload field's frozen state.");
70 bt_field_set_is_frozen(event->payload_field,
71 is_frozen);
72 }
73
74 ((struct bt_event *) event)->frozen = is_frozen;
75 BT_LOGD_STR("Setting event's packet's frozen state.");
76 bt_packet_set_is_frozen(event->packet, is_frozen);
77 }
78
79 BT_HIDDEN
80 struct bt_event *bt_event_new(struct bt_event_class *event_class)
81 {
82 struct bt_event *event = NULL;
83 struct bt_stream_class *stream_class;
84 struct bt_field_class *fc;
85
86 BT_ASSERT(event_class);
87 event = g_new0(struct bt_event, 1);
88 if (!event) {
89 BT_LOGE_STR("Failed to allocate one event.");
90 goto error;
91 }
92
93 bt_object_init_unique(&event->base);
94 stream_class = bt_event_class_borrow_stream_class(event_class);
95 BT_ASSERT(stream_class);
96 fc = stream_class->event_common_context_fc;
97 if (fc) {
98 event->common_context_field = bt_field_create(fc);
99 if (!event->common_context_field) {
100 /* bt_field_create() logs errors */
101 goto error;
102 }
103 }
104
105 fc = event_class->specific_context_fc;
106 if (fc) {
107 event->specific_context_field = bt_field_create(fc);
108 if (!event->specific_context_field) {
109 /* bt_field_create() logs errors */
110 goto error;
111 }
112 }
113
114 fc = event_class->payload_fc;
115 if (fc) {
116 event->payload_field = bt_field_create(fc);
117 if (!event->payload_field) {
118 /* bt_field_create() logs errors */
119 goto error;
120 }
121 }
122
123 goto end;
124
125 error:
126 if (event) {
127 bt_event_destroy(event);
128 event = NULL;
129 }
130
131 end:
132 return event;
133 }
134
135 struct bt_event_class *bt_event_borrow_class(struct bt_event *event)
136 {
137 BT_ASSERT_PRE_NON_NULL(event, "Event");
138 return event->class;
139 }
140
141 const struct bt_event_class *bt_event_borrow_class_const(
142 const struct bt_event *event)
143 {
144 return bt_event_borrow_class((void *) event);
145 }
146
147 struct bt_stream *bt_event_borrow_stream(struct bt_event *event)
148 {
149 BT_ASSERT_PRE_NON_NULL(event, "Event");
150 return event->packet ? event->packet->stream : NULL;
151 }
152
153 const struct bt_stream *bt_event_borrow_stream_const(
154 const struct bt_event *event)
155 {
156 return bt_event_borrow_stream((void *) event);
157 }
158
159 struct bt_field *bt_event_borrow_common_context_field(struct bt_event *event)
160 {
161 BT_ASSERT_PRE_NON_NULL(event, "Event");
162 return event->common_context_field;
163 }
164
165 const struct bt_field *bt_event_borrow_common_context_field_const(
166 const struct bt_event *event)
167 {
168 BT_ASSERT_PRE_NON_NULL(event, "Event");
169 return event->common_context_field;
170 }
171
172 struct bt_field *bt_event_borrow_specific_context_field(struct bt_event *event)
173 {
174 BT_ASSERT_PRE_NON_NULL(event, "Event");
175 return event->specific_context_field;
176 }
177
178 const struct bt_field *bt_event_borrow_specific_context_field_const(
179 const struct bt_event *event)
180 {
181 BT_ASSERT_PRE_NON_NULL(event, "Event");
182 return event->specific_context_field;
183 }
184
185 struct bt_field *bt_event_borrow_payload_field(struct bt_event *event)
186 {
187 BT_ASSERT_PRE_NON_NULL(event, "Event");
188 return event->payload_field;
189 }
190
191 const struct bt_field *bt_event_borrow_payload_field_const(
192 const struct bt_event *event)
193 {
194 BT_ASSERT_PRE_NON_NULL(event, "Event");
195 return event->payload_field;
196 }
197
198 BT_HIDDEN
199 void bt_event_destroy(struct bt_event *event)
200 {
201 BT_ASSERT(event);
202 BT_LIB_LOGD("Destroying event: %!+e", event);
203
204 if (event->common_context_field) {
205 BT_LOGD_STR("Destroying event's stream event context field.");
206 bt_field_destroy(event->common_context_field);
207 event->common_context_field = NULL;
208 }
209
210 if (event->specific_context_field) {
211 BT_LOGD_STR("Destroying event's context field.");
212 bt_field_destroy(event->specific_context_field);
213 event->specific_context_field = NULL;
214 }
215
216 if (event->payload_field) {
217 BT_LOGD_STR("Destroying event's payload field.");
218 bt_field_destroy(event->payload_field);
219 event->payload_field = NULL;
220 }
221
222 BT_LOGD_STR("Putting event's class.");
223 bt_object_put_ref(event->class);
224 BT_LOGD_STR("Putting event's packet.");
225 BT_OBJECT_PUT_REF_AND_RESET(event->packet);
226 g_free(event);
227 }
228
229 struct bt_packet *bt_event_borrow_packet(struct bt_event *event)
230 {
231 BT_ASSERT_PRE_NON_NULL(event, "Event");
232 return event->packet;
233 }
234
235 const struct bt_packet *bt_event_borrow_packet_const(
236 const struct bt_event *event)
237 {
238 return bt_event_borrow_packet((void *) event);
239 }
This page took 0.034025 seconds and 4 git commands to generate.