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 ret
=
166 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
167 bt_message_array_const msgs
;
168 bt_message_iterator
*it
;
169 struct pretty_component
*pretty
= bt_self_component_get_data(
170 bt_self_component_sink_as_self_component(comp
));
171 bt_message_iterator_next_status next_status
;
175 it
= pretty
->iterator
;
176 next_status
= bt_message_iterator_next(it
,
179 switch (next_status
) {
180 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
182 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
183 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
184 ret
= (int) next_status
;
186 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
187 ret
= (int) next_status
;
188 BT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
192 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
196 BT_ASSERT_DBG(next_status
== BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
);
198 for (i
= 0; i
< count
; i
++) {
199 ret
= (int) handle_message(pretty
, msgs
[i
]);
204 bt_message_put_ref(msgs
[i
]);
208 for (; i
< count
; i
++) {
209 bt_message_put_ref(msgs
[i
]);
216 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
218 const bt_value
*value
= NULL
;
221 value
= bt_value_map_borrow_entry_value_const(params
, key
);
226 str
= bt_value_string_get(value
);
227 *option
= g_strdup(str
);
234 * Apply parameter with key `key` to `option`. Use `def` as the value, if
235 * the parameter is not specified.
239 void apply_one_bool_with_default(const char *key
, const bt_value
*params
,
240 bool *option
, bool def
)
242 const bt_value
*value
;
244 value
= bt_value_map_borrow_entry_value_const(params
, key
);
246 bt_bool bool_val
= bt_value_bool_get(value
);
248 *option
= (bool) bool_val
;
255 void apply_one_bool_if_specified(const char *key
, const bt_value
*params
, bool *option
)
257 const bt_value
*value
;
259 value
= bt_value_map_borrow_entry_value_const(params
, key
);
261 bt_bool bool_val
= bt_value_bool_get(value
);
263 *option
= (bool) bool_val
;
268 int open_output_file(struct pretty_component
*pretty
)
272 if (!pretty
->options
.output_path
) {
276 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
290 static const char *color_choices
[] = { "never", "auto", "always", NULL
};
291 static const char *show_hide_choices
[] = { "show", "hide", NULL
};
294 struct bt_param_validation_map_value_entry_descr pretty_params
[] = {
295 { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
296 .choices
= color_choices
,
298 { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
299 { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
300 { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
301 { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
302 { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
303 { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
304 { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
306 { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
307 .choices
= show_hide_choices
,
309 { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
310 { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
311 { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
312 { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
314 { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
315 .choices
= show_hide_choices
,
317 { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
318 { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
319 { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
320 { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
321 { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
322 { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
323 { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
324 { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
325 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
329 bt_component_class_initialize_method_status
apply_params(
330 struct pretty_component
*pretty
, const bt_value
*params
)
333 const bt_value
*value
;
334 bt_component_class_initialize_method_status status
;
335 enum bt_param_validation_status validation_status
;
336 gchar
*validate_error
= NULL
;
338 validation_status
= bt_param_validation_validate(params
,
339 pretty_params
, &validate_error
);
340 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
341 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
343 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
344 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
345 BT_COMP_LOGE_APPEND_CAUSE(pretty
->self_comp
, "%s", validate_error
);
349 /* Known parameters. */
350 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
351 value
= bt_value_map_borrow_entry_value_const(params
, "color");
353 const char *color
= bt_value_string_get(value
);
355 if (strcmp(color
, "never") == 0) {
356 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
357 } else if (strcmp(color
, "auto") == 0) {
358 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
360 BT_ASSERT(strcmp(color
, "always") == 0);
361 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
365 apply_one_string("path", params
, &pretty
->options
.output_path
);
366 ret
= open_output_file(pretty
);
368 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
372 apply_one_bool_with_default("no-delta", params
,
373 &pretty
->options
.print_delta_field
, false);
375 pretty
->options
.print_delta_field
= !pretty
->options
.print_delta_field
;
377 apply_one_bool_with_default("clock-cycles", params
,
378 &pretty
->options
.print_timestamp_cycles
, false);
380 apply_one_bool_with_default("clock-seconds", params
,
381 &pretty
->options
.clock_seconds
, false);
383 apply_one_bool_with_default("clock-date", params
,
384 &pretty
->options
.clock_date
, false);
386 apply_one_bool_with_default("clock-gmt", params
,
387 &pretty
->options
.clock_gmt
, false);
389 apply_one_bool_with_default("verbose", params
,
390 &pretty
->options
.verbose
, false);
393 value
= bt_value_map_borrow_entry_value_const(params
, "name-default");
395 const char *str
= bt_value_string_get(value
);
397 if (strcmp(str
, "show") == 0) {
398 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
400 BT_ASSERT(strcmp(str
, "hide") == 0);
401 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
404 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
407 switch (pretty
->options
.name_default
) {
408 case PRETTY_DEFAULT_UNSET
:
409 pretty
->options
.print_payload_field_names
= true;
410 pretty
->options
.print_context_field_names
= true;
411 pretty
->options
.print_header_field_names
= false;
412 pretty
->options
.print_scope_field_names
= false;
414 case PRETTY_DEFAULT_SHOW
:
415 pretty
->options
.print_payload_field_names
= true;
416 pretty
->options
.print_context_field_names
= true;
417 pretty
->options
.print_header_field_names
= true;
418 pretty
->options
.print_scope_field_names
= true;
420 case PRETTY_DEFAULT_HIDE
:
421 pretty
->options
.print_payload_field_names
= false;
422 pretty
->options
.print_context_field_names
= false;
423 pretty
->options
.print_header_field_names
= false;
424 pretty
->options
.print_scope_field_names
= false;
430 apply_one_bool_if_specified("name-payload", params
,
431 &pretty
->options
.print_payload_field_names
);
433 apply_one_bool_if_specified("name-context", params
,
434 &pretty
->options
.print_context_field_names
);
436 apply_one_bool_if_specified("name-header", params
,
437 &pretty
->options
.print_header_field_names
);
439 apply_one_bool_if_specified("name-scope", params
,
440 &pretty
->options
.print_scope_field_names
);
443 value
= bt_value_map_borrow_entry_value_const(params
, "field-default");
445 const char *str
= bt_value_string_get(value
);
447 if (strcmp(str
, "show") == 0) {
448 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
450 BT_ASSERT(strcmp(str
, "hide") == 0);
451 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
454 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
457 switch (pretty
->options
.field_default
) {
458 case PRETTY_DEFAULT_UNSET
:
459 pretty
->options
.print_trace_field
= false;
460 pretty
->options
.print_trace_hostname_field
= true;
461 pretty
->options
.print_trace_domain_field
= false;
462 pretty
->options
.print_trace_procname_field
= true;
463 pretty
->options
.print_trace_vpid_field
= true;
464 pretty
->options
.print_loglevel_field
= false;
465 pretty
->options
.print_emf_field
= false;
466 pretty
->options
.print_callsite_field
= false;
468 case PRETTY_DEFAULT_SHOW
:
469 pretty
->options
.print_trace_field
= true;
470 pretty
->options
.print_trace_hostname_field
= true;
471 pretty
->options
.print_trace_domain_field
= true;
472 pretty
->options
.print_trace_procname_field
= true;
473 pretty
->options
.print_trace_vpid_field
= true;
474 pretty
->options
.print_loglevel_field
= true;
475 pretty
->options
.print_emf_field
= true;
476 pretty
->options
.print_callsite_field
= true;
478 case PRETTY_DEFAULT_HIDE
:
479 pretty
->options
.print_trace_field
= false;
480 pretty
->options
.print_trace_hostname_field
= false;
481 pretty
->options
.print_trace_domain_field
= false;
482 pretty
->options
.print_trace_procname_field
= false;
483 pretty
->options
.print_trace_vpid_field
= false;
484 pretty
->options
.print_loglevel_field
= false;
485 pretty
->options
.print_emf_field
= false;
486 pretty
->options
.print_callsite_field
= false;
492 apply_one_bool_if_specified("field-trace", params
,
493 &pretty
->options
.print_trace_field
);
495 apply_one_bool_if_specified("field-trace:hostname", params
,
496 &pretty
->options
.print_trace_hostname_field
);
498 apply_one_bool_if_specified("field-trace:domain", params
,
499 &pretty
->options
.print_trace_domain_field
);
501 apply_one_bool_if_specified("field-trace:procname", params
,
502 &pretty
->options
.print_trace_procname_field
);
504 apply_one_bool_if_specified("field-trace:vpid", params
,
505 &pretty
->options
.print_trace_vpid_field
);
507 apply_one_bool_if_specified("field-loglevel", params
,
508 &pretty
->options
.print_loglevel_field
);
510 apply_one_bool_if_specified("field-emf", params
,
511 &pretty
->options
.print_emf_field
);
513 apply_one_bool_if_specified("field-callsite", params
,
514 &pretty
->options
.print_callsite_field
);
517 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
520 g_free(validate_error
);
526 void set_use_colors(struct pretty_component
*pretty
)
528 switch (pretty
->options
.color
) {
529 case PRETTY_COLOR_OPT_ALWAYS
:
530 pretty
->use_colors
= true;
532 case PRETTY_COLOR_OPT_AUTO
:
533 pretty
->use_colors
= pretty
->out
== stdout
&&
534 bt_common_colors_supported();
536 case PRETTY_COLOR_OPT_NEVER
:
537 pretty
->use_colors
= false;
543 bt_component_class_initialize_method_status
pretty_init(
544 bt_self_component_sink
*self_comp_sink
,
545 bt_self_component_sink_configuration
*config
,
546 const bt_value
*params
,
547 __attribute__((unused
)) void *init_method_data
)
549 bt_component_class_initialize_method_status status
;
550 bt_self_component_add_port_status add_port_status
;
551 struct pretty_component
*pretty
= create_pretty();
552 bt_self_component
*self_comp
=
553 bt_self_component_sink_as_self_component(self_comp_sink
);
554 const bt_component
*comp
= bt_self_component_as_component(self_comp
);
555 bt_logging_level log_level
= bt_component_get_logging_level(comp
);
558 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
562 pretty
->self_comp
= self_comp
;
563 pretty
->log_level
= log_level
;
565 add_port_status
= bt_self_component_sink_add_input_port(self_comp_sink
,
566 in_port_name
, NULL
, NULL
);
567 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
568 status
= (int) add_port_status
;
572 pretty
->out
= stdout
;
573 pretty
->err
= stderr
;
575 pretty
->delta_cycles
= -1ULL;
576 pretty
->last_cycles_timestamp
= -1ULL;
578 pretty
->delta_real_timestamp
= -1ULL;
579 pretty
->last_real_timestamp
= -1ULL;
581 status
= apply_params(pretty
, params
);
582 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
586 set_use_colors(pretty
);
587 bt_self_component_set_data(self_comp
, pretty
);
589 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
593 destroy_pretty_data(pretty
);