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