2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
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"
30 #include <babeltrace2/babeltrace.h>
32 #include "common/common.h"
33 #include "common/assert.h"
36 #include "plugins/common/param-validation/param-validation.h"
38 #define IN_PORT_NAME "in"
39 #define COLOR_PARAM_NAME "color"
40 #define WITH_METADATA_PARAM_NAME "with-metadata"
41 #define WITH_DATA_PARAM_NAME "with-data"
42 #define WITH_TIME_PARAM_NAME "with-time"
43 #define WITH_TRACE_NAME_PARAM_NAME "with-trace-name"
44 #define WITH_STREAM_CLASS_NAME_PARAM_NAME "with-stream-class-name"
45 #define WITH_STREAM_NAME_PARAM_NAME "with-stream-name"
46 #define WITH_UUID_PARAM_NAME "with-uuid"
47 #define COMPACT_PARAM_NAME "compact"
50 void details_destroy_details_trace_class_meta(
51 struct details_trace_class_meta
*details_tc_meta
)
53 if (!details_tc_meta
) {
57 if (details_tc_meta
->objects
) {
58 g_hash_table_destroy(details_tc_meta
->objects
);
59 details_tc_meta
->objects
= NULL
;
62 g_free(details_tc_meta
);
69 struct details_trace_class_meta
*details_create_details_trace_class_meta(void)
71 struct details_trace_class_meta
*details_tc_meta
=
72 g_new0(struct details_trace_class_meta
, 1);
74 if (!details_tc_meta
) {
78 details_tc_meta
->objects
= g_hash_table_new(
79 g_direct_hash
, g_direct_equal
);
80 if (!details_tc_meta
->objects
) {
81 details_destroy_details_trace_class_meta(details_tc_meta
);
82 details_tc_meta
= NULL
;
86 details_tc_meta
->tc_destruction_listener_id
= UINT64_C(-1);
89 return details_tc_meta
;
93 void destroy_details_comp(struct details_comp
*details_comp
)
102 if (details_comp
->meta
) {
104 * Remove trace class destruction listeners, because
105 * otherwise, when they are called, `details_comp`
106 * (their user data) won't exist anymore (we're
107 * destroying it here).
109 g_hash_table_iter_init(&iter
, details_comp
->meta
);
111 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
112 struct details_trace_class_meta
*details_tc_meta
=
115 if (details_tc_meta
->tc_destruction_listener_id
!=
117 if (bt_trace_class_remove_destruction_listener(
119 details_tc_meta
->tc_destruction_listener_id
)) {
120 bt_current_thread_clear_error();
125 g_hash_table_destroy(details_comp
->meta
);
126 details_comp
->meta
= NULL
;
129 if (details_comp
->traces
) {
131 * Remove trace destruction listeners, because
132 * otherwise, when they are called, `details_comp` won't
133 * exist anymore (we're destroying it here).
135 g_hash_table_iter_init(&iter
, details_comp
->traces
);
137 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
138 struct details_trace
*details_trace
= value
;
140 if (bt_trace_remove_destruction_listener(
142 details_trace
->trace_destruction_listener_id
)) {
143 bt_current_thread_clear_error();
147 g_hash_table_destroy(details_comp
->traces
);
148 details_comp
->traces
= NULL
;
151 if (details_comp
->str
) {
152 g_string_free(details_comp
->str
, TRUE
);
153 details_comp
->str
= NULL
;
156 BT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
157 details_comp
->msg_iter
);
158 g_free(details_comp
);
165 struct details_comp
*create_details_comp(
166 bt_self_component_sink
*self_comp_sink
)
168 struct details_comp
*details_comp
= g_new0(struct details_comp
, 1);
169 bt_self_component
*self_comp
=
170 bt_self_component_sink_as_self_component(self_comp_sink
);
176 details_comp
->log_level
= bt_component_get_logging_level(
177 bt_self_component_as_component(self_comp
));
178 details_comp
->self_comp
= self_comp
;
179 details_comp
->meta
= g_hash_table_new_full(g_direct_hash
,
180 g_direct_equal
, NULL
,
181 (GDestroyNotify
) details_destroy_details_trace_class_meta
);
182 if (!details_comp
->meta
) {
186 details_comp
->traces
= g_hash_table_new_full(g_direct_hash
,
187 g_direct_equal
, NULL
, g_free
);
188 if (!details_comp
->traces
) {
192 details_comp
->str
= g_string_new(NULL
);
193 if (!details_comp
->str
) {
200 destroy_details_comp(details_comp
);
208 void details_finalize(bt_self_component_sink
*comp
)
210 struct details_comp
*details_comp
;
213 details_comp
= bt_self_component_get_data(
214 bt_self_component_sink_as_self_component(comp
));
215 BT_ASSERT(details_comp
);
216 destroy_details_comp(details_comp
);
220 void configure_bool_opt(struct details_comp
*details_comp
,
221 const bt_value
*params
, const char *param_name
,
222 bool default_value
, bool *opt_value
)
224 const bt_value
*value
;
226 *opt_value
= default_value
;
227 value
= bt_value_map_borrow_entry_value_const(params
, param_name
);
229 *opt_value
= (bool) bt_value_bool_get(value
);
233 static const char *color_choices
[] = { "never", "auto", "always", NULL
};
235 static const struct bt_param_validation_map_value_entry_descr details_params
[] = {
236 { COLOR_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
237 .choices
= color_choices
,
239 { WITH_METADATA_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
240 { WITH_DATA_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
241 { COMPACT_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
242 { WITH_TIME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
243 { WITH_TRACE_NAME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
244 { WITH_STREAM_CLASS_NAME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
245 { WITH_STREAM_NAME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
246 { WITH_UUID_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
247 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
251 bt_component_class_initialize_method_status
configure_details_comp(
252 struct details_comp
*details_comp
,
253 const bt_value
*params
)
255 bt_component_class_initialize_method_status status
;
256 const bt_value
*value
;
258 enum bt_param_validation_status validation_status
;
259 gchar
*validate_error
= NULL
;
261 validation_status
= bt_param_validation_validate(params
,
262 details_params
, &validate_error
);
263 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
264 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
266 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
267 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
268 BT_COMP_LOGE_APPEND_CAUSE(details_comp
->self_comp
,
269 "%s", validate_error
);
273 /* Colorize output? */
274 details_comp
->cfg
.with_color
= bt_common_colors_supported();
275 value
= bt_value_map_borrow_entry_value_const(params
, COLOR_PARAM_NAME
);
277 str
= bt_value_string_get(value
);
279 if (strcmp(str
, "never") == 0) {
280 details_comp
->cfg
.with_color
= false;
281 } else if (strcmp(str
, "auto") == 0) {
282 details_comp
->cfg
.with_color
=
283 bt_common_colors_supported();
285 BT_ASSERT(strcmp(str
, "always") == 0);
287 details_comp
->cfg
.with_color
= true;
291 /* With metadata objects? */
292 configure_bool_opt(details_comp
, params
, WITH_METADATA_PARAM_NAME
,
293 true, &details_comp
->cfg
.with_meta
);
295 /* With data objects? */
296 configure_bool_opt(details_comp
, params
, WITH_DATA_PARAM_NAME
,
297 true, &details_comp
->cfg
.with_data
);
300 configure_bool_opt(details_comp
, params
, COMPACT_PARAM_NAME
,
301 false, &details_comp
->cfg
.compact
);
304 configure_bool_opt(details_comp
, params
, WITH_TIME_PARAM_NAME
,
305 true, &details_comp
->cfg
.with_time
);
307 /* With trace name? */
308 configure_bool_opt(details_comp
, params
,
309 WITH_TRACE_NAME_PARAM_NAME
,
310 true, &details_comp
->cfg
.with_trace_name
);
312 /* With stream class name? */
313 configure_bool_opt(details_comp
, params
,
314 WITH_STREAM_CLASS_NAME_PARAM_NAME
,
315 true, &details_comp
->cfg
.with_stream_class_name
);
317 /* With stream name? */
318 configure_bool_opt(details_comp
, params
,
319 WITH_STREAM_NAME_PARAM_NAME
,
320 true, &details_comp
->cfg
.with_stream_name
);
323 configure_bool_opt(details_comp
, params
,
324 WITH_UUID_PARAM_NAME
, true, &details_comp
->cfg
.with_uuid
);
326 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
330 g_free(validate_error
);
336 void log_configuration(bt_self_component_sink
*comp
,
337 struct details_comp
*details_comp
)
339 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
340 bt_component_get_name(bt_self_component_as_component(
341 bt_self_component_sink_as_self_component(comp
))));
342 BT_COMP_LOGI(" Colorize output: %d", details_comp
->cfg
.with_color
);
343 BT_COMP_LOGI(" Compact: %d", details_comp
->cfg
.compact
);
344 BT_COMP_LOGI(" With metadata: %d", details_comp
->cfg
.with_meta
);
345 BT_COMP_LOGI(" With time: %d", details_comp
->cfg
.with_time
);
346 BT_COMP_LOGI(" With trace name: %d", details_comp
->cfg
.with_trace_name
);
347 BT_COMP_LOGI(" With stream class name: %d",
348 details_comp
->cfg
.with_stream_class_name
);
349 BT_COMP_LOGI(" With stream name: %d", details_comp
->cfg
.with_stream_name
);
350 BT_COMP_LOGI(" With UUID: %d", details_comp
->cfg
.with_uuid
);
354 bt_component_class_initialize_method_status
details_init(
355 bt_self_component_sink
*comp
,
356 bt_self_component_sink_configuration
*config
,
357 const bt_value
*params
,
358 __attribute__((unused
)) void *init_method_data
)
360 bt_component_class_initialize_method_status status
=
361 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
362 bt_self_component_add_port_status add_port_status
;
363 struct details_comp
*details_comp
= NULL
;
365 add_port_status
= bt_self_component_sink_add_input_port(comp
,
366 IN_PORT_NAME
, NULL
, NULL
);
367 switch (add_port_status
) {
368 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
:
369 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
371 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
372 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
374 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
375 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
381 details_comp
= create_details_comp(comp
);
383 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
387 if (configure_details_comp(details_comp
, params
)) {
388 BT_COMP_LOGE_STR("Failed to configure component.");
392 log_configuration(comp
, details_comp
);
393 bt_self_component_set_data(
394 bt_self_component_sink_as_self_component(comp
), details_comp
);
398 if (status
== BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
399 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
402 destroy_details_comp(details_comp
);
409 bt_component_class_sink_graph_is_configured_method_status
410 details_graph_is_configured(bt_self_component_sink
*comp
)
412 bt_component_class_sink_graph_is_configured_method_status status
;
413 bt_message_iterator_create_from_sink_component_status
415 bt_message_iterator
*iterator
;
416 struct details_comp
*details_comp
;
417 bt_self_component_port_input
*in_port
;
419 details_comp
= bt_self_component_get_data(
420 bt_self_component_sink_as_self_component(comp
));
421 BT_ASSERT(details_comp
);
422 in_port
= bt_self_component_sink_borrow_input_port_by_name(comp
,
424 if (!bt_port_is_connected(bt_port_input_as_port_const(
425 bt_self_component_port_input_as_port_input(in_port
)))) {
426 BT_COMP_LOGE("Single input port is not connected: "
427 "port-name=\"%s\"", IN_PORT_NAME
);
428 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR
;
432 msg_iter_status
= bt_message_iterator_create_from_sink_component(
433 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
434 IN_PORT_NAME
), &iterator
);
435 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
436 status
= (int) msg_iter_status
;
440 BT_MESSAGE_ITERATOR_MOVE_REF(
441 details_comp
->msg_iter
, iterator
);
443 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
450 bt_component_class_sink_consume_method_status
451 details_consume(bt_self_component_sink
*comp
)
453 bt_component_class_sink_consume_method_status ret
=
454 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
455 bt_message_array_const msgs
;
457 struct details_comp
*details_comp
;
458 bt_message_iterator_next_status next_status
;
461 details_comp
= bt_self_component_get_data(
462 bt_self_component_sink_as_self_component(comp
));
463 BT_ASSERT_DBG(details_comp
);
464 BT_ASSERT_DBG(details_comp
->msg_iter
);
466 /* Consume messages */
467 next_status
= bt_message_iterator_next(
468 details_comp
->msg_iter
, &msgs
, &count
);
469 switch (next_status
) {
470 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
471 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
473 for (i
= 0; i
< count
; i
++) {
474 int print_ret
= details_write_message(details_comp
,
478 for (; i
< count
; i
++) {
479 /* Put all remaining messages */
480 bt_message_put_ref(msgs
[i
]);
483 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
487 /* Print output buffer to standard output and flush */
488 if (details_comp
->str
->len
> 0) {
489 printf("%s", details_comp
->str
->str
);
491 details_comp
->printed_something
= true;
494 /* Put this message */
495 bt_message_put_ref(msgs
[i
]);
499 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
500 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN
;
502 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
503 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
505 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
:
506 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
508 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
509 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR
;