ca7ec670be742a83ed8e375739cdaf071df4ce85
[babeltrace.git] / src / ctf-writer / event-class.h
1 #ifndef BABELTRACE_CTF_WRITER_EVENT_CLASS_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_EVENT_CLASS_INTERNAL_H
3
4 /*
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 *
7 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "common/assert.h"
29 #include "common/macros.h"
30 #include <babeltrace2-ctf-writer/event.h>
31 #include <babeltrace2-ctf-writer/field-types.h>
32 #include <babeltrace2-ctf-writer/fields.h>
33 #include <babeltrace2-ctf-writer/stream-class.h>
34 #include <babeltrace2-ctf-writer/stream.h>
35 #include <glib.h>
36
37 #include "object.h"
38 #include "trace.h"
39 #include "values.h"
40
41 struct bt_ctf_event_class_common {
42 struct bt_ctf_object base;
43 struct bt_ctf_field_type_common *context_field_type;
44 struct bt_ctf_field_type_common *payload_field_type;
45 int frozen;
46
47 /*
48 * This flag indicates if the event class is valid. A valid
49 * event class is _always_ frozen. However, an event class
50 * may be frozen, but not valid yet. This is okay, as long as
51 * no events are created out of this event class.
52 */
53 int valid;
54
55 /* Attributes */
56 GString *name;
57 int64_t id;
58 int log_level;
59 GString *emf_uri;
60 };
61
62 BT_HIDDEN
63 void bt_ctf_event_class_common_freeze(struct bt_ctf_event_class_common *event_class);
64
65 BT_HIDDEN
66 void bt_ctf_event_class_common_set_native_byte_order(
67 struct bt_ctf_event_class_common *event_class, int byte_order);
68
69 static inline
70 struct bt_ctf_stream_class_common *bt_ctf_event_class_common_borrow_stream_class(
71 struct bt_ctf_event_class_common *event_class)
72 {
73 BT_ASSERT_DBG(event_class);
74 return (void *) bt_ctf_object_borrow_parent(&event_class->base);
75 }
76
77 typedef struct bt_ctf_field_type_common *
78 (*bt_ctf_field_type_structure_create_func)(void);
79
80 BT_HIDDEN
81 int bt_ctf_event_class_common_initialize(struct bt_ctf_event_class_common *event_class,
82 const char *name, bt_ctf_object_release_func release_func,
83 bt_ctf_field_type_structure_create_func ft_struct_create_func);
84
85 BT_HIDDEN
86 void bt_ctf_event_class_common_finalize(struct bt_ctf_object *obj);
87
88 BT_HIDDEN
89 int 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
93 static inline
94 const char *bt_ctf_event_class_common_get_name(
95 struct bt_ctf_event_class_common *event_class)
96 {
97 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
98 BT_ASSERT_DBG(event_class->name);
99 return event_class->name->str;
100 }
101
102 static inline
103 int64_t bt_ctf_event_class_common_get_id(
104 struct bt_ctf_event_class_common *event_class)
105 {
106 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
107 return event_class->id;
108 }
109
110 static inline
111 int 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_LOGT("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
148 end:
149 return ret;
150 }
151
152 static inline
153 int bt_ctf_event_class_common_get_log_level(
154 struct bt_ctf_event_class_common *event_class)
155 {
156 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
157 return event_class->log_level;
158 }
159
160 static inline
161 int 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_LOGT("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
216 end:
217 return ret;
218 }
219
220 static inline
221 const 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_CTF_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
235 static inline
236 int 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_LOGT("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_LOGT("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
277 end:
278 return ret;
279 }
280
281 static inline
282 struct 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_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
288
289 if (!event_class->context_field_type) {
290 BT_LOGT("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
299 end:
300 return context_ft;
301 }
302
303 static inline
304 int 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_ctf_object_put_ref(event_class->context_field_type);
339 event_class->context_field_type = context_ft;
340 bt_ctf_object_get_ref(event_class->context_field_type);
341 BT_LOGT("Set event class's context field type: "
342 "event-class-addr=%p, event-class-name=\"%s\", "
343 "event-class-id=%" PRId64 ", context-ft-addr=%p",
344 event_class, bt_ctf_event_class_common_get_name(event_class),
345 bt_ctf_event_class_common_get_id(event_class), context_ft);
346
347 end:
348 return ret;
349 }
350
351 static inline
352 struct bt_ctf_field_type_common *bt_ctf_event_class_common_borrow_payload_field_type(
353 struct bt_ctf_event_class_common *event_class)
354 {
355 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
356 return event_class->payload_field_type;
357 }
358
359 static inline
360 int bt_ctf_event_class_common_set_payload_field_type(
361 struct bt_ctf_event_class_common *event_class,
362 struct bt_ctf_field_type_common *payload_ft)
363 {
364 int ret = 0;
365
366 if (!event_class) {
367 BT_LOGW_STR("Invalid parameter: event class is NULL.");
368 ret = -1;
369 goto end;
370 }
371
372 if (payload_ft && bt_ctf_field_type_common_get_type_id(payload_ft) !=
373 BT_CTF_FIELD_TYPE_ID_STRUCT) {
374 BT_LOGW("Invalid parameter: event class's payload field type must be a structure: "
375 "addr=%p, name=\"%s\", id=%" PRId64 ", "
376 "payload-ft-addr=%p, payload-ft-id=%s",
377 event_class, bt_ctf_event_class_common_get_name(event_class),
378 bt_ctf_event_class_common_get_id(event_class), payload_ft,
379 bt_ctf_field_type_id_string(
380 bt_ctf_field_type_common_get_type_id(payload_ft)));
381 ret = -1;
382 goto end;
383 }
384
385 bt_ctf_object_put_ref(event_class->payload_field_type);
386 event_class->payload_field_type = payload_ft;
387 bt_ctf_object_get_ref(event_class->payload_field_type);
388 BT_LOGT("Set event class's payload field type: "
389 "event-class-addr=%p, event-class-name=\"%s\", "
390 "event-class-id=%" PRId64 ", payload-ft-addr=%p",
391 event_class, bt_ctf_event_class_common_get_name(event_class),
392 bt_ctf_event_class_common_get_id(event_class), payload_ft);
393 end:
394 return ret;
395 }
396
397 #endif /* BABELTRACE_CTF_WRITER_EVENT_CLASS_INTERNAL_H */
This page took 0.038676 seconds and 3 git commands to generate.