Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / plugins / text / details / details.c
CommitLineData
55478183
PP
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
d400b9e6 23#define BT_COMP_LOG_SELF_COMP (details_comp->self_comp)
6c868a3a 24#define BT_LOG_OUTPUT_LEVEL (details_comp->log_level)
350ad6c1 25#define BT_LOG_TAG "PLUGIN/SINK.TEXT.DETAILS"
d9c39b0a 26#include "logging/comp-logging.h"
55478183 27
c4f23e30
FD
28#include <stdbool.h>
29
55478183
PP
30#include <babeltrace2/babeltrace.h>
31
32#include "common/common.h"
33#include "common/assert.h"
34#include "details.h"
35#include "write.h"
48a0e52c 36#include "plugins/common/param-validation/param-validation.h"
55478183
PP
37
38#define LOG_WRONG_PARAM_TYPE(_name, _value, _exp_type) \
39 do { \
d400b9e6 40 BT_COMP_LOGE("Wrong `%s` parameter type: type=%s, " \
55478183
PP
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
48a0e52c
SM
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"
55478183
PP
57
58BT_HIDDEN
59void 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
73end:
74 return;
75}
76
77BT_HIDDEN
78struct 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
97end:
98 return details_tc_meta;
99}
100
101static
102void 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)) {
b80991f6
PP
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 }
55478183
PP
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
b80991f6
PP
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 }
55478183
PP
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
169end:
170 return;
171}
172
173static
6c868a3a
PP
174struct details_comp *create_details_comp(
175 bt_self_component_sink *self_comp_sink)
55478183
PP
176{
177 struct details_comp *details_comp = g_new0(struct details_comp, 1);
6c868a3a
PP
178 bt_self_component *self_comp =
179 bt_self_component_sink_as_self_component(self_comp_sink);
55478183
PP
180
181 if (!details_comp) {
182 goto error;
183 }
184
6c868a3a
PP
185 details_comp->log_level = bt_component_get_logging_level(
186 bt_self_component_as_component(self_comp));
d400b9e6 187 details_comp->self_comp = self_comp;
55478183
PP
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
208error:
209 destroy_details_comp(details_comp);
210 details_comp = NULL;
211
212end:
213 return details_comp;
214}
215
216BT_HIDDEN
217void 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
228static
48a0e52c 229void configure_bool_opt(struct details_comp *details_comp,
55478183
PP
230 const bt_value *params, const char *param_name,
231 bool default_value, bool *opt_value)
232{
55478183
PP
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) {
55478183
PP
238 *opt_value = (bool) bt_value_bool_get(value);
239 }
55478183
PP
240}
241
48a0e52c
SM
242static const char *color_choices[] = { "never", "auto", "always", NULL };
243
244static 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
55478183 259static
48a0e52c
SM
260bt_component_class_initialize_method_status configure_details_comp(
261 struct details_comp *details_comp,
55478183
PP
262 const bt_value *params)
263{
48a0e52c 264 bt_component_class_initialize_method_status status;
55478183
PP
265 const bt_value *value;
266 const char *str;
48a0e52c
SM
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 }
55478183
PP
281
282 /* Colorize output? */
283 details_comp->cfg.with_color = bt_common_colors_supported();
48a0e52c 284 value = bt_value_map_borrow_entry_value_const(params, COLOR_PARAM_NAME);
55478183 285 if (value) {
55478183
PP
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();
55478183 293 } else {
48a0e52c
SM
294 BT_ASSERT(strcmp(str, "always") == 0);
295
296 details_comp->cfg.with_color = true;
55478183
PP
297 }
298 }
299
300 /* With metadata objects? */
48a0e52c 301 configure_bool_opt(details_comp, params, WITH_METADATA_PARAM_NAME,
55478183 302 true, &details_comp->cfg.with_meta);
55478183 303
cc413248 304 /* With data objects? */
48a0e52c 305 configure_bool_opt(details_comp, params, WITH_DATA_PARAM_NAME,
cc413248 306 true, &details_comp->cfg.with_data);
cc413248 307
55478183 308 /* Compact? */
48a0e52c 309 configure_bool_opt(details_comp, params, COMPACT_PARAM_NAME,
55478183 310 false, &details_comp->cfg.compact);
55478183
PP
311
312 /* With time? */
48a0e52c 313 configure_bool_opt(details_comp, params, WITH_TIME_PARAM_NAME,
55478183 314 true, &details_comp->cfg.with_time);
55478183 315
55478183 316 /* With trace name? */
48a0e52c
SM
317 configure_bool_opt(details_comp, params,
318 WITH_TRACE_NAME_PARAM_NAME,
55478183 319 true, &details_comp->cfg.with_trace_name);
55478183
PP
320
321 /* With stream class name? */
48a0e52c
SM
322 configure_bool_opt(details_comp, params,
323 WITH_STREAM_CLASS_NAME_PARAM_NAME,
55478183 324 true, &details_comp->cfg.with_stream_class_name);
55478183
PP
325
326 /* With stream name? */
48a0e52c
SM
327 configure_bool_opt(details_comp, params,
328 WITH_STREAM_NAME_PARAM_NAME,
55478183 329 true, &details_comp->cfg.with_stream_name);
55478183
PP
330
331 /* With UUID? */
48a0e52c
SM
332 configure_bool_opt(details_comp, params,
333 WITH_UUID_PARAM_NAME, true, &details_comp->cfg.with_uuid);
55478183 334
48a0e52c 335 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
55478183
PP
336 goto end;
337
55478183 338end:
48a0e52c
SM
339 g_free(validate_error);
340
341 return status;
55478183
PP
342}
343
344static
345void log_configuration(bt_self_component_sink *comp,
346 struct details_comp *details_comp)
347{
d400b9e6 348 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
55478183
PP
349 bt_component_get_name(bt_self_component_as_component(
350 bt_self_component_sink_as_self_component(comp))));
d400b9e6
PP
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);
d400b9e6
PP
355 BT_COMP_LOGI(" With trace name: %d", details_comp->cfg.with_trace_name);
356 BT_COMP_LOGI(" With stream class name: %d",
55478183 357 details_comp->cfg.with_stream_class_name);
d400b9e6
PP
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);
55478183
PP
360}
361
362BT_HIDDEN
21a9f056 363bt_component_class_initialize_method_status details_init(
59225a3e
SM
364 bt_self_component_sink *comp,
365 bt_self_component_sink_configuration *config,
55478183
PP
366 const bt_value *params,
367 __attribute__((unused)) void *init_method_data)
368{
21a9f056
FD
369 bt_component_class_initialize_method_status status =
370 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
d24d5663 371 bt_self_component_add_port_status add_port_status;
55478183
PP
372 struct details_comp *details_comp = NULL;
373
d24d5663 374 add_port_status = bt_self_component_sink_add_input_port(comp,
48a0e52c 375 IN_PORT_NAME, NULL, NULL);
d24d5663
PP
376 switch (add_port_status) {
377 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
21a9f056 378 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
d24d5663
PP
379 break;
380 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
21a9f056 381 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
d24d5663
PP
382 break;
383 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
21a9f056 384 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
d24d5663
PP
385 break;
386 default:
387 abort();
55478183
PP
388 }
389
6c868a3a 390 details_comp = create_details_comp(comp);
55478183 391 if (!details_comp) {
21a9f056 392 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
55478183
PP
393 goto error;
394 }
395
396 if (configure_details_comp(details_comp, params)) {
d400b9e6 397 BT_COMP_LOGE_STR("Failed to configure component.");
55478183
PP
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
406error:
21a9f056
FD
407 if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
408 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
55478183
PP
409 }
410
411 destroy_details_comp(details_comp);
412
413end:
414 return status;
415}
416
417BT_HIDDEN
d24d5663
PP
418bt_component_class_sink_graph_is_configured_method_status
419details_graph_is_configured(bt_self_component_sink *comp)
55478183 420{
e803df70
SM
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;
55478183
PP
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,
48a0e52c 432 IN_PORT_NAME);
55478183
PP
433 if (!bt_port_is_connected(bt_port_input_as_port_const(
434 bt_self_component_port_input_as_port_input(in_port)))) {
d400b9e6 435 BT_COMP_LOGE("Single input port is not connected: "
48a0e52c 436 "port-name=\"%s\"", IN_PORT_NAME);
d24d5663 437 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
55478183
PP
438 goto end;
439 }
440
e803df70 441 msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component(
ca02df0a 442 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
48a0e52c 443 IN_PORT_NAME), &iterator);
e803df70
SM
444 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
445 status = (int) msg_iter_status;
55478183
PP
446 goto end;
447 }
448
449 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
450 details_comp->msg_iter, iterator);
451
e803df70
SM
452 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
453
55478183
PP
454end:
455 return status;
456}
457
458BT_HIDDEN
d24d5663
PP
459bt_component_class_sink_consume_method_status
460details_consume(bt_self_component_sink *comp)
55478183 461{
d24d5663
PP
462 bt_component_class_sink_consume_method_status ret =
463 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
55478183
PP
464 bt_message_array_const msgs;
465 uint64_t count;
466 struct details_comp *details_comp;
d24d5663 467 bt_message_iterator_next_status next_status;
55478183
PP
468 uint64_t i;
469
470 details_comp = bt_self_component_get_data(
471 bt_self_component_sink_as_self_component(comp));
98b15851
PP
472 BT_ASSERT_DBG(details_comp);
473 BT_ASSERT_DBG(details_comp->msg_iter);
55478183
PP
474
475 /* Consume messages */
d24d5663 476 next_status = bt_self_component_port_input_message_iterator_next(
55478183 477 details_comp->msg_iter, &msgs, &count);
d24d5663
PP
478 switch (next_status) {
479 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
480 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
55478183
PP
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
d24d5663 492 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
55478183
PP
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;
d24d5663
PP
508 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
509 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
55478183 510 goto end;
d24d5663
PP
511 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
512 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
55478183 513 goto end;
d24d5663
PP
514 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
515 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
55478183 516 goto end;
d24d5663
PP
517 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
518 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
55478183
PP
519 goto end;
520 default:
521 abort();
522 }
523
524end:
525 return ret;
526}
This page took 0.0613899999999999 seconds and 4 git commands to generate.