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