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 "plugins/comp-logging.h"
28 #include <babeltrace2/babeltrace.h>
30 #include "common/common.h"
31 #include "common/assert.h"
35 #define LOG_WRONG_PARAM_TYPE(_name, _value, _exp_type) \
37 BT_COMP_LOGE("Wrong `%s` parameter type: type=%s, " \
39 (_name), bt_common_value_type_string( \
40 bt_value_get_type(_value)), \
41 bt_common_value_type_string(_exp_type)); \
45 const char * const in_port_name
= "in";
48 const char * const color_param_name
= "color";
51 const char * const with_metadata_param_name
= "with-metadata";
54 const char * const with_time_param_name
= "with-time";
57 const char * const with_trace_class_name_param_name
= "with-trace-class-name";
60 const char * const with_trace_name_param_name
= "with-trace-name";
63 const char * const with_stream_class_name_param_name
= "with-stream-class-name";
66 const char * const with_stream_name_param_name
= "with-stream-name";
69 const char * const with_uuid_param_name
= "with-uuid";
72 const char * const compact_param_name
= "compact";
75 void details_destroy_details_trace_class_meta(
76 struct details_trace_class_meta
*details_tc_meta
)
78 if (!details_tc_meta
) {
82 if (details_tc_meta
->objects
) {
83 g_hash_table_destroy(details_tc_meta
->objects
);
84 details_tc_meta
->objects
= NULL
;
87 g_free(details_tc_meta
);
94 struct details_trace_class_meta
*details_create_details_trace_class_meta(void)
96 struct details_trace_class_meta
*details_tc_meta
=
97 g_new0(struct details_trace_class_meta
, 1);
99 if (!details_tc_meta
) {
103 details_tc_meta
->objects
= g_hash_table_new(
104 g_direct_hash
, g_direct_equal
);
105 if (!details_tc_meta
->objects
) {
106 details_destroy_details_trace_class_meta(details_tc_meta
);
107 details_tc_meta
= NULL
;
111 details_tc_meta
->tc_destruction_listener_id
= UINT64_C(-1);
114 return details_tc_meta
;
118 void destroy_details_comp(struct details_comp
*details_comp
)
127 if (details_comp
->meta
) {
129 * Remove trace class destruction listeners, because
130 * otherwise, when they are called, `details_comp`
131 * (their user data) won't exist anymore (we're
132 * destroying it here).
134 g_hash_table_iter_init(&iter
, details_comp
->meta
);
136 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
137 struct details_trace_class_meta
*details_tc_meta
=
140 if (details_tc_meta
->tc_destruction_listener_id
!=
142 if (bt_trace_class_remove_destruction_listener(
144 details_tc_meta
->tc_destruction_listener_id
)) {
145 bt_current_thread_clear_error();
150 g_hash_table_destroy(details_comp
->meta
);
151 details_comp
->meta
= NULL
;
154 if (details_comp
->traces
) {
156 * Remove trace destruction listeners, because
157 * otherwise, when they are called, `details_comp` won't
158 * exist anymore (we're destroying it here).
160 g_hash_table_iter_init(&iter
, details_comp
->traces
);
162 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
163 struct details_trace
*details_trace
= value
;
165 if (bt_trace_remove_destruction_listener(
167 details_trace
->trace_destruction_listener_id
)) {
168 bt_current_thread_clear_error();
172 g_hash_table_destroy(details_comp
->traces
);
173 details_comp
->traces
= NULL
;
176 if (details_comp
->str
) {
177 g_string_free(details_comp
->str
, TRUE
);
178 details_comp
->str
= NULL
;
181 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
182 details_comp
->msg_iter
);
183 g_free(details_comp
);
190 struct details_comp
*create_details_comp(
191 bt_self_component_sink
*self_comp_sink
)
193 struct details_comp
*details_comp
= g_new0(struct details_comp
, 1);
194 bt_self_component
*self_comp
=
195 bt_self_component_sink_as_self_component(self_comp_sink
);
201 details_comp
->log_level
= bt_component_get_logging_level(
202 bt_self_component_as_component(self_comp
));
203 details_comp
->self_comp
= self_comp
;
204 details_comp
->meta
= g_hash_table_new_full(g_direct_hash
,
205 g_direct_equal
, NULL
,
206 (GDestroyNotify
) details_destroy_details_trace_class_meta
);
207 if (!details_comp
->meta
) {
211 details_comp
->traces
= g_hash_table_new_full(g_direct_hash
,
212 g_direct_equal
, NULL
, g_free
);
213 if (!details_comp
->traces
) {
217 details_comp
->str
= g_string_new(NULL
);
218 if (!details_comp
->str
) {
225 destroy_details_comp(details_comp
);
233 void details_finalize(bt_self_component_sink
*comp
)
235 struct details_comp
*details_comp
;
238 details_comp
= bt_self_component_get_data(
239 bt_self_component_sink_as_self_component(comp
));
240 BT_ASSERT(details_comp
);
241 destroy_details_comp(details_comp
);
245 int configure_bool_opt(struct details_comp
*details_comp
,
246 const bt_value
*params
, const char *param_name
,
247 bool default_value
, bool *opt_value
)
250 const bt_value
*value
;
252 *opt_value
= default_value
;
253 value
= bt_value_map_borrow_entry_value_const(params
, param_name
);
255 if (!bt_value_is_bool(value
)) {
256 LOG_WRONG_PARAM_TYPE(param_name
, value
,
262 *opt_value
= (bool) bt_value_bool_get(value
);
270 int configure_details_comp(struct details_comp
*details_comp
,
271 const bt_value
*params
)
274 const bt_value
*value
;
277 /* Colorize output? */
278 details_comp
->cfg
.with_color
= bt_common_colors_supported();
279 value
= bt_value_map_borrow_entry_value_const(params
, color_param_name
);
281 if (!bt_value_is_string(value
)) {
282 LOG_WRONG_PARAM_TYPE(color_param_name
, value
,
283 BT_VALUE_TYPE_STRING
);
287 str
= bt_value_string_get(value
);
289 if (strcmp(str
, "never") == 0) {
290 details_comp
->cfg
.with_color
= false;
291 } else if (strcmp(str
, "auto") == 0) {
292 details_comp
->cfg
.with_color
=
293 bt_common_colors_supported();
294 } else if (strcmp(str
, "always") == 0) {
295 details_comp
->cfg
.with_color
= true;
297 BT_COMP_LOGE("Invalid `%s` parameter: unknown value "
298 "(expecting `never`, `auto`, or `always`): "
299 "value=\"%s\"", color_param_name
, str
);
304 /* With metadata objects? */
305 ret
= configure_bool_opt(details_comp
, params
, with_metadata_param_name
,
306 true, &details_comp
->cfg
.with_meta
);
312 ret
= configure_bool_opt(details_comp
, params
, compact_param_name
,
313 false, &details_comp
->cfg
.compact
);
319 ret
= configure_bool_opt(details_comp
, params
, with_time_param_name
,
320 true, &details_comp
->cfg
.with_time
);
325 /* With trace class name? */
326 ret
= configure_bool_opt(details_comp
, params
,
327 with_trace_class_name_param_name
,
328 true, &details_comp
->cfg
.with_trace_class_name
);
333 /* With trace name? */
334 ret
= configure_bool_opt(details_comp
, params
,
335 with_trace_name_param_name
,
336 true, &details_comp
->cfg
.with_trace_name
);
341 /* With stream class name? */
342 ret
= configure_bool_opt(details_comp
, params
,
343 with_stream_class_name_param_name
,
344 true, &details_comp
->cfg
.with_stream_class_name
);
349 /* With stream name? */
350 ret
= configure_bool_opt(details_comp
, params
,
351 with_stream_name_param_name
,
352 true, &details_comp
->cfg
.with_stream_name
);
358 ret
= configure_bool_opt(details_comp
, params
,
359 with_uuid_param_name
, true, &details_comp
->cfg
.with_uuid
);
374 void log_configuration(bt_self_component_sink
*comp
,
375 struct details_comp
*details_comp
)
377 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
378 bt_component_get_name(bt_self_component_as_component(
379 bt_self_component_sink_as_self_component(comp
))));
380 BT_COMP_LOGI(" Colorize output: %d", details_comp
->cfg
.with_color
);
381 BT_COMP_LOGI(" Compact: %d", details_comp
->cfg
.compact
);
382 BT_COMP_LOGI(" With metadata: %d", details_comp
->cfg
.with_meta
);
383 BT_COMP_LOGI(" With time: %d", details_comp
->cfg
.with_time
);
384 BT_COMP_LOGI(" With trace class name: %d",
385 details_comp
->cfg
.with_trace_class_name
);
386 BT_COMP_LOGI(" With trace name: %d", details_comp
->cfg
.with_trace_name
);
387 BT_COMP_LOGI(" With stream class name: %d",
388 details_comp
->cfg
.with_stream_class_name
);
389 BT_COMP_LOGI(" With stream name: %d", details_comp
->cfg
.with_stream_name
);
390 BT_COMP_LOGI(" With UUID: %d", details_comp
->cfg
.with_uuid
);
394 bt_component_class_init_method_status
details_init(bt_self_component_sink
*comp
,
395 const bt_value
*params
,
396 __attribute__((unused
)) void *init_method_data
)
398 bt_component_class_init_method_status status
=
399 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
400 bt_self_component_add_port_status add_port_status
;
401 struct details_comp
*details_comp
= NULL
;
403 add_port_status
= bt_self_component_sink_add_input_port(comp
,
404 in_port_name
, NULL
, NULL
);
405 switch (add_port_status
) {
406 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
:
407 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
409 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
410 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
412 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
413 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
419 details_comp
= create_details_comp(comp
);
421 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
425 if (configure_details_comp(details_comp
, params
)) {
426 BT_COMP_LOGE_STR("Failed to configure component.");
430 log_configuration(comp
, details_comp
);
431 bt_self_component_set_data(
432 bt_self_component_sink_as_self_component(comp
), details_comp
);
436 if (status
== BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
) {
437 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
440 destroy_details_comp(details_comp
);
447 bt_component_class_sink_graph_is_configured_method_status
448 details_graph_is_configured(bt_self_component_sink
*comp
)
450 bt_component_class_sink_graph_is_configured_method_status status
=
451 BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
452 bt_self_component_port_input_message_iterator
*iterator
;
453 struct details_comp
*details_comp
;
454 bt_self_component_port_input
*in_port
;
456 details_comp
= bt_self_component_get_data(
457 bt_self_component_sink_as_self_component(comp
));
458 BT_ASSERT(details_comp
);
459 in_port
= bt_self_component_sink_borrow_input_port_by_name(comp
,
461 if (!bt_port_is_connected(bt_port_input_as_port_const(
462 bt_self_component_port_input_as_port_input(in_port
)))) {
463 BT_COMP_LOGE("Single input port is not connected: "
464 "port-name=\"%s\"", in_port_name
);
465 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR
;
469 iterator
= bt_self_component_port_input_message_iterator_create(
470 bt_self_component_sink_borrow_input_port_by_name(comp
,
473 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR
;
477 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
478 details_comp
->msg_iter
, iterator
);
485 bt_component_class_sink_consume_method_status
486 details_consume(bt_self_component_sink
*comp
)
488 bt_component_class_sink_consume_method_status ret
=
489 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
490 bt_message_array_const msgs
;
492 struct details_comp
*details_comp
;
493 bt_message_iterator_next_status next_status
;
496 details_comp
= bt_self_component_get_data(
497 bt_self_component_sink_as_self_component(comp
));
498 BT_ASSERT(details_comp
);
499 BT_ASSERT(details_comp
->msg_iter
);
501 /* Consume messages */
502 next_status
= bt_self_component_port_input_message_iterator_next(
503 details_comp
->msg_iter
, &msgs
, &count
);
504 switch (next_status
) {
505 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
506 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
508 for (i
= 0; i
< count
; i
++) {
509 int print_ret
= details_write_message(details_comp
,
513 for (; i
< count
; i
++) {
514 /* Put all remaining messages */
515 bt_message_put_ref(msgs
[i
]);
518 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
522 /* Print output buffer to standard output and flush */
523 if (details_comp
->str
->len
> 0) {
524 printf("%s", details_comp
->str
->str
);
526 details_comp
->printed_something
= true;
529 /* Put this message */
530 bt_message_put_ref(msgs
[i
]);
534 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
535 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN
;
537 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
538 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
540 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
:
541 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
543 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
544 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR
;