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_name_param_name
= "with-trace-name";
60 const char * const with_stream_class_name_param_name
= "with-stream-class-name";
63 const char * const with_stream_name_param_name
= "with-stream-name";
66 const char * const with_uuid_param_name
= "with-uuid";
69 const char * const compact_param_name
= "compact";
72 void details_destroy_details_trace_class_meta(
73 struct details_trace_class_meta
*details_tc_meta
)
75 if (!details_tc_meta
) {
79 if (details_tc_meta
->objects
) {
80 g_hash_table_destroy(details_tc_meta
->objects
);
81 details_tc_meta
->objects
= NULL
;
84 g_free(details_tc_meta
);
91 struct details_trace_class_meta
*details_create_details_trace_class_meta(void)
93 struct details_trace_class_meta
*details_tc_meta
=
94 g_new0(struct details_trace_class_meta
, 1);
96 if (!details_tc_meta
) {
100 details_tc_meta
->objects
= g_hash_table_new(
101 g_direct_hash
, g_direct_equal
);
102 if (!details_tc_meta
->objects
) {
103 details_destroy_details_trace_class_meta(details_tc_meta
);
104 details_tc_meta
= NULL
;
108 details_tc_meta
->tc_destruction_listener_id
= UINT64_C(-1);
111 return details_tc_meta
;
115 void destroy_details_comp(struct details_comp
*details_comp
)
124 if (details_comp
->meta
) {
126 * Remove trace class destruction listeners, because
127 * otherwise, when they are called, `details_comp`
128 * (their user data) won't exist anymore (we're
129 * destroying it here).
131 g_hash_table_iter_init(&iter
, details_comp
->meta
);
133 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
134 struct details_trace_class_meta
*details_tc_meta
=
137 if (details_tc_meta
->tc_destruction_listener_id
!=
139 if (bt_trace_class_remove_destruction_listener(
141 details_tc_meta
->tc_destruction_listener_id
)) {
142 bt_current_thread_clear_error();
147 g_hash_table_destroy(details_comp
->meta
);
148 details_comp
->meta
= NULL
;
151 if (details_comp
->traces
) {
153 * Remove trace destruction listeners, because
154 * otherwise, when they are called, `details_comp` won't
155 * exist anymore (we're destroying it here).
157 g_hash_table_iter_init(&iter
, details_comp
->traces
);
159 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
160 struct details_trace
*details_trace
= value
;
162 if (bt_trace_remove_destruction_listener(
164 details_trace
->trace_destruction_listener_id
)) {
165 bt_current_thread_clear_error();
169 g_hash_table_destroy(details_comp
->traces
);
170 details_comp
->traces
= NULL
;
173 if (details_comp
->str
) {
174 g_string_free(details_comp
->str
, TRUE
);
175 details_comp
->str
= NULL
;
178 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
179 details_comp
->msg_iter
);
180 g_free(details_comp
);
187 struct details_comp
*create_details_comp(
188 bt_self_component_sink
*self_comp_sink
)
190 struct details_comp
*details_comp
= g_new0(struct details_comp
, 1);
191 bt_self_component
*self_comp
=
192 bt_self_component_sink_as_self_component(self_comp_sink
);
198 details_comp
->log_level
= bt_component_get_logging_level(
199 bt_self_component_as_component(self_comp
));
200 details_comp
->self_comp
= self_comp
;
201 details_comp
->meta
= g_hash_table_new_full(g_direct_hash
,
202 g_direct_equal
, NULL
,
203 (GDestroyNotify
) details_destroy_details_trace_class_meta
);
204 if (!details_comp
->meta
) {
208 details_comp
->traces
= g_hash_table_new_full(g_direct_hash
,
209 g_direct_equal
, NULL
, g_free
);
210 if (!details_comp
->traces
) {
214 details_comp
->str
= g_string_new(NULL
);
215 if (!details_comp
->str
) {
222 destroy_details_comp(details_comp
);
230 void details_finalize(bt_self_component_sink
*comp
)
232 struct details_comp
*details_comp
;
235 details_comp
= bt_self_component_get_data(
236 bt_self_component_sink_as_self_component(comp
));
237 BT_ASSERT(details_comp
);
238 destroy_details_comp(details_comp
);
242 int configure_bool_opt(struct details_comp
*details_comp
,
243 const bt_value
*params
, const char *param_name
,
244 bool default_value
, bool *opt_value
)
247 const bt_value
*value
;
249 *opt_value
= default_value
;
250 value
= bt_value_map_borrow_entry_value_const(params
, param_name
);
252 if (!bt_value_is_bool(value
)) {
253 LOG_WRONG_PARAM_TYPE(param_name
, value
,
259 *opt_value
= (bool) bt_value_bool_get(value
);
267 int configure_details_comp(struct details_comp
*details_comp
,
268 const bt_value
*params
)
271 const bt_value
*value
;
274 /* Colorize output? */
275 details_comp
->cfg
.with_color
= bt_common_colors_supported();
276 value
= bt_value_map_borrow_entry_value_const(params
, color_param_name
);
278 if (!bt_value_is_string(value
)) {
279 LOG_WRONG_PARAM_TYPE(color_param_name
, value
,
280 BT_VALUE_TYPE_STRING
);
284 str
= bt_value_string_get(value
);
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();
291 } else if (strcmp(str
, "always") == 0) {
292 details_comp
->cfg
.with_color
= true;
294 BT_COMP_LOGE("Invalid `%s` parameter: unknown value "
295 "(expecting `never`, `auto`, or `always`): "
296 "value=\"%s\"", color_param_name
, str
);
301 /* With metadata objects? */
302 ret
= configure_bool_opt(details_comp
, params
, with_metadata_param_name
,
303 true, &details_comp
->cfg
.with_meta
);
309 ret
= configure_bool_opt(details_comp
, params
, compact_param_name
,
310 false, &details_comp
->cfg
.compact
);
316 ret
= configure_bool_opt(details_comp
, params
, with_time_param_name
,
317 true, &details_comp
->cfg
.with_time
);
322 /* With trace name? */
323 ret
= configure_bool_opt(details_comp
, params
,
324 with_trace_name_param_name
,
325 true, &details_comp
->cfg
.with_trace_name
);
330 /* With stream class name? */
331 ret
= configure_bool_opt(details_comp
, params
,
332 with_stream_class_name_param_name
,
333 true, &details_comp
->cfg
.with_stream_class_name
);
338 /* With stream name? */
339 ret
= configure_bool_opt(details_comp
, params
,
340 with_stream_name_param_name
,
341 true, &details_comp
->cfg
.with_stream_name
);
347 ret
= configure_bool_opt(details_comp
, params
,
348 with_uuid_param_name
, true, &details_comp
->cfg
.with_uuid
);
363 void log_configuration(bt_self_component_sink
*comp
,
364 struct details_comp
*details_comp
)
366 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
367 bt_component_get_name(bt_self_component_as_component(
368 bt_self_component_sink_as_self_component(comp
))));
369 BT_COMP_LOGI(" Colorize output: %d", details_comp
->cfg
.with_color
);
370 BT_COMP_LOGI(" Compact: %d", details_comp
->cfg
.compact
);
371 BT_COMP_LOGI(" With metadata: %d", details_comp
->cfg
.with_meta
);
372 BT_COMP_LOGI(" With time: %d", details_comp
->cfg
.with_time
);
373 BT_COMP_LOGI(" With trace name: %d", details_comp
->cfg
.with_trace_name
);
374 BT_COMP_LOGI(" With stream class name: %d",
375 details_comp
->cfg
.with_stream_class_name
);
376 BT_COMP_LOGI(" With stream name: %d", details_comp
->cfg
.with_stream_name
);
377 BT_COMP_LOGI(" With UUID: %d", details_comp
->cfg
.with_uuid
);
381 bt_component_class_init_method_status
details_init(bt_self_component_sink
*comp
,
382 const bt_value
*params
,
383 __attribute__((unused
)) void *init_method_data
)
385 bt_component_class_init_method_status status
=
386 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
387 bt_self_component_add_port_status add_port_status
;
388 struct details_comp
*details_comp
= NULL
;
390 add_port_status
= bt_self_component_sink_add_input_port(comp
,
391 in_port_name
, NULL
, NULL
);
392 switch (add_port_status
) {
393 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
:
394 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
396 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
397 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
399 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
400 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
406 details_comp
= create_details_comp(comp
);
408 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
412 if (configure_details_comp(details_comp
, params
)) {
413 BT_COMP_LOGE_STR("Failed to configure component.");
417 log_configuration(comp
, details_comp
);
418 bt_self_component_set_data(
419 bt_self_component_sink_as_self_component(comp
), details_comp
);
423 if (status
== BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
) {
424 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
427 destroy_details_comp(details_comp
);
434 bt_component_class_sink_graph_is_configured_method_status
435 details_graph_is_configured(bt_self_component_sink
*comp
)
437 bt_component_class_sink_graph_is_configured_method_status status
=
438 BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
439 bt_self_component_port_input_message_iterator
*iterator
;
440 struct details_comp
*details_comp
;
441 bt_self_component_port_input
*in_port
;
443 details_comp
= bt_self_component_get_data(
444 bt_self_component_sink_as_self_component(comp
));
445 BT_ASSERT(details_comp
);
446 in_port
= bt_self_component_sink_borrow_input_port_by_name(comp
,
448 if (!bt_port_is_connected(bt_port_input_as_port_const(
449 bt_self_component_port_input_as_port_input(in_port
)))) {
450 BT_COMP_LOGE("Single input port is not connected: "
451 "port-name=\"%s\"", in_port_name
);
452 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR
;
456 iterator
= bt_self_component_port_input_message_iterator_create(
457 bt_self_component_sink_borrow_input_port_by_name(comp
,
460 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR
;
464 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
465 details_comp
->msg_iter
, iterator
);
472 bt_component_class_sink_consume_method_status
473 details_consume(bt_self_component_sink
*comp
)
475 bt_component_class_sink_consume_method_status ret
=
476 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
477 bt_message_array_const msgs
;
479 struct details_comp
*details_comp
;
480 bt_message_iterator_next_status next_status
;
483 details_comp
= bt_self_component_get_data(
484 bt_self_component_sink_as_self_component(comp
));
485 BT_ASSERT(details_comp
);
486 BT_ASSERT(details_comp
->msg_iter
);
488 /* Consume messages */
489 next_status
= bt_self_component_port_input_message_iterator_next(
490 details_comp
->msg_iter
, &msgs
, &count
);
491 switch (next_status
) {
492 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
493 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
495 for (i
= 0; i
< count
; i
++) {
496 int print_ret
= details_write_message(details_comp
,
500 for (; i
< count
; i
++) {
501 /* Put all remaining messages */
502 bt_message_put_ref(msgs
[i
]);
505 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
509 /* Print output buffer to standard output and flush */
510 if (details_comp
->str
->len
> 0) {
511 printf("%s", details_comp
->str
->str
);
513 details_comp
->printed_something
= true;
516 /* Put this message */
517 bt_message_put_ref(msgs
[i
]);
521 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
522 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN
;
524 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
525 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
527 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
:
528 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
530 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
531 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR
;