Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / plugins / text / details / details.c
1 /*
2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_COMP_LOG_SELF_COMP (details_comp->self_comp)
24 #define BT_LOG_OUTPUT_LEVEL (details_comp->log_level)
25 #define BT_LOG_TAG "PLUGIN/SINK.TEXT.DETAILS"
26 #include "logging/comp-logging.h"
27
28 #include <stdbool.h>
29
30 #include <babeltrace2/babeltrace.h>
31
32 #include "common/common.h"
33 #include "common/assert.h"
34 #include "details.h"
35 #include "write.h"
36 #include "plugins/common/param-validation/param-validation.h"
37
38 #define LOG_WRONG_PARAM_TYPE(_name, _value, _exp_type) \
39 do { \
40 BT_COMP_LOGE("Wrong `%s` parameter type: type=%s, " \
41 "expected-type=%s", \
42 (_name), bt_common_value_type_string( \
43 bt_value_get_type(_value)), \
44 bt_common_value_type_string(_exp_type)); \
45 } while (0)
46
47 #define IN_PORT_NAME "in"
48 #define COLOR_PARAM_NAME "color"
49 #define WITH_METADATA_PARAM_NAME "with-metadata"
50 #define WITH_DATA_PARAM_NAME "with-data"
51 #define WITH_TIME_PARAM_NAME "with-time"
52 #define WITH_TRACE_NAME_PARAM_NAME "with-trace-name"
53 #define WITH_STREAM_CLASS_NAME_PARAM_NAME "with-stream-class-name"
54 #define WITH_STREAM_NAME_PARAM_NAME "with-stream-name"
55 #define WITH_UUID_PARAM_NAME "with-uuid"
56 #define COMPACT_PARAM_NAME "compact"
57
58 BT_HIDDEN
59 void details_destroy_details_trace_class_meta(
60 struct details_trace_class_meta *details_tc_meta)
61 {
62 if (!details_tc_meta) {
63 goto end;
64 }
65
66 if (details_tc_meta->objects) {
67 g_hash_table_destroy(details_tc_meta->objects);
68 details_tc_meta->objects = NULL;
69 }
70
71 g_free(details_tc_meta);
72
73 end:
74 return;
75 }
76
77 BT_HIDDEN
78 struct details_trace_class_meta *details_create_details_trace_class_meta(void)
79 {
80 struct details_trace_class_meta *details_tc_meta =
81 g_new0(struct details_trace_class_meta, 1);
82
83 if (!details_tc_meta) {
84 goto end;
85 }
86
87 details_tc_meta->objects = g_hash_table_new(
88 g_direct_hash, g_direct_equal);
89 if (!details_tc_meta->objects) {
90 details_destroy_details_trace_class_meta(details_tc_meta);
91 details_tc_meta = NULL;
92 goto end;
93 }
94
95 details_tc_meta->tc_destruction_listener_id = UINT64_C(-1);
96
97 end:
98 return details_tc_meta;
99 }
100
101 static
102 void destroy_details_comp(struct details_comp *details_comp)
103 {
104 GHashTableIter iter;
105 gpointer key, value;
106
107 if (!details_comp) {
108 goto end;
109 }
110
111 if (details_comp->meta) {
112 /*
113 * Remove trace class destruction listeners, because
114 * otherwise, when they are called, `details_comp`
115 * (their user data) won't exist anymore (we're
116 * destroying it here).
117 */
118 g_hash_table_iter_init(&iter, details_comp->meta);
119
120 while (g_hash_table_iter_next(&iter, &key, &value)) {
121 struct details_trace_class_meta *details_tc_meta =
122 value;
123
124 if (details_tc_meta->tc_destruction_listener_id !=
125 UINT64_C(-1)) {
126 if (bt_trace_class_remove_destruction_listener(
127 (const void *) key,
128 details_tc_meta->tc_destruction_listener_id)) {
129 bt_current_thread_clear_error();
130 }
131 }
132 }
133
134 g_hash_table_destroy(details_comp->meta);
135 details_comp->meta = NULL;
136 }
137
138 if (details_comp->traces) {
139 /*
140 * Remove trace destruction listeners, because
141 * otherwise, when they are called, `details_comp` won't
142 * exist anymore (we're destroying it here).
143 */
144 g_hash_table_iter_init(&iter, details_comp->traces);
145
146 while (g_hash_table_iter_next(&iter, &key, &value)) {
147 struct details_trace *details_trace = value;
148
149 if (bt_trace_remove_destruction_listener(
150 (const void *) key,
151 details_trace->trace_destruction_listener_id)) {
152 bt_current_thread_clear_error();
153 }
154 }
155
156 g_hash_table_destroy(details_comp->traces);
157 details_comp->traces = NULL;
158 }
159
160 if (details_comp->str) {
161 g_string_free(details_comp->str, TRUE);
162 details_comp->str = NULL;
163 }
164
165 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
166 details_comp->msg_iter);
167 g_free(details_comp);
168
169 end:
170 return;
171 }
172
173 static
174 struct details_comp *create_details_comp(
175 bt_self_component_sink *self_comp_sink)
176 {
177 struct details_comp *details_comp = g_new0(struct details_comp, 1);
178 bt_self_component *self_comp =
179 bt_self_component_sink_as_self_component(self_comp_sink);
180
181 if (!details_comp) {
182 goto error;
183 }
184
185 details_comp->log_level = bt_component_get_logging_level(
186 bt_self_component_as_component(self_comp));
187 details_comp->self_comp = self_comp;
188 details_comp->meta = g_hash_table_new_full(g_direct_hash,
189 g_direct_equal, NULL,
190 (GDestroyNotify) details_destroy_details_trace_class_meta);
191 if (!details_comp->meta) {
192 goto error;
193 }
194
195 details_comp->traces = g_hash_table_new_full(g_direct_hash,
196 g_direct_equal, NULL, g_free);
197 if (!details_comp->traces) {
198 goto error;
199 }
200
201 details_comp->str = g_string_new(NULL);
202 if (!details_comp->str) {
203 goto error;
204 }
205
206 goto end;
207
208 error:
209 destroy_details_comp(details_comp);
210 details_comp = NULL;
211
212 end:
213 return details_comp;
214 }
215
216 BT_HIDDEN
217 void details_finalize(bt_self_component_sink *comp)
218 {
219 struct details_comp *details_comp;
220
221 BT_ASSERT(comp);
222 details_comp = bt_self_component_get_data(
223 bt_self_component_sink_as_self_component(comp));
224 BT_ASSERT(details_comp);
225 destroy_details_comp(details_comp);
226 }
227
228 static
229 void configure_bool_opt(struct details_comp *details_comp,
230 const bt_value *params, const char *param_name,
231 bool default_value, bool *opt_value)
232 {
233 const bt_value *value;
234
235 *opt_value = default_value;
236 value = bt_value_map_borrow_entry_value_const(params, param_name);
237 if (value) {
238 *opt_value = (bool) bt_value_bool_get(value);
239 }
240 }
241
242 static const char *color_choices[] = { "never", "auto", "always", NULL };
243
244 static const struct bt_param_validation_map_value_entry_descr details_params[] = {
245 { COLOR_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = {
246 .choices = color_choices,
247 } } },
248 { WITH_METADATA_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
249 { WITH_DATA_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
250 { COMPACT_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
251 { WITH_TIME_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
252 { WITH_TRACE_NAME_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
253 { WITH_STREAM_CLASS_NAME_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
254 { WITH_STREAM_NAME_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
255 { WITH_UUID_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
256 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
257 };
258
259 static
260 bt_component_class_initialize_method_status configure_details_comp(
261 struct details_comp *details_comp,
262 const bt_value *params)
263 {
264 bt_component_class_initialize_method_status status;
265 const bt_value *value;
266 const char *str;
267 enum bt_param_validation_status validation_status;
268 gchar *validate_error = NULL;
269
270 validation_status = bt_param_validation_validate(params,
271 details_params, &validate_error);
272 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
273 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
274 goto end;
275 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
276 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
277 BT_COMP_LOGE_APPEND_CAUSE(details_comp->self_comp,
278 "%s", validate_error);
279 goto end;
280 }
281
282 /* Colorize output? */
283 details_comp->cfg.with_color = bt_common_colors_supported();
284 value = bt_value_map_borrow_entry_value_const(params, COLOR_PARAM_NAME);
285 if (value) {
286 str = bt_value_string_get(value);
287
288 if (strcmp(str, "never") == 0) {
289 details_comp->cfg.with_color = false;
290 } else if (strcmp(str, "auto") == 0) {
291 details_comp->cfg.with_color =
292 bt_common_colors_supported();
293 } else {
294 BT_ASSERT(strcmp(str, "always") == 0);
295
296 details_comp->cfg.with_color = true;
297 }
298 }
299
300 /* With metadata objects? */
301 configure_bool_opt(details_comp, params, WITH_METADATA_PARAM_NAME,
302 true, &details_comp->cfg.with_meta);
303
304 /* With data objects? */
305 configure_bool_opt(details_comp, params, WITH_DATA_PARAM_NAME,
306 true, &details_comp->cfg.with_data);
307
308 /* Compact? */
309 configure_bool_opt(details_comp, params, COMPACT_PARAM_NAME,
310 false, &details_comp->cfg.compact);
311
312 /* With time? */
313 configure_bool_opt(details_comp, params, WITH_TIME_PARAM_NAME,
314 true, &details_comp->cfg.with_time);
315
316 /* With trace name? */
317 configure_bool_opt(details_comp, params,
318 WITH_TRACE_NAME_PARAM_NAME,
319 true, &details_comp->cfg.with_trace_name);
320
321 /* With stream class name? */
322 configure_bool_opt(details_comp, params,
323 WITH_STREAM_CLASS_NAME_PARAM_NAME,
324 true, &details_comp->cfg.with_stream_class_name);
325
326 /* With stream name? */
327 configure_bool_opt(details_comp, params,
328 WITH_STREAM_NAME_PARAM_NAME,
329 true, &details_comp->cfg.with_stream_name);
330
331 /* With UUID? */
332 configure_bool_opt(details_comp, params,
333 WITH_UUID_PARAM_NAME, true, &details_comp->cfg.with_uuid);
334
335 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
336 goto end;
337
338 end:
339 g_free(validate_error);
340
341 return status;
342 }
343
344 static
345 void log_configuration(bt_self_component_sink *comp,
346 struct details_comp *details_comp)
347 {
348 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
349 bt_component_get_name(bt_self_component_as_component(
350 bt_self_component_sink_as_self_component(comp))));
351 BT_COMP_LOGI(" Colorize output: %d", details_comp->cfg.with_color);
352 BT_COMP_LOGI(" Compact: %d", details_comp->cfg.compact);
353 BT_COMP_LOGI(" With metadata: %d", details_comp->cfg.with_meta);
354 BT_COMP_LOGI(" With time: %d", details_comp->cfg.with_time);
355 BT_COMP_LOGI(" With trace name: %d", details_comp->cfg.with_trace_name);
356 BT_COMP_LOGI(" With stream class name: %d",
357 details_comp->cfg.with_stream_class_name);
358 BT_COMP_LOGI(" With stream name: %d", details_comp->cfg.with_stream_name);
359 BT_COMP_LOGI(" With UUID: %d", details_comp->cfg.with_uuid);
360 }
361
362 BT_HIDDEN
363 bt_component_class_initialize_method_status details_init(
364 bt_self_component_sink *comp,
365 bt_self_component_sink_configuration *config,
366 const bt_value *params,
367 __attribute__((unused)) void *init_method_data)
368 {
369 bt_component_class_initialize_method_status status =
370 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
371 bt_self_component_add_port_status add_port_status;
372 struct details_comp *details_comp = NULL;
373
374 add_port_status = bt_self_component_sink_add_input_port(comp,
375 IN_PORT_NAME, NULL, NULL);
376 switch (add_port_status) {
377 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
378 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
379 break;
380 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
381 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
382 break;
383 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
384 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
385 break;
386 default:
387 abort();
388 }
389
390 details_comp = create_details_comp(comp);
391 if (!details_comp) {
392 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
393 goto error;
394 }
395
396 if (configure_details_comp(details_comp, params)) {
397 BT_COMP_LOGE_STR("Failed to configure component.");
398 goto error;
399 }
400
401 log_configuration(comp, details_comp);
402 bt_self_component_set_data(
403 bt_self_component_sink_as_self_component(comp), details_comp);
404 goto end;
405
406 error:
407 if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
408 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
409 }
410
411 destroy_details_comp(details_comp);
412
413 end:
414 return status;
415 }
416
417 BT_HIDDEN
418 bt_component_class_sink_graph_is_configured_method_status
419 details_graph_is_configured(bt_self_component_sink *comp)
420 {
421 bt_component_class_sink_graph_is_configured_method_status status;
422 bt_self_component_port_input_message_iterator_create_from_sink_component_status
423 msg_iter_status;
424 bt_self_component_port_input_message_iterator *iterator;
425 struct details_comp *details_comp;
426 bt_self_component_port_input *in_port;
427
428 details_comp = bt_self_component_get_data(
429 bt_self_component_sink_as_self_component(comp));
430 BT_ASSERT(details_comp);
431 in_port = bt_self_component_sink_borrow_input_port_by_name(comp,
432 IN_PORT_NAME);
433 if (!bt_port_is_connected(bt_port_input_as_port_const(
434 bt_self_component_port_input_as_port_input(in_port)))) {
435 BT_COMP_LOGE("Single input port is not connected: "
436 "port-name=\"%s\"", IN_PORT_NAME);
437 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
438 goto end;
439 }
440
441 msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component(
442 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
443 IN_PORT_NAME), &iterator);
444 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
445 status = (int) msg_iter_status;
446 goto end;
447 }
448
449 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
450 details_comp->msg_iter, iterator);
451
452 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
453
454 end:
455 return status;
456 }
457
458 BT_HIDDEN
459 bt_component_class_sink_consume_method_status
460 details_consume(bt_self_component_sink *comp)
461 {
462 bt_component_class_sink_consume_method_status ret =
463 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
464 bt_message_array_const msgs;
465 uint64_t count;
466 struct details_comp *details_comp;
467 bt_message_iterator_next_status next_status;
468 uint64_t i;
469
470 details_comp = bt_self_component_get_data(
471 bt_self_component_sink_as_self_component(comp));
472 BT_ASSERT_DBG(details_comp);
473 BT_ASSERT_DBG(details_comp->msg_iter);
474
475 /* Consume messages */
476 next_status = bt_self_component_port_input_message_iterator_next(
477 details_comp->msg_iter, &msgs, &count);
478 switch (next_status) {
479 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
480 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
481
482 for (i = 0; i < count; i++) {
483 int print_ret = details_write_message(details_comp,
484 msgs[i]);
485
486 if (print_ret) {
487 for (; i < count; i++) {
488 /* Put all remaining messages */
489 bt_message_put_ref(msgs[i]);
490 }
491
492 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
493 goto end;
494 }
495
496 /* Print output buffer to standard output and flush */
497 if (details_comp->str->len > 0) {
498 printf("%s", details_comp->str->str);
499 fflush(stdout);
500 details_comp->printed_something = true;
501 }
502
503 /* Put this message */
504 bt_message_put_ref(msgs[i]);
505 }
506
507 break;
508 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
509 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
510 goto end;
511 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
512 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
513 goto end;
514 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
515 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
516 goto end;
517 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
518 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
519 goto end;
520 default:
521 abort();
522 }
523
524 end:
525 return ret;
526 }
This page took 0.041317 seconds and 4 git commands to generate.