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