Cleanup: remove private babeltrace.h
[babeltrace.git] / src / ctf-writer / event-class.h
1 #ifndef BABELTRACE_CTF_WRITER_EVENT_CLASS_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_EVENT_CLASS_INTERNAL_H
3
4 /*
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 *
7 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "common/assert.h"
29 #include "common/macros.h"
30 #include <babeltrace2/ctf-writer/event.h>
31 #include <babeltrace2/ctf-writer/field-types.h>
32 #include <babeltrace2/ctf-writer/fields.h>
33 #include <babeltrace2/ctf-writer/stream-class.h>
34 #include <babeltrace2/ctf-writer/stream.h>
35 #include <glib.h>
36
37 #include "object.h"
38 #include "trace.h"
39 #include "values.h"
40
41 struct bt_ctf_event_class_common {
42 struct bt_ctf_object base;
43 struct bt_ctf_field_type_common *context_field_type;
44 struct bt_ctf_field_type_common *payload_field_type;
45 int frozen;
46
47 /*
48 * This flag indicates if the event class is valid. A valid
49 * event class is _always_ frozen. However, an event class
50 * may be frozen, but not valid yet. This is okay, as long as
51 * no events are created out of this event class.
52 */
53 int valid;
54
55 /* Attributes */
56 GString *name;
57 int64_t id;
58 int log_level;
59 GString *emf_uri;
60 };
61
62 BT_HIDDEN
63 void bt_ctf_event_class_common_freeze(struct bt_ctf_event_class_common *event_class);
64
65 BT_HIDDEN
66 void bt_ctf_event_class_common_set_native_byte_order(
67 struct bt_ctf_event_class_common *event_class, int byte_order);
68
69 static inline
70 struct bt_ctf_stream_class_common *bt_ctf_event_class_common_borrow_stream_class(
71 struct bt_ctf_event_class_common *event_class)
72 {
73 BT_ASSERT(event_class);
74 return (void *) bt_ctf_object_borrow_parent(&event_class->base);
75 }
76
77 typedef struct bt_ctf_field_type_common *(*bt_ctf_field_type_structure_create_func)();
78
79 BT_HIDDEN
80 int bt_ctf_event_class_common_initialize(struct bt_ctf_event_class_common *event_class,
81 const char *name, bt_ctf_object_release_func release_func,
82 bt_ctf_field_type_structure_create_func ft_struct_create_func);
83
84 BT_HIDDEN
85 void bt_ctf_event_class_common_finalize(struct bt_ctf_object *obj);
86
87 BT_HIDDEN
88 int bt_ctf_event_class_common_validate_single_clock_class(
89 struct bt_ctf_event_class_common *event_class,
90 struct bt_ctf_clock_class **expected_clock_class);
91
92 static inline
93 const char *bt_ctf_event_class_common_get_name(
94 struct bt_ctf_event_class_common *event_class)
95 {
96 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
97 BT_ASSERT(event_class->name);
98 return event_class->name->str;
99 }
100
101 static inline
102 int64_t bt_ctf_event_class_common_get_id(
103 struct bt_ctf_event_class_common *event_class)
104 {
105 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
106 return event_class->id;
107 }
108
109 static inline
110 int bt_ctf_event_class_common_set_id(
111 struct bt_ctf_event_class_common *event_class, uint64_t id_param)
112 {
113 int ret = 0;
114 int64_t id = (int64_t) id_param;
115
116 if (!event_class) {
117 BT_LOGW_STR("Invalid parameter: event class is NULL.");
118 ret = -1;
119 goto end;
120 }
121
122 if (event_class->frozen) {
123 BT_LOGW("Invalid parameter: event class is frozen: "
124 "addr=%p, name=\"%s\", id=%" PRId64,
125 event_class,
126 bt_ctf_event_class_common_get_name(event_class),
127 bt_ctf_event_class_common_get_id(event_class));
128 ret = -1;
129 goto end;
130 }
131
132 if (id < 0) {
133 BT_LOGW("Invalid parameter: invalid event class's ID: "
134 "addr=%p, name=\"%s\", id=%" PRIu64,
135 event_class,
136 bt_ctf_event_class_common_get_name(event_class),
137 id_param);
138 ret = -1;
139 goto end;
140 }
141
142 event_class->id = id;
143 BT_LOGV("Set event class's ID: "
144 "addr=%p, name=\"%s\", id=%" PRId64,
145 event_class, bt_ctf_event_class_common_get_name(event_class), id);
146
147 end:
148 return ret;
149 }
150
151 static inline
152 int bt_ctf_event_class_common_get_log_level(
153 struct bt_ctf_event_class_common *event_class)
154 {
155 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
156 return event_class->log_level;
157 }
158
159 static inline
160 int bt_ctf_event_class_common_set_log_level(
161 struct bt_ctf_event_class_common *event_class, int log_level)
162 {
163 int ret = 0;
164
165 if (!event_class) {
166 BT_LOGW_STR("Invalid parameter: event class is NULL.");
167 ret = -1;
168 goto end;
169 }
170
171 if (event_class->frozen) {
172 BT_LOGW("Invalid parameter: event class is frozen: "
173 "addr=%p, name=\"%s\", id=%" PRId64,
174 event_class,
175 bt_ctf_event_class_common_get_name(event_class),
176 bt_ctf_event_class_common_get_id(event_class));
177 ret = -1;
178 goto end;
179 }
180
181 switch (log_level) {
182 case BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED:
183 case BT_CTF_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
184 case BT_CTF_EVENT_CLASS_LOG_LEVEL_ALERT:
185 case BT_CTF_EVENT_CLASS_LOG_LEVEL_CRITICAL:
186 case BT_CTF_EVENT_CLASS_LOG_LEVEL_ERROR:
187 case BT_CTF_EVENT_CLASS_LOG_LEVEL_WARNING:
188 case BT_CTF_EVENT_CLASS_LOG_LEVEL_NOTICE:
189 case BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO:
190 case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
191 case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
192 case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
193 case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
194 case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
195 case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
196 case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
197 case BT_CTF_EVENT_CLASS_LOG_LEVEL_DEBUG:
198 break;
199 default:
200 BT_LOGW("Invalid parameter: unknown event class log level: "
201 "addr=%p, name=\"%s\", id=%" PRId64 ", log-level=%d",
202 event_class, bt_ctf_event_class_common_get_name(event_class),
203 bt_ctf_event_class_common_get_id(event_class), log_level);
204 ret = -1;
205 goto end;
206 }
207
208 event_class->log_level = log_level;
209 BT_LOGV("Set event class's log level: "
210 "addr=%p, name=\"%s\", id=%" PRId64 ", log-level=%s",
211 event_class, bt_ctf_event_class_common_get_name(event_class),
212 bt_ctf_event_class_common_get_id(event_class),
213 bt_ctf_event_class_log_level_string(log_level));
214
215 end:
216 return ret;
217 }
218
219 static inline
220 const char *bt_ctf_event_class_common_get_emf_uri(
221 struct bt_ctf_event_class_common *event_class)
222 {
223 const char *emf_uri = NULL;
224
225 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
226
227 if (event_class->emf_uri->len > 0) {
228 emf_uri = event_class->emf_uri->str;
229 }
230
231 return emf_uri;
232 }
233
234 static inline
235 int bt_ctf_event_class_common_set_emf_uri(
236 struct bt_ctf_event_class_common *event_class,
237 const char *emf_uri)
238 {
239 int ret = 0;
240
241 if (!event_class) {
242 BT_LOGW_STR("Invalid parameter: event class is NULL.");
243 ret = -1;
244 goto end;
245 }
246
247 if (emf_uri && strlen(emf_uri) == 0) {
248 BT_LOGW_STR("Invalid parameter: EMF URI is empty.");
249 ret = -1;
250 goto end;
251 }
252
253 if (event_class->frozen) {
254 BT_LOGW("Invalid parameter: event class is frozen: "
255 "addr=%p, name=\"%s\", id=%" PRId64,
256 event_class, bt_ctf_event_class_common_get_name(event_class),
257 bt_ctf_event_class_common_get_id(event_class));
258 ret = -1;
259 goto end;
260 }
261
262 if (emf_uri) {
263 g_string_assign(event_class->emf_uri, emf_uri);
264 BT_LOGV("Set event class's EMF URI: "
265 "addr=%p, name=\"%s\", id=%" PRId64 ", emf-uri=\"%s\"",
266 event_class, bt_ctf_event_class_common_get_name(event_class),
267 bt_ctf_event_class_common_get_id(event_class), emf_uri);
268 } else {
269 g_string_assign(event_class->emf_uri, "");
270 BT_LOGV("Reset event class's EMF URI: "
271 "addr=%p, name=\"%s\", id=%" PRId64,
272 event_class, bt_ctf_event_class_common_get_name(event_class),
273 bt_ctf_event_class_common_get_id(event_class));
274 }
275
276 end:
277 return ret;
278 }
279
280 static inline
281 struct bt_ctf_field_type_common *bt_ctf_event_class_common_borrow_context_field_type(
282 struct bt_ctf_event_class_common *event_class)
283 {
284 struct bt_ctf_field_type_common *context_ft = NULL;
285
286 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
287
288 if (!event_class->context_field_type) {
289 BT_LOGV("Event class has no context field type: "
290 "addr=%p, name=\"%s\", id=%" PRId64,
291 event_class, bt_ctf_event_class_common_get_name(event_class),
292 bt_ctf_event_class_common_get_id(event_class));
293 goto end;
294 }
295
296 context_ft = event_class->context_field_type;
297
298 end:
299 return context_ft;
300 }
301
302 static inline
303 int bt_ctf_event_class_common_set_context_field_type(
304 struct bt_ctf_event_class_common *event_class,
305 struct bt_ctf_field_type_common *context_ft)
306 {
307 int ret = 0;
308
309 if (!event_class) {
310 BT_LOGW_STR("Invalid parameter: event class is NULL.");
311 ret = -1;
312 goto end;
313 }
314
315 if (event_class->frozen) {
316 BT_LOGW("Invalid parameter: event class is frozen: "
317 "addr=%p, name=\"%s\", id=%" PRId64,
318 event_class, bt_ctf_event_class_common_get_name(event_class),
319 bt_ctf_event_class_common_get_id(event_class));
320 ret = -1;
321 goto end;
322 }
323
324 if (context_ft && bt_ctf_field_type_common_get_type_id(context_ft) !=
325 BT_CTF_FIELD_TYPE_ID_STRUCT) {
326 BT_LOGW("Invalid parameter: event class's context field type must be a structure: "
327 "addr=%p, name=\"%s\", id=%" PRId64 ", "
328 "context-ft-id=%s",
329 event_class, bt_ctf_event_class_common_get_name(event_class),
330 bt_ctf_event_class_common_get_id(event_class),
331 bt_ctf_field_type_id_string(
332 bt_ctf_field_type_common_get_type_id(context_ft)));
333 ret = -1;
334 goto end;
335 }
336
337 bt_ctf_object_put_ref(event_class->context_field_type);
338 event_class->context_field_type = context_ft;
339 bt_ctf_object_get_ref(event_class->context_field_type);
340 BT_LOGV("Set event class's context field type: "
341 "event-class-addr=%p, event-class-name=\"%s\", "
342 "event-class-id=%" PRId64 ", context-ft-addr=%p",
343 event_class, bt_ctf_event_class_common_get_name(event_class),
344 bt_ctf_event_class_common_get_id(event_class), context_ft);
345
346 end:
347 return ret;
348 }
349
350 static inline
351 struct bt_ctf_field_type_common *bt_ctf_event_class_common_borrow_payload_field_type(
352 struct bt_ctf_event_class_common *event_class)
353 {
354 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
355 return event_class->payload_field_type;
356 }
357
358 static inline
359 int bt_ctf_event_class_common_set_payload_field_type(
360 struct bt_ctf_event_class_common *event_class,
361 struct bt_ctf_field_type_common *payload_ft)
362 {
363 int ret = 0;
364
365 if (!event_class) {
366 BT_LOGW_STR("Invalid parameter: event class is NULL.");
367 ret = -1;
368 goto end;
369 }
370
371 if (payload_ft && bt_ctf_field_type_common_get_type_id(payload_ft) !=
372 BT_CTF_FIELD_TYPE_ID_STRUCT) {
373 BT_LOGW("Invalid parameter: event class's payload field type must be a structure: "
374 "addr=%p, name=\"%s\", id=%" PRId64 ", "
375 "payload-ft-addr=%p, payload-ft-id=%s",
376 event_class, bt_ctf_event_class_common_get_name(event_class),
377 bt_ctf_event_class_common_get_id(event_class), payload_ft,
378 bt_ctf_field_type_id_string(
379 bt_ctf_field_type_common_get_type_id(payload_ft)));
380 ret = -1;
381 goto end;
382 }
383
384 bt_ctf_object_put_ref(event_class->payload_field_type);
385 event_class->payload_field_type = payload_ft;
386 bt_ctf_object_get_ref(event_class->payload_field_type);
387 BT_LOGV("Set event class's payload field type: "
388 "event-class-addr=%p, event-class-name=\"%s\", "
389 "event-class-id=%" PRId64 ", payload-ft-addr=%p",
390 event_class, bt_ctf_event_class_common_get_name(event_class),
391 bt_ctf_event_class_common_get_id(event_class), payload_ft);
392 end:
393 return ret;
394 }
395
396 #endif /* BABELTRACE_CTF_WRITER_EVENT_CLASS_INTERNAL_H */
This page took 0.039134 seconds and 4 git commands to generate.