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_LOG_TAG "PLUGIN-TEXT-DETAILS-SINK"
26 #include <babeltrace2/babeltrace.h>
28 #include "common/common.h"
29 #include "common/assert.h"
33 #define LOG_WRONG_PARAM_TYPE(_name, _value, _exp_type) \
35 BT_LOGE("Wrong `%s` parameter type: type=%s, " \
37 (_name), bt_common_value_type_string( \
38 bt_value_get_type(_value)), \
39 bt_common_value_type_string(_exp_type)); \
43 const char * const in_port_name
= "in";
46 const char * const color_param_name
= "color";
49 const char * const with_metadata_param_name
= "with-metadata";
52 const char * const with_time_param_name
= "with-time";
55 const char * const with_trace_class_name_param_name
= "with-trace-class-name";
58 const char * const with_trace_name_param_name
= "with-trace-name";
61 const char * const with_stream_class_name_param_name
= "with-stream-class-name";
64 const char * const with_stream_name_param_name
= "with-stream-name";
67 const char * const with_uuid_param_name
= "with-uuid";
70 const char * const compact_param_name
= "compact";
73 void details_destroy_details_trace_class_meta(
74 struct details_trace_class_meta
*details_tc_meta
)
76 if (!details_tc_meta
) {
80 if (details_tc_meta
->objects
) {
81 g_hash_table_destroy(details_tc_meta
->objects
);
82 details_tc_meta
->objects
= NULL
;
85 g_free(details_tc_meta
);
92 struct details_trace_class_meta
*details_create_details_trace_class_meta(void)
94 struct details_trace_class_meta
*details_tc_meta
=
95 g_new0(struct details_trace_class_meta
, 1);
97 if (!details_tc_meta
) {
101 details_tc_meta
->objects
= g_hash_table_new(
102 g_direct_hash
, g_direct_equal
);
103 if (!details_tc_meta
->objects
) {
104 details_destroy_details_trace_class_meta(details_tc_meta
);
105 details_tc_meta
= NULL
;
109 details_tc_meta
->tc_destruction_listener_id
= UINT64_C(-1);
112 return details_tc_meta
;
116 void destroy_details_comp(struct details_comp
*details_comp
)
125 if (details_comp
->meta
) {
127 * Remove trace class destruction listeners, because
128 * otherwise, when they are called, `details_comp`
129 * (their user data) won't exist anymore (we're
130 * destroying it here).
132 g_hash_table_iter_init(&iter
, details_comp
->meta
);
134 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
135 struct details_trace_class_meta
*details_tc_meta
=
138 if (details_tc_meta
->tc_destruction_listener_id
!=
140 bt_trace_class_remove_destruction_listener(
142 details_tc_meta
->tc_destruction_listener_id
);
146 g_hash_table_destroy(details_comp
->meta
);
147 details_comp
->meta
= NULL
;
150 if (details_comp
->traces
) {
152 * Remove trace destruction listeners, because
153 * otherwise, when they are called, `details_comp` won't
154 * exist anymore (we're destroying it here).
156 g_hash_table_iter_init(&iter
, details_comp
->traces
);
158 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
159 struct details_trace
*details_trace
= value
;
161 bt_trace_remove_destruction_listener(
163 details_trace
->trace_destruction_listener_id
);
166 g_hash_table_destroy(details_comp
->traces
);
167 details_comp
->traces
= NULL
;
170 if (details_comp
->str
) {
171 g_string_free(details_comp
->str
, TRUE
);
172 details_comp
->str
= NULL
;
175 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
176 details_comp
->msg_iter
);
177 g_free(details_comp
);
184 struct details_comp
*create_details_comp(void)
186 struct details_comp
*details_comp
= g_new0(struct details_comp
, 1);
192 details_comp
->meta
= g_hash_table_new_full(g_direct_hash
,
193 g_direct_equal
, NULL
,
194 (GDestroyNotify
) details_destroy_details_trace_class_meta
);
195 if (!details_comp
->meta
) {
199 details_comp
->traces
= g_hash_table_new_full(g_direct_hash
,
200 g_direct_equal
, NULL
, g_free
);
201 if (!details_comp
->traces
) {
205 details_comp
->str
= g_string_new(NULL
);
206 if (!details_comp
->str
) {
213 destroy_details_comp(details_comp
);
221 void details_finalize(bt_self_component_sink
*comp
)
223 struct details_comp
*details_comp
;
226 details_comp
= bt_self_component_get_data(
227 bt_self_component_sink_as_self_component(comp
));
228 BT_ASSERT(details_comp
);
229 destroy_details_comp(details_comp
);
233 int configure_bool_opt(struct details_comp
*details_comp
,
234 const bt_value
*params
, const char *param_name
,
235 bool default_value
, bool *opt_value
)
238 const bt_value
*value
;
240 *opt_value
= default_value
;
241 value
= bt_value_map_borrow_entry_value_const(params
, param_name
);
243 if (!bt_value_is_bool(value
)) {
244 LOG_WRONG_PARAM_TYPE(param_name
, value
,
250 *opt_value
= (bool) bt_value_bool_get(value
);
258 int configure_details_comp(struct details_comp
*details_comp
,
259 const bt_value
*params
)
262 const bt_value
*value
;
265 /* Colorize output? */
266 details_comp
->cfg
.with_color
= bt_common_colors_supported();
267 value
= bt_value_map_borrow_entry_value_const(params
, color_param_name
);
269 if (!bt_value_is_string(value
)) {
270 LOG_WRONG_PARAM_TYPE(color_param_name
, value
,
271 BT_VALUE_TYPE_STRING
);
275 str
= bt_value_string_get(value
);
277 if (strcmp(str
, "never") == 0) {
278 details_comp
->cfg
.with_color
= false;
279 } else if (strcmp(str
, "auto") == 0) {
280 details_comp
->cfg
.with_color
=
281 bt_common_colors_supported();
282 } else if (strcmp(str
, "always") == 0) {
283 details_comp
->cfg
.with_color
= true;
285 BT_LOGE("Invalid `%s` parameter: unknown value "
286 "(expecting `never`, `auto`, or `always`): "
287 "value=\"%s\"", color_param_name
, str
);
292 /* With metadata objects? */
293 ret
= configure_bool_opt(details_comp
, params
, with_metadata_param_name
,
294 true, &details_comp
->cfg
.with_meta
);
300 ret
= configure_bool_opt(details_comp
, params
, compact_param_name
,
301 false, &details_comp
->cfg
.compact
);
307 ret
= configure_bool_opt(details_comp
, params
, with_time_param_name
,
308 true, &details_comp
->cfg
.with_time
);
313 /* With trace class name? */
314 ret
= configure_bool_opt(details_comp
, params
,
315 with_trace_class_name_param_name
,
316 true, &details_comp
->cfg
.with_trace_class_name
);
321 /* With trace name? */
322 ret
= configure_bool_opt(details_comp
, params
,
323 with_trace_name_param_name
,
324 true, &details_comp
->cfg
.with_trace_name
);
329 /* With stream class name? */
330 ret
= configure_bool_opt(details_comp
, params
,
331 with_stream_class_name_param_name
,
332 true, &details_comp
->cfg
.with_stream_class_name
);
337 /* With stream name? */
338 ret
= configure_bool_opt(details_comp
, params
,
339 with_stream_name_param_name
,
340 true, &details_comp
->cfg
.with_stream_name
);
346 ret
= configure_bool_opt(details_comp
, params
,
347 with_uuid_param_name
, true, &details_comp
->cfg
.with_uuid
);
362 void log_configuration(bt_self_component_sink
*comp
,
363 struct details_comp
*details_comp
)
365 BT_LOGI("Configuration for `sink.text.details` component `%s`:",
366 bt_component_get_name(bt_self_component_as_component(
367 bt_self_component_sink_as_self_component(comp
))));
368 BT_LOGI(" Colorize output: %d", details_comp
->cfg
.with_color
);
369 BT_LOGI(" Compact: %d", details_comp
->cfg
.compact
);
370 BT_LOGI(" With metadata: %d", details_comp
->cfg
.with_meta
);
371 BT_LOGI(" With time: %d", details_comp
->cfg
.with_time
);
372 BT_LOGI(" With trace class name: %d",
373 details_comp
->cfg
.with_trace_class_name
);
374 BT_LOGI(" With trace name: %d", details_comp
->cfg
.with_trace_name
);
375 BT_LOGI(" With stream class name: %d",
376 details_comp
->cfg
.with_stream_class_name
);
377 BT_LOGI(" With stream name: %d", details_comp
->cfg
.with_stream_name
);
378 BT_LOGI(" With UUID: %d", details_comp
->cfg
.with_uuid
);
382 bt_self_component_status
details_init(bt_self_component_sink
*comp
,
383 const bt_value
*params
,
384 __attribute__((unused
)) void *init_method_data
)
386 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
387 struct details_comp
*details_comp
= NULL
;
389 status
= bt_self_component_sink_add_input_port(comp
, in_port_name
,
391 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
395 details_comp
= create_details_comp();
397 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
401 if (configure_details_comp(details_comp
, params
)) {
402 BT_LOGE_STR("Failed to configure component.");
406 log_configuration(comp
, details_comp
);
407 bt_self_component_set_data(
408 bt_self_component_sink_as_self_component(comp
), details_comp
);
412 if (status
== BT_SELF_COMPONENT_STATUS_OK
) {
413 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
416 destroy_details_comp(details_comp
);
423 bt_self_component_status
details_graph_is_configured(
424 bt_self_component_sink
*comp
)
426 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
427 bt_self_component_port_input_message_iterator
*iterator
;
428 struct details_comp
*details_comp
;
429 bt_self_component_port_input
*in_port
;
431 details_comp
= bt_self_component_get_data(
432 bt_self_component_sink_as_self_component(comp
));
433 BT_ASSERT(details_comp
);
434 in_port
= bt_self_component_sink_borrow_input_port_by_name(comp
,
436 if (!bt_port_is_connected(bt_port_input_as_port_const(
437 bt_self_component_port_input_as_port_input(in_port
)))) {
438 BT_LOGE("Single input port is not connected: "
439 "port-name=\"%s\"", in_port_name
);
440 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
444 iterator
= bt_self_component_port_input_message_iterator_create(
445 bt_self_component_sink_borrow_input_port_by_name(comp
,
448 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
452 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
453 details_comp
->msg_iter
, iterator
);
460 bt_self_component_status
details_consume(bt_self_component_sink
*comp
)
462 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
463 bt_message_array_const msgs
;
465 struct details_comp
*details_comp
;
466 bt_message_iterator_status it_ret
;
469 details_comp
= bt_self_component_get_data(
470 bt_self_component_sink_as_self_component(comp
));
471 BT_ASSERT(details_comp
);
472 BT_ASSERT(details_comp
->msg_iter
);
474 /* Consume messages */
475 it_ret
= bt_self_component_port_input_message_iterator_next(
476 details_comp
->msg_iter
, &msgs
, &count
);
478 case BT_MESSAGE_ITERATOR_STATUS_OK
:
479 ret
= BT_SELF_COMPONENT_STATUS_OK
;
481 for (i
= 0; i
< count
; i
++) {
482 int print_ret
= details_write_message(details_comp
,
486 for (; i
< count
; i
++) {
487 /* Put all remaining messages */
488 bt_message_put_ref(msgs
[i
]);
491 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
495 /* Print output buffer to standard output and flush */
496 if (details_comp
->str
->len
> 0) {
497 printf("%s", details_comp
->str
->str
);
499 details_comp
->printed_something
= true;
502 /* Put this message */
503 bt_message_put_ref(msgs
[i
]);
507 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
508 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
510 case BT_MESSAGE_ITERATOR_STATUS_END
:
511 ret
= BT_SELF_COMPONENT_STATUS_END
;
513 case BT_MESSAGE_ITERATOR_STATUS_ERROR
:
514 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
516 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
517 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;