2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #define BT_LOG_TAG "PLUGIN-TEXT-PRETTY-SINK"
29 #include <babeltrace2/babeltrace.h>
30 #include "compat/compiler.h"
31 #include "common/common.h"
32 #include "plugins/plugins-common.h"
36 #include "common/assert.h"
40 GQuark stream_packet_context_quarks
[STREAM_PACKET_CONTEXT_QUARKS_LEN
];
43 const char *plugin_options
[] = {
52 "name-default", /* show/hide */
57 "field-default", /* show/hide */
59 "field-trace:hostname",
61 "field-trace:procname",
69 const char * const in_port_name
= "in";
72 void destroy_pretty_data(struct pretty_component
*pretty
)
74 bt_self_component_port_input_message_iterator_put_ref(pretty
->iterator
);
77 (void) g_string_free(pretty
->string
, TRUE
);
80 if (pretty
->tmp_string
) {
81 (void) g_string_free(pretty
->tmp_string
, TRUE
);
84 if (pretty
->out
!= stdout
) {
87 ret
= fclose(pretty
->out
);
89 perror("close output file");
92 g_free(pretty
->options
.output_path
);
97 struct pretty_component
*create_pretty(void)
99 struct pretty_component
*pretty
;
101 pretty
= g_new0(struct pretty_component
, 1);
105 pretty
->string
= g_string_new("");
106 if (!pretty
->string
) {
109 pretty
->tmp_string
= g_string_new("");
110 if (!pretty
->tmp_string
) {
122 void pretty_finalize(bt_self_component_sink
*comp
)
125 bt_self_component_get_data(
126 bt_self_component_sink_as_self_component(comp
)));
130 bt_self_component_status
handle_message(
131 struct pretty_component
*pretty
,
132 const bt_message
*message
)
134 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
138 switch (bt_message_get_type(message
)) {
139 case BT_MESSAGE_TYPE_EVENT
:
140 if (pretty_print_event(pretty
, message
)) {
141 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
144 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
145 BT_LOGD_STR("Message iterator inactivity message.");
147 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
148 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
149 if (pretty_print_discarded_items(pretty
, message
)) {
150 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
161 bt_self_component_status
pretty_graph_is_configured(
162 bt_self_component_sink
*comp
)
164 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
165 struct pretty_component
*pretty
;
167 pretty
= bt_self_component_get_data(
168 bt_self_component_sink_as_self_component(comp
));
170 BT_ASSERT(!pretty
->iterator
);
171 pretty
->iterator
= bt_self_component_port_input_message_iterator_create(
172 bt_self_component_sink_borrow_input_port_by_name(comp
,
174 if (!pretty
->iterator
) {
175 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
182 bt_self_component_status
pretty_consume(
183 bt_self_component_sink
*comp
)
185 bt_self_component_status ret
;
186 bt_message_array_const msgs
;
187 bt_self_component_port_input_message_iterator
*it
;
188 struct pretty_component
*pretty
= bt_self_component_get_data(
189 bt_self_component_sink_as_self_component(comp
));
190 bt_message_iterator_status it_ret
;
194 it
= pretty
->iterator
;
195 it_ret
= bt_self_component_port_input_message_iterator_next(it
,
199 case BT_MESSAGE_ITERATOR_STATUS_OK
:
201 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
202 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
204 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
205 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
207 case BT_MESSAGE_ITERATOR_STATUS_END
:
208 ret
= BT_SELF_COMPONENT_STATUS_END
;
209 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
213 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
217 BT_ASSERT(it_ret
== BT_MESSAGE_ITERATOR_STATUS_OK
);
219 for (i
= 0; i
< count
; i
++) {
220 ret
= handle_message(pretty
, msgs
[i
]);
225 bt_message_put_ref(msgs
[i
]);
229 for (; i
< count
; i
++) {
230 bt_message_put_ref(msgs
[i
]);
237 int add_params_to_map(bt_value
*plugin_opt_map
)
242 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
243 const char *key
= plugin_options
[i
];
244 bt_value_status status
;
246 status
= bt_value_map_insert_entry(plugin_opt_map
, key
,
249 case BT_VALUE_STATUS_OK
:
261 bt_bool
check_param_exists(const char *key
, const bt_value
*object
,
264 struct pretty_component
*pretty
= data
;
266 if (!bt_value_map_has_entry(pretty
->plugin_opt_map
,
269 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
275 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
277 const bt_value
*value
= NULL
;
280 value
= bt_value_map_borrow_entry_value_const(params
, key
);
284 if (bt_value_is_null(value
)) {
287 str
= bt_value_string_get(value
);
288 *option
= g_strdup(str
);
295 void apply_one_bool(const char *key
, const bt_value
*params
, bool *option
,
298 const bt_value
*value
= NULL
;
301 value
= bt_value_map_borrow_entry_value_const(params
, key
);
305 bool_val
= bt_value_bool_get(value
);
306 *option
= (bool) bool_val
;
316 void warn_wrong_color_param(struct pretty_component
*pretty
)
319 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
323 int open_output_file(struct pretty_component
*pretty
)
327 if (!pretty
->options
.output_path
) {
331 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
346 int apply_params(struct pretty_component
*pretty
, const bt_value
*params
)
349 bt_value_status status
;
353 pretty
->plugin_opt_map
= bt_value_map_create();
354 if (!pretty
->plugin_opt_map
) {
358 ret
= add_params_to_map(pretty
->plugin_opt_map
);
362 /* Report unknown parameters. */
363 status
= bt_value_map_foreach_entry_const(params
,
364 check_param_exists
, pretty
);
366 case BT_VALUE_STATUS_OK
:
372 /* Known parameters. */
373 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
374 if (bt_value_map_has_entry(params
, "color")) {
375 const bt_value
*color_value
;
378 color_value
= bt_value_map_borrow_entry_value_const(params
,
384 color
= bt_value_string_get(color_value
);
386 if (strcmp(color
, "never") == 0) {
387 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
388 } else if (strcmp(color
, "auto") == 0) {
389 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
390 } else if (strcmp(color
, "always") == 0) {
391 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
393 warn_wrong_color_param(pretty
);
397 apply_one_string("path", params
, &pretty
->options
.output_path
);
398 ret
= open_output_file(pretty
);
403 value
= false; /* Default. */
404 apply_one_bool("no-delta", params
, &value
, NULL
);
405 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
407 value
= false; /* Default. */
408 apply_one_bool("clock-cycles", params
, &value
, NULL
);
409 pretty
->options
.print_timestamp_cycles
= value
;
411 value
= false; /* Default. */
412 apply_one_bool("clock-seconds", params
, &value
, NULL
);
413 pretty
->options
.clock_seconds
= value
;
415 value
= false; /* Default. */
416 apply_one_bool("clock-date", params
, &value
, NULL
);
417 pretty
->options
.clock_date
= value
;
419 value
= false; /* Default. */
420 apply_one_bool("clock-gmt", params
, &value
, NULL
);
421 pretty
->options
.clock_gmt
= value
;
423 value
= false; /* Default. */
424 apply_one_bool("verbose", params
, &value
, NULL
);
425 pretty
->options
.verbose
= value
;
428 apply_one_string("name-default", params
, &str
);
430 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
431 } else if (!strcmp(str
, "show")) {
432 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
433 } else if (!strcmp(str
, "hide")) {
434 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
442 switch (pretty
->options
.name_default
) {
443 case PRETTY_DEFAULT_UNSET
:
444 pretty
->options
.print_payload_field_names
= true;
445 pretty
->options
.print_context_field_names
= true;
446 pretty
->options
.print_header_field_names
= false;
447 pretty
->options
.print_scope_field_names
= false;
449 case PRETTY_DEFAULT_SHOW
:
450 pretty
->options
.print_payload_field_names
= true;
451 pretty
->options
.print_context_field_names
= true;
452 pretty
->options
.print_header_field_names
= true;
453 pretty
->options
.print_scope_field_names
= true;
455 case PRETTY_DEFAULT_HIDE
:
456 pretty
->options
.print_payload_field_names
= false;
457 pretty
->options
.print_context_field_names
= false;
458 pretty
->options
.print_header_field_names
= false;
459 pretty
->options
.print_scope_field_names
= false;
468 apply_one_bool("name-payload", params
, &value
, &found
);
470 pretty
->options
.print_payload_field_names
= value
;
475 apply_one_bool("name-context", params
, &value
, &found
);
477 pretty
->options
.print_context_field_names
= value
;
482 apply_one_bool("name-header", params
, &value
, &found
);
484 pretty
->options
.print_header_field_names
= value
;
489 apply_one_bool("name-scope", params
, &value
, &found
);
491 pretty
->options
.print_scope_field_names
= value
;
495 apply_one_string("field-default", params
, &str
);
497 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
498 } else if (!strcmp(str
, "show")) {
499 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
500 } else if (!strcmp(str
, "hide")) {
501 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
509 switch (pretty
->options
.field_default
) {
510 case PRETTY_DEFAULT_UNSET
:
511 pretty
->options
.print_trace_field
= false;
512 pretty
->options
.print_trace_hostname_field
= true;
513 pretty
->options
.print_trace_domain_field
= false;
514 pretty
->options
.print_trace_procname_field
= true;
515 pretty
->options
.print_trace_vpid_field
= true;
516 pretty
->options
.print_loglevel_field
= false;
517 pretty
->options
.print_emf_field
= false;
518 pretty
->options
.print_callsite_field
= false;
520 case PRETTY_DEFAULT_SHOW
:
521 pretty
->options
.print_trace_field
= true;
522 pretty
->options
.print_trace_hostname_field
= true;
523 pretty
->options
.print_trace_domain_field
= true;
524 pretty
->options
.print_trace_procname_field
= true;
525 pretty
->options
.print_trace_vpid_field
= true;
526 pretty
->options
.print_loglevel_field
= true;
527 pretty
->options
.print_emf_field
= true;
528 pretty
->options
.print_callsite_field
= true;
530 case PRETTY_DEFAULT_HIDE
:
531 pretty
->options
.print_trace_field
= false;
532 pretty
->options
.print_trace_hostname_field
= false;
533 pretty
->options
.print_trace_domain_field
= false;
534 pretty
->options
.print_trace_procname_field
= false;
535 pretty
->options
.print_trace_vpid_field
= false;
536 pretty
->options
.print_loglevel_field
= false;
537 pretty
->options
.print_emf_field
= false;
538 pretty
->options
.print_callsite_field
= false;
547 apply_one_bool("field-trace", params
, &value
, &found
);
549 pretty
->options
.print_trace_field
= value
;
554 apply_one_bool("field-trace:hostname", params
, &value
, &found
);
556 pretty
->options
.print_trace_hostname_field
= value
;
561 apply_one_bool("field-trace:domain", params
, &value
, &found
);
563 pretty
->options
.print_trace_domain_field
= value
;
568 apply_one_bool("field-trace:procname", params
, &value
, &found
);
570 pretty
->options
.print_trace_procname_field
= value
;
575 apply_one_bool("field-trace:vpid", params
, &value
, &found
);
577 pretty
->options
.print_trace_vpid_field
= value
;
582 apply_one_bool("field-loglevel", params
, &value
, &found
);
584 pretty
->options
.print_loglevel_field
= value
;
589 apply_one_bool("field-emf", params
, &value
, &found
);
591 pretty
->options
.print_emf_field
= value
;
596 apply_one_bool("field-callsite", params
, &value
, &found
);
598 pretty
->options
.print_callsite_field
= value
;
602 bt_value_put_ref(pretty
->plugin_opt_map
);
603 pretty
->plugin_opt_map
= NULL
;
609 void set_use_colors(struct pretty_component
*pretty
)
611 switch (pretty
->options
.color
) {
612 case PRETTY_COLOR_OPT_ALWAYS
:
613 pretty
->use_colors
= true;
615 case PRETTY_COLOR_OPT_AUTO
:
616 pretty
->use_colors
= pretty
->out
== stdout
&&
617 bt_common_colors_supported();
619 case PRETTY_COLOR_OPT_NEVER
:
620 pretty
->use_colors
= false;
626 void init_stream_packet_context_quarks(void)
628 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
629 g_quark_from_string("timestamp_begin");
630 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
631 g_quark_from_string("timestamp_begin");
632 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
633 g_quark_from_string("timestamp_end");
634 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
635 g_quark_from_string("events_discarded");
636 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
637 g_quark_from_string("content_size");
638 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
639 g_quark_from_string("packet_size");
640 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
641 g_quark_from_string("packet_seq_num");
645 bt_self_component_status
pretty_init(
646 bt_self_component_sink
*comp
,
647 const bt_value
*params
,
648 UNUSED_VAR
void *init_method_data
)
650 bt_self_component_status ret
;
651 struct pretty_component
*pretty
= create_pretty();
654 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
658 ret
= bt_self_component_sink_add_input_port(comp
, in_port_name
,
660 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
664 pretty
->out
= stdout
;
665 pretty
->err
= stderr
;
667 pretty
->delta_cycles
= -1ULL;
668 pretty
->last_cycles_timestamp
= -1ULL;
670 pretty
->delta_real_timestamp
= -1ULL;
671 pretty
->last_real_timestamp
= -1ULL;
673 if (apply_params(pretty
, params
)) {
674 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
678 set_use_colors(pretty
);
679 bt_self_component_set_data(
680 bt_self_component_sink_as_self_component(comp
), pretty
);
681 init_stream_packet_context_quarks();
687 destroy_pretty_data(pretty
);