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