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 LOG_WRONG_PARAM_TYPE(_name, _value, _exp_type) \
40 BT_COMP_LOGE("Wrong `%s` parameter type: type=%s, " \
42 (_name), bt_common_value_type_string( \
43 bt_value_get_type(_value)), \
44 bt_common_value_type_string(_exp_type)); \
47 #define IN_PORT_NAME "in"
48 #define COLOR_PARAM_NAME "color"
49 #define WITH_METADATA_PARAM_NAME "with-metadata"
50 #define WITH_DATA_PARAM_NAME "with-data"
51 #define WITH_TIME_PARAM_NAME "with-time"
52 #define WITH_TRACE_NAME_PARAM_NAME "with-trace-name"
53 #define WITH_STREAM_CLASS_NAME_PARAM_NAME "with-stream-class-name"
54 #define WITH_STREAM_NAME_PARAM_NAME "with-stream-name"
55 #define WITH_UUID_PARAM_NAME "with-uuid"
56 #define COMPACT_PARAM_NAME "compact"
59 void details_destroy_details_trace_class_meta(
60 struct details_trace_class_meta
*details_tc_meta
)
62 if (!details_tc_meta
) {
66 if (details_tc_meta
->objects
) {
67 g_hash_table_destroy(details_tc_meta
->objects
);
68 details_tc_meta
->objects
= NULL
;
71 g_free(details_tc_meta
);
78 struct details_trace_class_meta
*details_create_details_trace_class_meta(void)
80 struct details_trace_class_meta
*details_tc_meta
=
81 g_new0(struct details_trace_class_meta
, 1);
83 if (!details_tc_meta
) {
87 details_tc_meta
->objects
= g_hash_table_new(
88 g_direct_hash
, g_direct_equal
);
89 if (!details_tc_meta
->objects
) {
90 details_destroy_details_trace_class_meta(details_tc_meta
);
91 details_tc_meta
= NULL
;
95 details_tc_meta
->tc_destruction_listener_id
= UINT64_C(-1);
98 return details_tc_meta
;
102 void destroy_details_comp(struct details_comp
*details_comp
)
111 if (details_comp
->meta
) {
113 * Remove trace class destruction listeners, because
114 * otherwise, when they are called, `details_comp`
115 * (their user data) won't exist anymore (we're
116 * destroying it here).
118 g_hash_table_iter_init(&iter
, details_comp
->meta
);
120 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
121 struct details_trace_class_meta
*details_tc_meta
=
124 if (details_tc_meta
->tc_destruction_listener_id
!=
126 if (bt_trace_class_remove_destruction_listener(
128 details_tc_meta
->tc_destruction_listener_id
)) {
129 bt_current_thread_clear_error();
134 g_hash_table_destroy(details_comp
->meta
);
135 details_comp
->meta
= NULL
;
138 if (details_comp
->traces
) {
140 * Remove trace destruction listeners, because
141 * otherwise, when they are called, `details_comp` won't
142 * exist anymore (we're destroying it here).
144 g_hash_table_iter_init(&iter
, details_comp
->traces
);
146 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
147 struct details_trace
*details_trace
= value
;
149 if (bt_trace_remove_destruction_listener(
151 details_trace
->trace_destruction_listener_id
)) {
152 bt_current_thread_clear_error();
156 g_hash_table_destroy(details_comp
->traces
);
157 details_comp
->traces
= NULL
;
160 if (details_comp
->str
) {
161 g_string_free(details_comp
->str
, TRUE
);
162 details_comp
->str
= NULL
;
165 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
166 details_comp
->msg_iter
);
167 g_free(details_comp
);
174 struct details_comp
*create_details_comp(
175 bt_self_component_sink
*self_comp_sink
)
177 struct details_comp
*details_comp
= g_new0(struct details_comp
, 1);
178 bt_self_component
*self_comp
=
179 bt_self_component_sink_as_self_component(self_comp_sink
);
185 details_comp
->log_level
= bt_component_get_logging_level(
186 bt_self_component_as_component(self_comp
));
187 details_comp
->self_comp
= self_comp
;
188 details_comp
->meta
= g_hash_table_new_full(g_direct_hash
,
189 g_direct_equal
, NULL
,
190 (GDestroyNotify
) details_destroy_details_trace_class_meta
);
191 if (!details_comp
->meta
) {
195 details_comp
->traces
= g_hash_table_new_full(g_direct_hash
,
196 g_direct_equal
, NULL
, g_free
);
197 if (!details_comp
->traces
) {
201 details_comp
->str
= g_string_new(NULL
);
202 if (!details_comp
->str
) {
209 destroy_details_comp(details_comp
);
217 void details_finalize(bt_self_component_sink
*comp
)
219 struct details_comp
*details_comp
;
222 details_comp
= bt_self_component_get_data(
223 bt_self_component_sink_as_self_component(comp
));
224 BT_ASSERT(details_comp
);
225 destroy_details_comp(details_comp
);
229 void configure_bool_opt(struct details_comp
*details_comp
,
230 const bt_value
*params
, const char *param_name
,
231 bool default_value
, bool *opt_value
)
233 const bt_value
*value
;
235 *opt_value
= default_value
;
236 value
= bt_value_map_borrow_entry_value_const(params
, param_name
);
238 *opt_value
= (bool) bt_value_bool_get(value
);
242 static const char *color_choices
[] = { "never", "auto", "always", NULL
};
244 static const struct bt_param_validation_map_value_entry_descr details_params
[] = {
245 { COLOR_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
246 .choices
= color_choices
,
248 { WITH_METADATA_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
249 { WITH_DATA_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
250 { COMPACT_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
251 { WITH_TIME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
252 { WITH_TRACE_NAME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
253 { WITH_STREAM_CLASS_NAME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
254 { WITH_STREAM_NAME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
255 { WITH_UUID_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
256 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
260 bt_component_class_initialize_method_status
configure_details_comp(
261 struct details_comp
*details_comp
,
262 const bt_value
*params
)
264 bt_component_class_initialize_method_status status
;
265 const bt_value
*value
;
267 enum bt_param_validation_status validation_status
;
268 gchar
*validate_error
= NULL
;
270 validation_status
= bt_param_validation_validate(params
,
271 details_params
, &validate_error
);
272 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
273 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
275 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
276 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
277 BT_COMP_LOGE_APPEND_CAUSE(details_comp
->self_comp
,
278 "%s", validate_error
);
282 /* Colorize output? */
283 details_comp
->cfg
.with_color
= bt_common_colors_supported();
284 value
= bt_value_map_borrow_entry_value_const(params
, COLOR_PARAM_NAME
);
286 str
= bt_value_string_get(value
);
288 if (strcmp(str
, "never") == 0) {
289 details_comp
->cfg
.with_color
= false;
290 } else if (strcmp(str
, "auto") == 0) {
291 details_comp
->cfg
.with_color
=
292 bt_common_colors_supported();
294 BT_ASSERT(strcmp(str
, "always") == 0);
296 details_comp
->cfg
.with_color
= true;
300 /* With metadata objects? */
301 configure_bool_opt(details_comp
, params
, WITH_METADATA_PARAM_NAME
,
302 true, &details_comp
->cfg
.with_meta
);
304 /* With data objects? */
305 configure_bool_opt(details_comp
, params
, WITH_DATA_PARAM_NAME
,
306 true, &details_comp
->cfg
.with_data
);
309 configure_bool_opt(details_comp
, params
, COMPACT_PARAM_NAME
,
310 false, &details_comp
->cfg
.compact
);
313 configure_bool_opt(details_comp
, params
, WITH_TIME_PARAM_NAME
,
314 true, &details_comp
->cfg
.with_time
);
316 /* With trace name? */
317 configure_bool_opt(details_comp
, params
,
318 WITH_TRACE_NAME_PARAM_NAME
,
319 true, &details_comp
->cfg
.with_trace_name
);
321 /* With stream class name? */
322 configure_bool_opt(details_comp
, params
,
323 WITH_STREAM_CLASS_NAME_PARAM_NAME
,
324 true, &details_comp
->cfg
.with_stream_class_name
);
326 /* With stream name? */
327 configure_bool_opt(details_comp
, params
,
328 WITH_STREAM_NAME_PARAM_NAME
,
329 true, &details_comp
->cfg
.with_stream_name
);
332 configure_bool_opt(details_comp
, params
,
333 WITH_UUID_PARAM_NAME
, true, &details_comp
->cfg
.with_uuid
);
335 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
339 g_free(validate_error
);
345 void log_configuration(bt_self_component_sink
*comp
,
346 struct details_comp
*details_comp
)
348 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
349 bt_component_get_name(bt_self_component_as_component(
350 bt_self_component_sink_as_self_component(comp
))));
351 BT_COMP_LOGI(" Colorize output: %d", details_comp
->cfg
.with_color
);
352 BT_COMP_LOGI(" Compact: %d", details_comp
->cfg
.compact
);
353 BT_COMP_LOGI(" With metadata: %d", details_comp
->cfg
.with_meta
);
354 BT_COMP_LOGI(" With time: %d", details_comp
->cfg
.with_time
);
355 BT_COMP_LOGI(" With trace name: %d", details_comp
->cfg
.with_trace_name
);
356 BT_COMP_LOGI(" With stream class name: %d",
357 details_comp
->cfg
.with_stream_class_name
);
358 BT_COMP_LOGI(" With stream name: %d", details_comp
->cfg
.with_stream_name
);
359 BT_COMP_LOGI(" With UUID: %d", details_comp
->cfg
.with_uuid
);
363 bt_component_class_initialize_method_status
details_init(
364 bt_self_component_sink
*comp
,
365 bt_self_component_sink_configuration
*config
,
366 const bt_value
*params
,
367 __attribute__((unused
)) void *init_method_data
)
369 bt_component_class_initialize_method_status status
=
370 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
371 bt_self_component_add_port_status add_port_status
;
372 struct details_comp
*details_comp
= NULL
;
374 add_port_status
= bt_self_component_sink_add_input_port(comp
,
375 IN_PORT_NAME
, NULL
, NULL
);
376 switch (add_port_status
) {
377 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
:
378 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
380 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
381 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
383 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
384 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
390 details_comp
= create_details_comp(comp
);
392 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
396 if (configure_details_comp(details_comp
, params
)) {
397 BT_COMP_LOGE_STR("Failed to configure component.");
401 log_configuration(comp
, details_comp
);
402 bt_self_component_set_data(
403 bt_self_component_sink_as_self_component(comp
), details_comp
);
407 if (status
== BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
408 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
411 destroy_details_comp(details_comp
);
418 bt_component_class_sink_graph_is_configured_method_status
419 details_graph_is_configured(bt_self_component_sink
*comp
)
421 bt_component_class_sink_graph_is_configured_method_status status
;
422 bt_self_component_port_input_message_iterator_create_from_sink_component_status
424 bt_self_component_port_input_message_iterator
*iterator
;
425 struct details_comp
*details_comp
;
426 bt_self_component_port_input
*in_port
;
428 details_comp
= bt_self_component_get_data(
429 bt_self_component_sink_as_self_component(comp
));
430 BT_ASSERT(details_comp
);
431 in_port
= bt_self_component_sink_borrow_input_port_by_name(comp
,
433 if (!bt_port_is_connected(bt_port_input_as_port_const(
434 bt_self_component_port_input_as_port_input(in_port
)))) {
435 BT_COMP_LOGE("Single input port is not connected: "
436 "port-name=\"%s\"", IN_PORT_NAME
);
437 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR
;
441 msg_iter_status
= bt_self_component_port_input_message_iterator_create_from_sink_component(
442 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
443 IN_PORT_NAME
), &iterator
);
444 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
445 status
= (int) msg_iter_status
;
449 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
450 details_comp
->msg_iter
, iterator
);
452 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
459 bt_component_class_sink_consume_method_status
460 details_consume(bt_self_component_sink
*comp
)
462 bt_component_class_sink_consume_method_status ret
=
463 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
464 bt_message_array_const msgs
;
466 struct details_comp
*details_comp
;
467 bt_message_iterator_next_status next_status
;
470 details_comp
= bt_self_component_get_data(
471 bt_self_component_sink_as_self_component(comp
));
472 BT_ASSERT_DBG(details_comp
);
473 BT_ASSERT_DBG(details_comp
->msg_iter
);
475 /* Consume messages */
476 next_status
= bt_self_component_port_input_message_iterator_next(
477 details_comp
->msg_iter
, &msgs
, &count
);
478 switch (next_status
) {
479 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
480 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
482 for (i
= 0; i
< count
; i
++) {
483 int print_ret
= details_write_message(details_comp
,
487 for (; i
< count
; i
++) {
488 /* Put all remaining messages */
489 bt_message_put_ref(msgs
[i
]);
492 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
496 /* Print output buffer to standard output and flush */
497 if (details_comp
->str
->len
> 0) {
498 printf("%s", details_comp
->str
->str
);
500 details_comp
->printed_something
= true;
503 /* Put this message */
504 bt_message_put_ref(msgs
[i
]);
508 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
509 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN
;
511 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
512 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
514 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
:
515 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
517 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
518 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR
;