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