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"
28 #include <babeltrace2/babeltrace.h>
30 #include "common/common.h"
31 #include "common/assert.h"
34 #include "plugins/common/param-validation/param-validation.h"
36 #define LOG_WRONG_PARAM_TYPE(_name, _value, _exp_type) \
38 BT_COMP_LOGE("Wrong `%s` parameter type: type=%s, " \
40 (_name), bt_common_value_type_string( \
41 bt_value_get_type(_value)), \
42 bt_common_value_type_string(_exp_type)); \
45 #define IN_PORT_NAME "in"
46 #define COLOR_PARAM_NAME "color"
47 #define WITH_METADATA_PARAM_NAME "with-metadata"
48 #define WITH_DATA_PARAM_NAME "with-data"
49 #define WITH_TIME_PARAM_NAME "with-time"
50 #define WITH_TRACE_NAME_PARAM_NAME "with-trace-name"
51 #define WITH_STREAM_CLASS_NAME_PARAM_NAME "with-stream-class-name"
52 #define WITH_STREAM_NAME_PARAM_NAME "with-stream-name"
53 #define WITH_UUID_PARAM_NAME "with-uuid"
54 #define COMPACT_PARAM_NAME "compact"
57 void details_destroy_details_trace_class_meta(
58 struct details_trace_class_meta
*details_tc_meta
)
60 if (!details_tc_meta
) {
64 if (details_tc_meta
->objects
) {
65 g_hash_table_destroy(details_tc_meta
->objects
);
66 details_tc_meta
->objects
= NULL
;
69 g_free(details_tc_meta
);
76 struct details_trace_class_meta
*details_create_details_trace_class_meta(void)
78 struct details_trace_class_meta
*details_tc_meta
=
79 g_new0(struct details_trace_class_meta
, 1);
81 if (!details_tc_meta
) {
85 details_tc_meta
->objects
= g_hash_table_new(
86 g_direct_hash
, g_direct_equal
);
87 if (!details_tc_meta
->objects
) {
88 details_destroy_details_trace_class_meta(details_tc_meta
);
89 details_tc_meta
= NULL
;
93 details_tc_meta
->tc_destruction_listener_id
= UINT64_C(-1);
96 return details_tc_meta
;
100 void destroy_details_comp(struct details_comp
*details_comp
)
109 if (details_comp
->meta
) {
111 * Remove trace class destruction listeners, because
112 * otherwise, when they are called, `details_comp`
113 * (their user data) won't exist anymore (we're
114 * destroying it here).
116 g_hash_table_iter_init(&iter
, details_comp
->meta
);
118 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
119 struct details_trace_class_meta
*details_tc_meta
=
122 if (details_tc_meta
->tc_destruction_listener_id
!=
124 if (bt_trace_class_remove_destruction_listener(
126 details_tc_meta
->tc_destruction_listener_id
)) {
127 bt_current_thread_clear_error();
132 g_hash_table_destroy(details_comp
->meta
);
133 details_comp
->meta
= NULL
;
136 if (details_comp
->traces
) {
138 * Remove trace destruction listeners, because
139 * otherwise, when they are called, `details_comp` won't
140 * exist anymore (we're destroying it here).
142 g_hash_table_iter_init(&iter
, details_comp
->traces
);
144 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
145 struct details_trace
*details_trace
= value
;
147 if (bt_trace_remove_destruction_listener(
149 details_trace
->trace_destruction_listener_id
)) {
150 bt_current_thread_clear_error();
154 g_hash_table_destroy(details_comp
->traces
);
155 details_comp
->traces
= NULL
;
158 if (details_comp
->str
) {
159 g_string_free(details_comp
->str
, TRUE
);
160 details_comp
->str
= NULL
;
163 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
164 details_comp
->msg_iter
);
165 g_free(details_comp
);
172 struct details_comp
*create_details_comp(
173 bt_self_component_sink
*self_comp_sink
)
175 struct details_comp
*details_comp
= g_new0(struct details_comp
, 1);
176 bt_self_component
*self_comp
=
177 bt_self_component_sink_as_self_component(self_comp_sink
);
183 details_comp
->log_level
= bt_component_get_logging_level(
184 bt_self_component_as_component(self_comp
));
185 details_comp
->self_comp
= self_comp
;
186 details_comp
->meta
= g_hash_table_new_full(g_direct_hash
,
187 g_direct_equal
, NULL
,
188 (GDestroyNotify
) details_destroy_details_trace_class_meta
);
189 if (!details_comp
->meta
) {
193 details_comp
->traces
= g_hash_table_new_full(g_direct_hash
,
194 g_direct_equal
, NULL
, g_free
);
195 if (!details_comp
->traces
) {
199 details_comp
->str
= g_string_new(NULL
);
200 if (!details_comp
->str
) {
207 destroy_details_comp(details_comp
);
215 void details_finalize(bt_self_component_sink
*comp
)
217 struct details_comp
*details_comp
;
220 details_comp
= bt_self_component_get_data(
221 bt_self_component_sink_as_self_component(comp
));
222 BT_ASSERT(details_comp
);
223 destroy_details_comp(details_comp
);
227 void configure_bool_opt(struct details_comp
*details_comp
,
228 const bt_value
*params
, const char *param_name
,
229 bool default_value
, bool *opt_value
)
231 const bt_value
*value
;
233 *opt_value
= default_value
;
234 value
= bt_value_map_borrow_entry_value_const(params
, param_name
);
236 *opt_value
= (bool) bt_value_bool_get(value
);
240 static const char *color_choices
[] = { "never", "auto", "always", NULL
};
242 static const struct bt_param_validation_map_value_entry_descr details_params
[] = {
243 { COLOR_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
244 .choices
= color_choices
,
246 { WITH_METADATA_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
247 { WITH_DATA_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
248 { COMPACT_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
249 { WITH_TIME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
250 { WITH_TRACE_NAME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
251 { WITH_STREAM_CLASS_NAME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
252 { WITH_STREAM_NAME_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
253 { WITH_UUID_PARAM_NAME
, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
254 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
258 bt_component_class_initialize_method_status
configure_details_comp(
259 struct details_comp
*details_comp
,
260 const bt_value
*params
)
262 bt_component_class_initialize_method_status status
;
263 const bt_value
*value
;
265 enum bt_param_validation_status validation_status
;
266 gchar
*validate_error
= NULL
;
268 validation_status
= bt_param_validation_validate(params
,
269 details_params
, &validate_error
);
270 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
271 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
273 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
274 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
275 BT_COMP_LOGE_APPEND_CAUSE(details_comp
->self_comp
,
276 "%s", validate_error
);
280 /* Colorize output? */
281 details_comp
->cfg
.with_color
= bt_common_colors_supported();
282 value
= bt_value_map_borrow_entry_value_const(params
, COLOR_PARAM_NAME
);
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();
292 BT_ASSERT(strcmp(str
, "always") == 0);
294 details_comp
->cfg
.with_color
= true;
298 /* With metadata objects? */
299 configure_bool_opt(details_comp
, params
, WITH_METADATA_PARAM_NAME
,
300 true, &details_comp
->cfg
.with_meta
);
302 /* With data objects? */
303 configure_bool_opt(details_comp
, params
, WITH_DATA_PARAM_NAME
,
304 true, &details_comp
->cfg
.with_data
);
307 configure_bool_opt(details_comp
, params
, COMPACT_PARAM_NAME
,
308 false, &details_comp
->cfg
.compact
);
311 configure_bool_opt(details_comp
, params
, WITH_TIME_PARAM_NAME
,
312 true, &details_comp
->cfg
.with_time
);
314 /* With trace name? */
315 configure_bool_opt(details_comp
, params
,
316 WITH_TRACE_NAME_PARAM_NAME
,
317 true, &details_comp
->cfg
.with_trace_name
);
319 /* With stream class name? */
320 configure_bool_opt(details_comp
, params
,
321 WITH_STREAM_CLASS_NAME_PARAM_NAME
,
322 true, &details_comp
->cfg
.with_stream_class_name
);
324 /* With stream name? */
325 configure_bool_opt(details_comp
, params
,
326 WITH_STREAM_NAME_PARAM_NAME
,
327 true, &details_comp
->cfg
.with_stream_name
);
330 configure_bool_opt(details_comp
, params
,
331 WITH_UUID_PARAM_NAME
, true, &details_comp
->cfg
.with_uuid
);
333 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
337 g_free(validate_error
);
343 void log_configuration(bt_self_component_sink
*comp
,
344 struct details_comp
*details_comp
)
346 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
347 bt_component_get_name(bt_self_component_as_component(
348 bt_self_component_sink_as_self_component(comp
))));
349 BT_COMP_LOGI(" Colorize output: %d", details_comp
->cfg
.with_color
);
350 BT_COMP_LOGI(" Compact: %d", details_comp
->cfg
.compact
);
351 BT_COMP_LOGI(" With metadata: %d", details_comp
->cfg
.with_meta
);
352 BT_COMP_LOGI(" With time: %d", details_comp
->cfg
.with_time
);
353 BT_COMP_LOGI(" With trace name: %d", details_comp
->cfg
.with_trace_name
);
354 BT_COMP_LOGI(" With stream class name: %d",
355 details_comp
->cfg
.with_stream_class_name
);
356 BT_COMP_LOGI(" With stream name: %d", details_comp
->cfg
.with_stream_name
);
357 BT_COMP_LOGI(" With UUID: %d", details_comp
->cfg
.with_uuid
);
361 bt_component_class_initialize_method_status
details_init(
362 bt_self_component_sink
*comp
,
363 bt_self_component_sink_configuration
*config
,
364 const bt_value
*params
,
365 __attribute__((unused
)) void *init_method_data
)
367 bt_component_class_initialize_method_status status
=
368 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
369 bt_self_component_add_port_status add_port_status
;
370 struct details_comp
*details_comp
= NULL
;
372 add_port_status
= bt_self_component_sink_add_input_port(comp
,
373 IN_PORT_NAME
, NULL
, NULL
);
374 switch (add_port_status
) {
375 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
:
376 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
378 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
379 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
381 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
382 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
388 details_comp
= create_details_comp(comp
);
390 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
394 if (configure_details_comp(details_comp
, params
)) {
395 BT_COMP_LOGE_STR("Failed to configure component.");
399 log_configuration(comp
, details_comp
);
400 bt_self_component_set_data(
401 bt_self_component_sink_as_self_component(comp
), details_comp
);
405 if (status
== BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
406 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
409 destroy_details_comp(details_comp
);
416 bt_component_class_sink_graph_is_configured_method_status
417 details_graph_is_configured(bt_self_component_sink
*comp
)
419 bt_component_class_sink_graph_is_configured_method_status status
;
420 bt_self_component_port_input_message_iterator_create_from_sink_component_status
422 bt_self_component_port_input_message_iterator
*iterator
;
423 struct details_comp
*details_comp
;
424 bt_self_component_port_input
*in_port
;
426 details_comp
= bt_self_component_get_data(
427 bt_self_component_sink_as_self_component(comp
));
428 BT_ASSERT(details_comp
);
429 in_port
= bt_self_component_sink_borrow_input_port_by_name(comp
,
431 if (!bt_port_is_connected(bt_port_input_as_port_const(
432 bt_self_component_port_input_as_port_input(in_port
)))) {
433 BT_COMP_LOGE("Single input port is not connected: "
434 "port-name=\"%s\"", IN_PORT_NAME
);
435 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR
;
439 msg_iter_status
= bt_self_component_port_input_message_iterator_create_from_sink_component(
440 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
441 IN_PORT_NAME
), &iterator
);
442 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
443 status
= (int) msg_iter_status
;
447 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
448 details_comp
->msg_iter
, iterator
);
450 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
457 bt_component_class_sink_consume_method_status
458 details_consume(bt_self_component_sink
*comp
)
460 bt_component_class_sink_consume_method_status ret
=
461 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
462 bt_message_array_const msgs
;
464 struct details_comp
*details_comp
;
465 bt_message_iterator_next_status next_status
;
468 details_comp
= bt_self_component_get_data(
469 bt_self_component_sink_as_self_component(comp
));
470 BT_ASSERT(details_comp
);
471 BT_ASSERT(details_comp
->msg_iter
);
473 /* Consume messages */
474 next_status
= bt_self_component_port_input_message_iterator_next(
475 details_comp
->msg_iter
, &msgs
, &count
);
476 switch (next_status
) {
477 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
478 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
480 for (i
= 0; i
< count
; i
++) {
481 int print_ret
= details_write_message(details_comp
,
485 for (; i
< count
; i
++) {
486 /* Put all remaining messages */
487 bt_message_put_ref(msgs
[i
]);
490 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
494 /* Print output buffer to standard output and flush */
495 if (details_comp
->str
->len
> 0) {
496 printf("%s", details_comp
->str
->str
);
498 details_comp
->printed_something
= true;
501 /* Put this message */
502 bt_message_put_ref(msgs
[i
]);
506 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
507 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN
;
509 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
510 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
512 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
:
513 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
515 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
516 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR
;