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