2 * SPDX-License-Identifier: MIT
4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 #define BT_COMP_LOG_SELF_COMP (pretty->self_comp)
9 #define BT_LOG_OUTPUT_LEVEL (pretty->log_level)
10 #define BT_LOG_TAG "PLUGIN/SINK.TEXT.PRETTY"
11 #include "logging/comp-logging.h"
13 #include <babeltrace2/babeltrace.h>
14 #include "compat/compiler.h"
15 #include "common/common.h"
20 #include "common/assert.h"
21 #include "plugins/common/param-validation/param-validation.h"
26 const char * const in_port_name
= "in";
29 void destroy_pretty_data(struct pretty_component
*pretty
)
35 bt_message_iterator_put_ref(pretty
->iterator
);
38 (void) g_string_free(pretty
->string
, TRUE
);
41 if (pretty
->tmp_string
) {
42 (void) g_string_free(pretty
->tmp_string
, TRUE
);
45 if (pretty
->out
!= stdout
) {
48 ret
= fclose(pretty
->out
);
50 perror("close output file");
53 g_free(pretty
->options
.output_path
);
61 struct pretty_component
*create_pretty(void)
63 struct pretty_component
*pretty
;
65 pretty
= g_new0(struct pretty_component
, 1);
69 pretty
->string
= g_string_new("");
70 if (!pretty
->string
) {
73 pretty
->tmp_string
= g_string_new("");
74 if (!pretty
->tmp_string
) {
86 void pretty_finalize(bt_self_component_sink
*comp
)
89 bt_self_component_get_data(
90 bt_self_component_sink_as_self_component(comp
)));
94 bt_message_iterator_class_next_method_status
handle_message(
95 struct pretty_component
*pretty
,
96 const bt_message
*message
)
98 bt_message_iterator_class_next_method_status ret
=
99 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
101 BT_ASSERT_DBG(pretty
);
103 switch (bt_message_get_type(message
)) {
104 case BT_MESSAGE_TYPE_EVENT
:
105 if (pretty_print_event(pretty
, message
)) {
106 ret
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
109 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
110 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
111 if (pretty_print_discarded_items(pretty
, message
)) {
112 ret
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
123 bt_component_class_sink_graph_is_configured_method_status
124 pretty_graph_is_configured(bt_self_component_sink
*self_comp_sink
)
126 bt_component_class_sink_graph_is_configured_method_status status
;
127 bt_message_iterator_create_from_sink_component_status
129 struct pretty_component
*pretty
;
130 bt_self_component
*self_comp
=
131 bt_self_component_sink_as_self_component(self_comp_sink
);
132 bt_self_component_port_input
*in_port
;
134 pretty
= bt_self_component_get_data(self_comp
);
136 BT_ASSERT(!pretty
->iterator
);
138 in_port
= bt_self_component_sink_borrow_input_port_by_name(self_comp_sink
,
140 if (!bt_port_is_connected(bt_port_input_as_port_const(
141 bt_self_component_port_input_as_port_input(in_port
)))) {
142 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Single input port is not connected: "
143 "port-name=\"%s\"", in_port_name
);
144 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR
;
148 msg_iter_status
= bt_message_iterator_create_from_sink_component(
149 self_comp_sink
, in_port
, &pretty
->iterator
);
150 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
151 status
= (int) msg_iter_status
;
155 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
162 bt_component_class_sink_consume_method_status
pretty_consume(
163 bt_self_component_sink
*comp
)
165 bt_component_class_sink_consume_method_status status
;
166 bt_message_array_const msgs
;
167 bt_message_iterator
*it
;
168 struct pretty_component
*pretty
= bt_self_component_get_data(
169 bt_self_component_sink_as_self_component(comp
));
170 bt_message_iterator_next_status next_status
;
174 it
= pretty
->iterator
;
175 next_status
= bt_message_iterator_next(it
,
177 if (next_status
!= BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
) {
178 status
= (int) next_status
;
182 for (i
= 0; i
< count
; i
++) {
183 status
= (int) handle_message(pretty
, msgs
[i
]);
188 bt_message_put_ref(msgs
[i
]);
192 for (; i
< count
; i
++) {
193 bt_message_put_ref(msgs
[i
]);
200 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
202 const bt_value
*value
= NULL
;
205 value
= bt_value_map_borrow_entry_value_const(params
, key
);
210 str
= bt_value_string_get(value
);
211 *option
= g_strdup(str
);
218 * Apply parameter with key `key` to `option`. Use `def` as the value, if
219 * the parameter is not specified.
223 void apply_one_bool_with_default(const char *key
, const bt_value
*params
,
224 bool *option
, bool def
)
226 const bt_value
*value
;
228 value
= bt_value_map_borrow_entry_value_const(params
, key
);
230 bt_bool bool_val
= bt_value_bool_get(value
);
232 *option
= (bool) bool_val
;
239 void apply_one_bool_if_specified(const char *key
, const bt_value
*params
, bool *option
)
241 const bt_value
*value
;
243 value
= bt_value_map_borrow_entry_value_const(params
, key
);
245 bt_bool bool_val
= bt_value_bool_get(value
);
247 *option
= (bool) bool_val
;
252 int open_output_file(struct pretty_component
*pretty
)
256 if (!pretty
->options
.output_path
) {
260 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
274 static const char *color_choices
[] = { "never", "auto", "always", NULL
};
275 static const char *show_hide_choices
[] = { "show", "hide", NULL
};
278 struct bt_param_validation_map_value_entry_descr pretty_params
[] = {
279 { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
280 .choices
= color_choices
,
282 { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
283 { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
284 { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
285 { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
286 { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
287 { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
288 { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
290 { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
291 .choices
= show_hide_choices
,
293 { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
294 { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
295 { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
296 { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
298 { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
299 .choices
= show_hide_choices
,
301 { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
302 { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
303 { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
304 { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
305 { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
306 { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
307 { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
308 { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
309 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
313 bt_component_class_initialize_method_status
apply_params(
314 struct pretty_component
*pretty
, const bt_value
*params
)
317 const bt_value
*value
;
318 bt_component_class_initialize_method_status status
;
319 enum bt_param_validation_status validation_status
;
320 gchar
*validate_error
= NULL
;
322 validation_status
= bt_param_validation_validate(params
,
323 pretty_params
, &validate_error
);
324 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
325 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
327 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
328 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
329 BT_COMP_LOGE_APPEND_CAUSE(pretty
->self_comp
, "%s", validate_error
);
333 /* Known parameters. */
334 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
335 value
= bt_value_map_borrow_entry_value_const(params
, "color");
337 const char *color
= bt_value_string_get(value
);
339 if (strcmp(color
, "never") == 0) {
340 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
341 } else if (strcmp(color
, "auto") == 0) {
342 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
344 BT_ASSERT(strcmp(color
, "always") == 0);
345 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
349 apply_one_string("path", params
, &pretty
->options
.output_path
);
350 ret
= open_output_file(pretty
);
352 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
356 apply_one_bool_with_default("no-delta", params
,
357 &pretty
->options
.print_delta_field
, false);
359 pretty
->options
.print_delta_field
= !pretty
->options
.print_delta_field
;
361 apply_one_bool_with_default("clock-cycles", params
,
362 &pretty
->options
.print_timestamp_cycles
, false);
364 apply_one_bool_with_default("clock-seconds", params
,
365 &pretty
->options
.clock_seconds
, false);
367 apply_one_bool_with_default("clock-date", params
,
368 &pretty
->options
.clock_date
, false);
370 apply_one_bool_with_default("clock-gmt", params
,
371 &pretty
->options
.clock_gmt
, false);
373 apply_one_bool_with_default("verbose", params
,
374 &pretty
->options
.verbose
, false);
377 value
= bt_value_map_borrow_entry_value_const(params
, "name-default");
379 const char *str
= bt_value_string_get(value
);
381 if (strcmp(str
, "show") == 0) {
382 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
384 BT_ASSERT(strcmp(str
, "hide") == 0);
385 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
388 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
391 switch (pretty
->options
.name_default
) {
392 case PRETTY_DEFAULT_UNSET
:
393 pretty
->options
.print_payload_field_names
= true;
394 pretty
->options
.print_context_field_names
= true;
395 pretty
->options
.print_header_field_names
= false;
396 pretty
->options
.print_scope_field_names
= false;
398 case PRETTY_DEFAULT_SHOW
:
399 pretty
->options
.print_payload_field_names
= true;
400 pretty
->options
.print_context_field_names
= true;
401 pretty
->options
.print_header_field_names
= true;
402 pretty
->options
.print_scope_field_names
= true;
404 case PRETTY_DEFAULT_HIDE
:
405 pretty
->options
.print_payload_field_names
= false;
406 pretty
->options
.print_context_field_names
= false;
407 pretty
->options
.print_header_field_names
= false;
408 pretty
->options
.print_scope_field_names
= false;
414 apply_one_bool_if_specified("name-payload", params
,
415 &pretty
->options
.print_payload_field_names
);
417 apply_one_bool_if_specified("name-context", params
,
418 &pretty
->options
.print_context_field_names
);
420 apply_one_bool_if_specified("name-header", params
,
421 &pretty
->options
.print_header_field_names
);
423 apply_one_bool_if_specified("name-scope", params
,
424 &pretty
->options
.print_scope_field_names
);
427 value
= bt_value_map_borrow_entry_value_const(params
, "field-default");
429 const char *str
= bt_value_string_get(value
);
431 if (strcmp(str
, "show") == 0) {
432 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
434 BT_ASSERT(strcmp(str
, "hide") == 0);
435 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
438 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
441 switch (pretty
->options
.field_default
) {
442 case PRETTY_DEFAULT_UNSET
:
443 pretty
->options
.print_trace_field
= false;
444 pretty
->options
.print_trace_hostname_field
= true;
445 pretty
->options
.print_trace_domain_field
= false;
446 pretty
->options
.print_trace_procname_field
= true;
447 pretty
->options
.print_trace_vpid_field
= true;
448 pretty
->options
.print_loglevel_field
= false;
449 pretty
->options
.print_emf_field
= false;
450 pretty
->options
.print_callsite_field
= false;
452 case PRETTY_DEFAULT_SHOW
:
453 pretty
->options
.print_trace_field
= true;
454 pretty
->options
.print_trace_hostname_field
= true;
455 pretty
->options
.print_trace_domain_field
= true;
456 pretty
->options
.print_trace_procname_field
= true;
457 pretty
->options
.print_trace_vpid_field
= true;
458 pretty
->options
.print_loglevel_field
= true;
459 pretty
->options
.print_emf_field
= true;
460 pretty
->options
.print_callsite_field
= true;
462 case PRETTY_DEFAULT_HIDE
:
463 pretty
->options
.print_trace_field
= false;
464 pretty
->options
.print_trace_hostname_field
= false;
465 pretty
->options
.print_trace_domain_field
= false;
466 pretty
->options
.print_trace_procname_field
= false;
467 pretty
->options
.print_trace_vpid_field
= false;
468 pretty
->options
.print_loglevel_field
= false;
469 pretty
->options
.print_emf_field
= false;
470 pretty
->options
.print_callsite_field
= false;
476 apply_one_bool_if_specified("field-trace", params
,
477 &pretty
->options
.print_trace_field
);
479 apply_one_bool_if_specified("field-trace:hostname", params
,
480 &pretty
->options
.print_trace_hostname_field
);
482 apply_one_bool_if_specified("field-trace:domain", params
,
483 &pretty
->options
.print_trace_domain_field
);
485 apply_one_bool_if_specified("field-trace:procname", params
,
486 &pretty
->options
.print_trace_procname_field
);
488 apply_one_bool_if_specified("field-trace:vpid", params
,
489 &pretty
->options
.print_trace_vpid_field
);
491 apply_one_bool_if_specified("field-loglevel", params
,
492 &pretty
->options
.print_loglevel_field
);
494 apply_one_bool_if_specified("field-emf", params
,
495 &pretty
->options
.print_emf_field
);
497 apply_one_bool_if_specified("field-callsite", params
,
498 &pretty
->options
.print_callsite_field
);
501 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
504 g_free(validate_error
);
510 void set_use_colors(struct pretty_component
*pretty
)
512 switch (pretty
->options
.color
) {
513 case PRETTY_COLOR_OPT_ALWAYS
:
514 pretty
->use_colors
= true;
516 case PRETTY_COLOR_OPT_AUTO
:
517 pretty
->use_colors
= pretty
->out
== stdout
&&
518 bt_common_colors_supported();
520 case PRETTY_COLOR_OPT_NEVER
:
521 pretty
->use_colors
= false;
527 bt_component_class_initialize_method_status
pretty_init(
528 bt_self_component_sink
*self_comp_sink
,
529 bt_self_component_sink_configuration
*config
,
530 const bt_value
*params
,
531 __attribute__((unused
)) void *init_method_data
)
533 bt_component_class_initialize_method_status status
;
534 bt_self_component_add_port_status add_port_status
;
535 struct pretty_component
*pretty
= create_pretty();
536 bt_self_component
*self_comp
=
537 bt_self_component_sink_as_self_component(self_comp_sink
);
538 const bt_component
*comp
= bt_self_component_as_component(self_comp
);
539 bt_logging_level log_level
= bt_component_get_logging_level(comp
);
542 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
546 pretty
->self_comp
= self_comp
;
547 pretty
->log_level
= log_level
;
549 add_port_status
= bt_self_component_sink_add_input_port(self_comp_sink
,
550 in_port_name
, NULL
, NULL
);
551 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
552 status
= (int) add_port_status
;
556 pretty
->out
= stdout
;
557 pretty
->err
= stderr
;
559 pretty
->delta_cycles
= -1ULL;
560 pretty
->last_cycles_timestamp
= -1ULL;
562 pretty
->delta_real_timestamp
= -1ULL;
563 pretty
->last_real_timestamp
= -1ULL;
565 status
= apply_params(pretty
, params
);
566 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
570 set_use_colors(pretty
);
571 bt_self_component_set_data(self_comp
, pretty
);
573 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
577 destroy_pretty_data(pretty
);