ctf plugin: notif iter: use "borrow" functions for metadata where possible
[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>
272df73e
PP
41#include <glib.h>
42
3dca2276 43struct bt_event_class_common {
272df73e 44 struct bt_object base;
3dca2276
PP
45 struct bt_field_type_common *context_field_type;
46 struct bt_field_type_common *payload_field_type;
272df73e
PP
47 int frozen;
48
49 /*
50 * This flag indicates if the event class is valid. A valid
51 * event class is _always_ frozen. However, an event class
52 * may be frozen, but not valid yet. This is okay, as long as
53 * no events are created out of this event class.
54 */
55 int valid;
5990dd44 56
cf76ce92
PP
57 /* Attributes */
58 GString *name;
5990dd44 59 int64_t id;
3dca2276 60 int log_level;
cf76ce92 61 GString *emf_uri;
272df73e
PP
62};
63
3dca2276
PP
64struct bt_event_class {
65 struct bt_event_class_common common;
66};
67
272df73e 68BT_HIDDEN
50842bdc 69void bt_event_class_freeze(struct bt_event_class *event_class);
272df73e
PP
70
71BT_HIDDEN
3dca2276 72void bt_event_class_common_freeze(struct bt_event_class_common *event_class);
272df73e
PP
73
74BT_HIDDEN
3dca2276
PP
75void bt_event_class_common_set_native_byte_order(
76 struct bt_event_class_common *event_class, int byte_order);
272df73e 77
dc3fffef 78static inline
3dca2276
PP
79struct bt_stream_class_common *bt_event_class_common_borrow_stream_class(
80 struct bt_event_class_common *event_class)
dc3fffef 81{
f6ccaed9 82 BT_ASSERT(event_class);
dc3fffef
PP
83 return (void *) bt_object_borrow_parent(event_class);
84}
85
3dca2276
PP
86typedef struct bt_field_type_common *(*bt_field_type_structure_create_func)();
87
88BT_HIDDEN
89int bt_event_class_common_initialize(struct bt_event_class_common *event_class,
90 const char *name, bt_object_release_func release_func,
91 bt_field_type_structure_create_func ft_struct_create_func);
92
93BT_HIDDEN
94void bt_event_class_common_finalize(struct bt_object *obj);
95
96BT_HIDDEN
97int bt_event_class_common_validate_single_clock_class(
98 struct bt_event_class_common *event_class,
99 struct bt_clock_class **expected_clock_class);
100
101static inline
102const char *bt_event_class_common_get_name(
103 struct bt_event_class_common *event_class)
104{
105 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
106 BT_ASSERT(event_class->name);
107 return event_class->name->str;
108}
109
110static inline
111int64_t bt_event_class_common_get_id(
112 struct bt_event_class_common *event_class)
113{
114 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
115 return event_class->id;
116}
117
118static inline
119int bt_event_class_common_set_id(
120 struct bt_event_class_common *event_class, uint64_t id_param)
cf76ce92 121{
3dca2276
PP
122 int ret = 0;
123 int64_t id = (int64_t) id_param;
124
125 if (!event_class) {
126 BT_LOGW_STR("Invalid parameter: event class is NULL.");
127 ret = -1;
128 goto end;
129 }
130
131 if (event_class->frozen) {
132 BT_LOGW("Invalid parameter: event class is frozen: "
133 "addr=%p, name=\"%s\", id=%" PRId64,
134 event_class,
135 bt_event_class_common_get_name(event_class),
136 bt_event_class_common_get_id(event_class));
137 ret = -1;
138 goto end;
139 }
140
141 if (id < 0) {
142 BT_LOGW("Invalid parameter: invalid event class's ID: "
143 "addr=%p, name=\"%s\", id=%" PRIu64,
144 event_class,
145 bt_event_class_common_get_name(event_class),
146 id_param);
147 ret = -1;
148 goto end;
149 }
150
151 event_class->id = id;
152 BT_LOGV("Set event class's ID: "
153 "addr=%p, name=\"%s\", id=%" PRId64,
154 event_class, bt_event_class_common_get_name(event_class), id);
155
156end:
157 return ret;
158}
159
160static inline
161int bt_event_class_common_get_log_level(
162 struct bt_event_class_common *event_class)
163{
164 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
165 return event_class->log_level;
166}
167
168static inline
169int bt_event_class_common_set_log_level(
170 struct bt_event_class_common *event_class, int log_level)
171{
172 int ret = 0;
173
174 if (!event_class) {
175 BT_LOGW_STR("Invalid parameter: event class is NULL.");
176 ret = -1;
177 goto end;
178 }
179
180 if (event_class->frozen) {
181 BT_LOGW("Invalid parameter: event class is frozen: "
182 "addr=%p, name=\"%s\", id=%" PRId64,
183 event_class,
184 bt_event_class_common_get_name(event_class),
185 bt_event_class_common_get_id(event_class));
186 ret = -1;
187 goto end;
188 }
189
190 switch (log_level) {
50842bdc 191 case BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED:
50842bdc 192 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
50842bdc 193 case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
50842bdc 194 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
50842bdc 195 case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
50842bdc 196 case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
50842bdc 197 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
50842bdc 198 case BT_EVENT_CLASS_LOG_LEVEL_INFO:
50842bdc 199 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
50842bdc 200 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
50842bdc 201 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
50842bdc 202 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
50842bdc 203 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
50842bdc 204 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
50842bdc 205 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
50842bdc 206 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
3dca2276 207 break;
cf76ce92 208 default:
3dca2276
PP
209 BT_LOGW("Invalid parameter: unknown event class log level: "
210 "addr=%p, name=\"%s\", id=%" PRId64 ", log-level=%d",
211 event_class, bt_event_class_common_get_name(event_class),
212 bt_event_class_common_get_id(event_class), log_level);
213 ret = -1;
214 goto end;
cf76ce92 215 }
cf76ce92 216
3dca2276
PP
217 event_class->log_level = log_level;
218 BT_LOGV("Set event class's log level: "
219 "addr=%p, name=\"%s\", id=%" PRId64 ", log-level=%s",
220 event_class, bt_event_class_common_get_name(event_class),
221 bt_event_class_common_get_id(event_class),
222 bt_common_event_class_log_level_string(log_level));
223
224end:
225 return ret;
226}
227
228static inline
229const char *bt_event_class_common_get_emf_uri(
230 struct bt_event_class_common *event_class)
231{
232 const char *emf_uri = NULL;
233
234 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
235
236 if (event_class->emf_uri->len > 0) {
237 emf_uri = event_class->emf_uri->str;
238 }
239
240 return emf_uri;
241}
242
243static inline
244int bt_event_class_common_set_emf_uri(
245 struct bt_event_class_common *event_class,
246 const char *emf_uri)
247{
248 int ret = 0;
249
250 if (!event_class) {
251 BT_LOGW_STR("Invalid parameter: event class is NULL.");
252 ret = -1;
253 goto end;
254 }
255
256 if (emf_uri && strlen(emf_uri) == 0) {
257 BT_LOGW_STR("Invalid parameter: EMF URI is empty.");
258 ret = -1;
259 goto end;
260 }
261
262 if (event_class->frozen) {
263 BT_LOGW("Invalid parameter: event class is frozen: "
264 "addr=%p, name=\"%s\", id=%" PRId64,
265 event_class, bt_event_class_common_get_name(event_class),
266 bt_event_class_common_get_id(event_class));
267 ret = -1;
268 goto end;
269 }
270
271 if (emf_uri) {
272 g_string_assign(event_class->emf_uri, emf_uri);
273 BT_LOGV("Set event class's EMF URI: "
274 "addr=%p, name=\"%s\", id=%" PRId64 ", emf-uri=\"%s\"",
275 event_class, bt_event_class_common_get_name(event_class),
276 bt_event_class_common_get_id(event_class), emf_uri);
277 } else {
278 g_string_assign(event_class->emf_uri, "");
279 BT_LOGV("Reset event class's EMF URI: "
280 "addr=%p, name=\"%s\", id=%" PRId64,
281 event_class, bt_event_class_common_get_name(event_class),
282 bt_event_class_common_get_id(event_class));
283 }
284
285end:
286 return ret;
287}
288
289static inline
094ff7c0 290struct bt_field_type_common *bt_event_class_common_borrow_context_field_type(
3dca2276
PP
291 struct bt_event_class_common *event_class)
292{
293 struct bt_field_type_common *context_ft = NULL;
294
295 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
296
297 if (!event_class->context_field_type) {
298 BT_LOGV("Event class has no context field type: "
299 "addr=%p, name=\"%s\", id=%" PRId64,
300 event_class, bt_event_class_common_get_name(event_class),
301 bt_event_class_common_get_id(event_class));
302 goto end;
303 }
304
094ff7c0 305 context_ft = event_class->context_field_type;
3dca2276
PP
306
307end:
308 return context_ft;
309}
310
311static inline
312int bt_event_class_common_set_context_field_type(
313 struct bt_event_class_common *event_class,
314 struct bt_field_type_common *context_ft)
315{
316 int ret = 0;
317
318 if (!event_class) {
319 BT_LOGW_STR("Invalid parameter: event class is NULL.");
320 ret = -1;
321 goto end;
322 }
323
324 if (event_class->frozen) {
325 BT_LOGW("Invalid parameter: event class is frozen: "
326 "addr=%p, name=\"%s\", id=%" PRId64,
327 event_class, bt_event_class_common_get_name(event_class),
328 bt_event_class_common_get_id(event_class));
329 ret = -1;
330 goto end;
331 }
332
333 if (context_ft && bt_field_type_common_get_type_id(context_ft) !=
334 BT_FIELD_TYPE_ID_STRUCT) {
335 BT_LOGW("Invalid parameter: event class's context field type must be a structure: "
336 "addr=%p, name=\"%s\", id=%" PRId64 ", "
337 "context-ft-id=%s",
338 event_class, bt_event_class_common_get_name(event_class),
339 bt_event_class_common_get_id(event_class),
340 bt_common_field_type_id_string(
341 bt_field_type_common_get_type_id(context_ft)));
342 ret = -1;
343 goto end;
344 }
345
346 bt_put(event_class->context_field_type);
347 event_class->context_field_type = bt_get(context_ft);
348 BT_LOGV("Set event class's context field type: "
349 "event-class-addr=%p, event-class-name=\"%s\", "
350 "event-class-id=%" PRId64 ", context-ft-addr=%p",
351 event_class, bt_event_class_common_get_name(event_class),
352 bt_event_class_common_get_id(event_class), context_ft);
353
354end:
355 return ret;
356}
357
358static inline
094ff7c0 359struct bt_field_type_common *bt_event_class_common_borrow_payload_field_type(
3dca2276
PP
360 struct bt_event_class_common *event_class)
361{
362 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
094ff7c0 363 return event_class->payload_field_type;
3dca2276
PP
364}
365
366static inline
367int bt_event_class_common_set_payload_field_type(
368 struct bt_event_class_common *event_class,
369 struct bt_field_type_common *payload_ft)
370{
371 int ret = 0;
372
373 if (!event_class) {
374 BT_LOGW_STR("Invalid parameter: event class is NULL.");
375 ret = -1;
376 goto end;
377 }
378
379 if (payload_ft && bt_field_type_common_get_type_id(payload_ft) !=
380 BT_FIELD_TYPE_ID_STRUCT) {
381 BT_LOGW("Invalid parameter: event class's payload field type must be a structure: "
382 "addr=%p, name=\"%s\", id=%" PRId64 ", "
383 "payload-ft-addr=%p, payload-ft-id=%s",
384 event_class, bt_event_class_common_get_name(event_class),
385 bt_event_class_common_get_id(event_class), payload_ft,
386 bt_common_field_type_id_string(
387 bt_field_type_common_get_type_id(payload_ft)));
388 ret = -1;
389 goto end;
390 }
391
392 bt_put(event_class->payload_field_type);
393 event_class->payload_field_type = bt_get(payload_ft);
394 BT_LOGV("Set event class's payload field type: "
395 "event-class-addr=%p, event-class-name=\"%s\", "
396 "event-class-id=%" PRId64 ", payload-ft-addr=%p",
397 event_class, bt_event_class_common_get_name(event_class),
398 bt_event_class_common_get_id(event_class), payload_ft);
399end:
400 return ret;
401}
2a3ced3c 402
272df73e 403#endif /* BABELTRACE_CTF_IR_EVENT_CLASS_INTERNAL_H */
This page took 0.05576 seconds and 4 git commands to generate.