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 BT_COMP_LOGE_APPEND_CAUSE(pretty
->self_comp
,
107 "Failed to print one event.");
108 ret
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
111 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
112 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
113 if (pretty_print_discarded_items(pretty
, message
)) {
114 BT_COMP_LOGE_APPEND_CAUSE(pretty
->self_comp
,
115 "Failed to print discarded items.");
116 ret
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
127 bt_component_class_sink_graph_is_configured_method_status
128 pretty_graph_is_configured(bt_self_component_sink
*self_comp_sink
)
130 bt_component_class_sink_graph_is_configured_method_status status
;
131 bt_message_iterator_create_from_sink_component_status
133 struct pretty_component
*pretty
;
134 bt_self_component
*self_comp
=
135 bt_self_component_sink_as_self_component(self_comp_sink
);
136 bt_self_component_port_input
*in_port
;
138 pretty
= bt_self_component_get_data(self_comp
);
140 BT_ASSERT(!pretty
->iterator
);
142 in_port
= bt_self_component_sink_borrow_input_port_by_name(self_comp_sink
,
144 if (!bt_port_is_connected(bt_port_input_as_port_const(
145 bt_self_component_port_input_as_port_input(in_port
)))) {
146 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Single input port is not connected: "
147 "port-name=\"%s\"", in_port_name
);
148 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR
;
152 msg_iter_status
= bt_message_iterator_create_from_sink_component(
153 self_comp_sink
, in_port
, &pretty
->iterator
);
154 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
155 status
= (int) msg_iter_status
;
159 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
166 bt_component_class_sink_consume_method_status
pretty_consume(
167 bt_self_component_sink
*comp
)
169 bt_component_class_sink_consume_method_status status
;
170 bt_message_array_const msgs
;
171 bt_message_iterator
*it
;
172 struct pretty_component
*pretty
= bt_self_component_get_data(
173 bt_self_component_sink_as_self_component(comp
));
174 bt_message_iterator_next_status next_status
;
178 it
= pretty
->iterator
;
179 next_status
= bt_message_iterator_next(it
,
181 if (next_status
!= BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
) {
182 status
= (int) next_status
;
186 for (i
= 0; i
< count
; i
++) {
187 status
= (int) handle_message(pretty
, msgs
[i
]);
192 bt_message_put_ref(msgs
[i
]);
196 for (; i
< count
; i
++) {
197 bt_message_put_ref(msgs
[i
]);
204 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
206 const bt_value
*value
= NULL
;
209 value
= bt_value_map_borrow_entry_value_const(params
, key
);
214 str
= bt_value_string_get(value
);
215 *option
= g_strdup(str
);
222 * Apply parameter with key `key` to `option`. Use `def` as the value, if
223 * the parameter is not specified.
227 void apply_one_bool_with_default(const char *key
, const bt_value
*params
,
228 bool *option
, bool def
)
230 const bt_value
*value
;
232 value
= bt_value_map_borrow_entry_value_const(params
, key
);
234 bt_bool bool_val
= bt_value_bool_get(value
);
236 *option
= (bool) bool_val
;
243 void apply_one_bool_if_specified(const char *key
, const bt_value
*params
, bool *option
)
245 const bt_value
*value
;
247 value
= bt_value_map_borrow_entry_value_const(params
, key
);
249 bt_bool bool_val
= bt_value_bool_get(value
);
251 *option
= (bool) bool_val
;
256 int open_output_file(struct pretty_component
*pretty
)
260 if (!pretty
->options
.output_path
) {
264 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
278 static const char *color_choices
[] = { "never", "auto", "always", NULL
};
279 static const char *show_hide_choices
[] = { "show", "hide", NULL
};
282 struct bt_param_validation_map_value_entry_descr pretty_params
[] = {
283 { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
284 .choices
= color_choices
,
286 { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
287 { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
288 { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
289 { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
290 { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
291 { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
292 { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
294 { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
295 .choices
= show_hide_choices
,
297 { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
298 { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
299 { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
300 { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
302 { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
303 .choices
= show_hide_choices
,
305 { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
306 { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
307 { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
308 { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
309 { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
310 { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
311 { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
312 { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
313 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
317 bt_component_class_initialize_method_status
apply_params(
318 struct pretty_component
*pretty
, const bt_value
*params
)
321 const bt_value
*value
;
322 bt_component_class_initialize_method_status status
;
323 enum bt_param_validation_status validation_status
;
324 gchar
*validate_error
= NULL
;
326 validation_status
= bt_param_validation_validate(params
,
327 pretty_params
, &validate_error
);
328 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
329 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
331 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
332 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
333 BT_COMP_LOGE_APPEND_CAUSE(pretty
->self_comp
, "%s", validate_error
);
337 /* Known parameters. */
338 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
339 value
= bt_value_map_borrow_entry_value_const(params
, "color");
341 const char *color
= bt_value_string_get(value
);
343 if (strcmp(color
, "never") == 0) {
344 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
345 } else if (strcmp(color
, "auto") == 0) {
346 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
348 BT_ASSERT(strcmp(color
, "always") == 0);
349 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
353 apply_one_string("path", params
, &pretty
->options
.output_path
);
354 ret
= open_output_file(pretty
);
356 BT_COMP_LOGE_APPEND_CAUSE(pretty
->self_comp
,
357 "Failed to open output file: %s", validate_error
);
358 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
362 apply_one_bool_with_default("no-delta", params
,
363 &pretty
->options
.print_delta_field
, false);
365 pretty
->options
.print_delta_field
= !pretty
->options
.print_delta_field
;
367 apply_one_bool_with_default("clock-cycles", params
,
368 &pretty
->options
.print_timestamp_cycles
, false);
370 apply_one_bool_with_default("clock-seconds", params
,
371 &pretty
->options
.clock_seconds
, false);
373 apply_one_bool_with_default("clock-date", params
,
374 &pretty
->options
.clock_date
, false);
376 apply_one_bool_with_default("clock-gmt", params
,
377 &pretty
->options
.clock_gmt
, false);
379 apply_one_bool_with_default("verbose", params
,
380 &pretty
->options
.verbose
, false);
383 value
= bt_value_map_borrow_entry_value_const(params
, "name-default");
385 const char *str
= bt_value_string_get(value
);
387 if (strcmp(str
, "show") == 0) {
388 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
390 BT_ASSERT(strcmp(str
, "hide") == 0);
391 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
394 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
397 switch (pretty
->options
.name_default
) {
398 case PRETTY_DEFAULT_UNSET
:
399 pretty
->options
.print_payload_field_names
= true;
400 pretty
->options
.print_context_field_names
= true;
401 pretty
->options
.print_header_field_names
= false;
402 pretty
->options
.print_scope_field_names
= false;
404 case PRETTY_DEFAULT_SHOW
:
405 pretty
->options
.print_payload_field_names
= true;
406 pretty
->options
.print_context_field_names
= true;
407 pretty
->options
.print_header_field_names
= true;
408 pretty
->options
.print_scope_field_names
= true;
410 case PRETTY_DEFAULT_HIDE
:
411 pretty
->options
.print_payload_field_names
= false;
412 pretty
->options
.print_context_field_names
= false;
413 pretty
->options
.print_header_field_names
= false;
414 pretty
->options
.print_scope_field_names
= false;
420 apply_one_bool_if_specified("name-payload", params
,
421 &pretty
->options
.print_payload_field_names
);
423 apply_one_bool_if_specified("name-context", params
,
424 &pretty
->options
.print_context_field_names
);
426 apply_one_bool_if_specified("name-header", params
,
427 &pretty
->options
.print_header_field_names
);
429 apply_one_bool_if_specified("name-scope", params
,
430 &pretty
->options
.print_scope_field_names
);
433 value
= bt_value_map_borrow_entry_value_const(params
, "field-default");
435 const char *str
= bt_value_string_get(value
);
437 if (strcmp(str
, "show") == 0) {
438 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
440 BT_ASSERT(strcmp(str
, "hide") == 0);
441 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
444 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
447 switch (pretty
->options
.field_default
) {
448 case PRETTY_DEFAULT_UNSET
:
449 pretty
->options
.print_trace_field
= false;
450 pretty
->options
.print_trace_hostname_field
= true;
451 pretty
->options
.print_trace_domain_field
= false;
452 pretty
->options
.print_trace_procname_field
= true;
453 pretty
->options
.print_trace_vpid_field
= true;
454 pretty
->options
.print_loglevel_field
= false;
455 pretty
->options
.print_emf_field
= false;
456 pretty
->options
.print_callsite_field
= false;
458 case PRETTY_DEFAULT_SHOW
:
459 pretty
->options
.print_trace_field
= true;
460 pretty
->options
.print_trace_hostname_field
= true;
461 pretty
->options
.print_trace_domain_field
= true;
462 pretty
->options
.print_trace_procname_field
= true;
463 pretty
->options
.print_trace_vpid_field
= true;
464 pretty
->options
.print_loglevel_field
= true;
465 pretty
->options
.print_emf_field
= true;
466 pretty
->options
.print_callsite_field
= true;
468 case PRETTY_DEFAULT_HIDE
:
469 pretty
->options
.print_trace_field
= false;
470 pretty
->options
.print_trace_hostname_field
= false;
471 pretty
->options
.print_trace_domain_field
= false;
472 pretty
->options
.print_trace_procname_field
= false;
473 pretty
->options
.print_trace_vpid_field
= false;
474 pretty
->options
.print_loglevel_field
= false;
475 pretty
->options
.print_emf_field
= false;
476 pretty
->options
.print_callsite_field
= false;
482 apply_one_bool_if_specified("field-trace", params
,
483 &pretty
->options
.print_trace_field
);
485 apply_one_bool_if_specified("field-trace:hostname", params
,
486 &pretty
->options
.print_trace_hostname_field
);
488 apply_one_bool_if_specified("field-trace:domain", params
,
489 &pretty
->options
.print_trace_domain_field
);
491 apply_one_bool_if_specified("field-trace:procname", params
,
492 &pretty
->options
.print_trace_procname_field
);
494 apply_one_bool_if_specified("field-trace:vpid", params
,
495 &pretty
->options
.print_trace_vpid_field
);
497 apply_one_bool_if_specified("field-loglevel", params
,
498 &pretty
->options
.print_loglevel_field
);
500 apply_one_bool_if_specified("field-emf", params
,
501 &pretty
->options
.print_emf_field
);
503 apply_one_bool_if_specified("field-callsite", params
,
504 &pretty
->options
.print_callsite_field
);
507 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
510 g_free(validate_error
);
516 void set_use_colors(struct pretty_component
*pretty
)
518 switch (pretty
->options
.color
) {
519 case PRETTY_COLOR_OPT_ALWAYS
:
520 pretty
->use_colors
= true;
522 case PRETTY_COLOR_OPT_AUTO
:
523 pretty
->use_colors
= pretty
->out
== stdout
&&
524 bt_common_colors_supported();
526 case PRETTY_COLOR_OPT_NEVER
:
527 pretty
->use_colors
= false;
533 bt_component_class_initialize_method_status
pretty_init(
534 bt_self_component_sink
*self_comp_sink
,
535 bt_self_component_sink_configuration
*config
,
536 const bt_value
*params
,
537 __attribute__((unused
)) void *init_method_data
)
539 bt_component_class_initialize_method_status status
;
540 bt_self_component_add_port_status add_port_status
;
541 struct pretty_component
*pretty
= create_pretty();
542 bt_self_component
*self_comp
=
543 bt_self_component_sink_as_self_component(self_comp_sink
);
544 const bt_component
*comp
= bt_self_component_as_component(self_comp
);
545 bt_logging_level log_level
= bt_component_get_logging_level(comp
);
549 * Don't use BT_COMP_LOGE_APPEND_CAUSE, as `pretty` is not
552 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
553 "Failed to allocate component.");
554 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
555 self_comp
, "Failed to allocate component.");
556 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
560 pretty
->self_comp
= self_comp
;
561 pretty
->log_level
= log_level
;
563 add_port_status
= bt_self_component_sink_add_input_port(self_comp_sink
,
564 in_port_name
, NULL
, NULL
);
565 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
566 status
= (int) add_port_status
;
570 pretty
->out
= stdout
;
571 pretty
->err
= stderr
;
573 pretty
->delta_cycles
= -1ULL;
574 pretty
->last_cycles_timestamp
= -1ULL;
576 pretty
->delta_real_timestamp
= -1ULL;
577 pretty
->last_real_timestamp
= -1ULL;
579 status
= apply_params(pretty
, params
);
580 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
584 set_use_colors(pretty
);
585 bt_self_component_set_data(self_comp
, pretty
);
587 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
591 destroy_pretty_data(pretty
);