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/SINK.TEXT.PRETTY"
29 #include <babeltrace2/babeltrace.h>
30 #include "compat/compiler.h"
31 #include "common/common.h"
35 #include "common/assert.h"
39 GQuark stream_packet_context_quarks
[STREAM_PACKET_CONTEXT_QUARKS_LEN
];
42 const char *plugin_options
[] = {
51 "name-default", /* show/hide */
56 "field-default", /* show/hide */
58 "field-trace:hostname",
60 "field-trace:procname",
68 const char * const in_port_name
= "in";
71 void destroy_pretty_data(struct pretty_component
*pretty
)
73 bt_self_component_port_input_message_iterator_put_ref(pretty
->iterator
);
76 (void) g_string_free(pretty
->string
, TRUE
);
79 if (pretty
->tmp_string
) {
80 (void) g_string_free(pretty
->tmp_string
, TRUE
);
83 if (pretty
->out
!= stdout
) {
86 ret
= fclose(pretty
->out
);
88 perror("close output file");
91 g_free(pretty
->options
.output_path
);
96 struct pretty_component
*create_pretty(void)
98 struct pretty_component
*pretty
;
100 pretty
= g_new0(struct pretty_component
, 1);
104 pretty
->string
= g_string_new("");
105 if (!pretty
->string
) {
108 pretty
->tmp_string
= g_string_new("");
109 if (!pretty
->tmp_string
) {
121 void pretty_finalize(bt_self_component_sink
*comp
)
124 bt_self_component_get_data(
125 bt_self_component_sink_as_self_component(comp
)));
129 bt_self_component_status
handle_message(
130 struct pretty_component
*pretty
,
131 const bt_message
*message
)
133 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
137 switch (bt_message_get_type(message
)) {
138 case BT_MESSAGE_TYPE_EVENT
:
139 if (pretty_print_event(pretty
, message
)) {
140 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
143 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
144 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
145 if (pretty_print_discarded_items(pretty
, message
)) {
146 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
157 bt_self_component_status
pretty_graph_is_configured(
158 bt_self_component_sink
*comp
)
160 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
161 struct pretty_component
*pretty
;
163 pretty
= bt_self_component_get_data(
164 bt_self_component_sink_as_self_component(comp
));
166 BT_ASSERT(!pretty
->iterator
);
167 pretty
->iterator
= bt_self_component_port_input_message_iterator_create(
168 bt_self_component_sink_borrow_input_port_by_name(comp
,
170 if (!pretty
->iterator
) {
171 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
178 bt_self_component_status
pretty_consume(
179 bt_self_component_sink
*comp
)
181 bt_self_component_status ret
;
182 bt_message_array_const msgs
;
183 bt_self_component_port_input_message_iterator
*it
;
184 struct pretty_component
*pretty
= bt_self_component_get_data(
185 bt_self_component_sink_as_self_component(comp
));
186 bt_message_iterator_status it_ret
;
190 it
= pretty
->iterator
;
191 it_ret
= bt_self_component_port_input_message_iterator_next(it
,
195 case BT_MESSAGE_ITERATOR_STATUS_OK
:
197 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
198 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
200 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
201 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
203 case BT_MESSAGE_ITERATOR_STATUS_END
:
204 ret
= BT_SELF_COMPONENT_STATUS_END
;
205 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
209 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
213 BT_ASSERT(it_ret
== BT_MESSAGE_ITERATOR_STATUS_OK
);
215 for (i
= 0; i
< count
; i
++) {
216 ret
= handle_message(pretty
, msgs
[i
]);
221 bt_message_put_ref(msgs
[i
]);
225 for (; i
< count
; i
++) {
226 bt_message_put_ref(msgs
[i
]);
233 int add_params_to_map(bt_value
*plugin_opt_map
)
238 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
239 const char *key
= plugin_options
[i
];
240 bt_value_status status
;
242 status
= bt_value_map_insert_entry(plugin_opt_map
, key
,
245 case BT_VALUE_STATUS_OK
:
257 bt_bool
check_param_exists(const char *key
, const bt_value
*object
,
260 struct pretty_component
*pretty
= data
;
262 if (!bt_value_map_has_entry(pretty
->plugin_opt_map
,
265 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
271 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
273 const bt_value
*value
= NULL
;
276 value
= bt_value_map_borrow_entry_value_const(params
, key
);
280 if (bt_value_is_null(value
)) {
283 str
= bt_value_string_get(value
);
284 *option
= g_strdup(str
);
291 void apply_one_bool(const char *key
, const bt_value
*params
, bool *option
,
294 const bt_value
*value
= NULL
;
297 value
= bt_value_map_borrow_entry_value_const(params
, key
);
301 bool_val
= bt_value_bool_get(value
);
302 *option
= (bool) bool_val
;
312 void warn_wrong_color_param(struct pretty_component
*pretty
)
315 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
319 int open_output_file(struct pretty_component
*pretty
)
323 if (!pretty
->options
.output_path
) {
327 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
342 int apply_params(struct pretty_component
*pretty
, const bt_value
*params
)
345 bt_value_status status
;
349 pretty
->plugin_opt_map
= bt_value_map_create();
350 if (!pretty
->plugin_opt_map
) {
354 ret
= add_params_to_map(pretty
->plugin_opt_map
);
358 /* Report unknown parameters. */
359 status
= bt_value_map_foreach_entry_const(params
,
360 check_param_exists
, pretty
);
362 case BT_VALUE_STATUS_OK
:
368 /* Known parameters. */
369 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
370 if (bt_value_map_has_entry(params
, "color")) {
371 const bt_value
*color_value
;
374 color_value
= bt_value_map_borrow_entry_value_const(params
,
380 color
= bt_value_string_get(color_value
);
382 if (strcmp(color
, "never") == 0) {
383 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
384 } else if (strcmp(color
, "auto") == 0) {
385 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
386 } else if (strcmp(color
, "always") == 0) {
387 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
389 warn_wrong_color_param(pretty
);
393 apply_one_string("path", params
, &pretty
->options
.output_path
);
394 ret
= open_output_file(pretty
);
399 value
= false; /* Default. */
400 apply_one_bool("no-delta", params
, &value
, NULL
);
401 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
403 value
= false; /* Default. */
404 apply_one_bool("clock-cycles", params
, &value
, NULL
);
405 pretty
->options
.print_timestamp_cycles
= value
;
407 value
= false; /* Default. */
408 apply_one_bool("clock-seconds", params
, &value
, NULL
);
409 pretty
->options
.clock_seconds
= value
;
411 value
= false; /* Default. */
412 apply_one_bool("clock-date", params
, &value
, NULL
);
413 pretty
->options
.clock_date
= value
;
415 value
= false; /* Default. */
416 apply_one_bool("clock-gmt", params
, &value
, NULL
);
417 pretty
->options
.clock_gmt
= value
;
419 value
= false; /* Default. */
420 apply_one_bool("verbose", params
, &value
, NULL
);
421 pretty
->options
.verbose
= value
;
424 apply_one_string("name-default", params
, &str
);
426 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
427 } else if (!strcmp(str
, "show")) {
428 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
429 } else if (!strcmp(str
, "hide")) {
430 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
438 switch (pretty
->options
.name_default
) {
439 case PRETTY_DEFAULT_UNSET
:
440 pretty
->options
.print_payload_field_names
= true;
441 pretty
->options
.print_context_field_names
= true;
442 pretty
->options
.print_header_field_names
= false;
443 pretty
->options
.print_scope_field_names
= false;
445 case PRETTY_DEFAULT_SHOW
:
446 pretty
->options
.print_payload_field_names
= true;
447 pretty
->options
.print_context_field_names
= true;
448 pretty
->options
.print_header_field_names
= true;
449 pretty
->options
.print_scope_field_names
= true;
451 case PRETTY_DEFAULT_HIDE
:
452 pretty
->options
.print_payload_field_names
= false;
453 pretty
->options
.print_context_field_names
= false;
454 pretty
->options
.print_header_field_names
= false;
455 pretty
->options
.print_scope_field_names
= false;
464 apply_one_bool("name-payload", params
, &value
, &found
);
466 pretty
->options
.print_payload_field_names
= value
;
471 apply_one_bool("name-context", params
, &value
, &found
);
473 pretty
->options
.print_context_field_names
= value
;
478 apply_one_bool("name-header", params
, &value
, &found
);
480 pretty
->options
.print_header_field_names
= value
;
485 apply_one_bool("name-scope", params
, &value
, &found
);
487 pretty
->options
.print_scope_field_names
= value
;
491 apply_one_string("field-default", params
, &str
);
493 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
494 } else if (!strcmp(str
, "show")) {
495 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
496 } else if (!strcmp(str
, "hide")) {
497 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
505 switch (pretty
->options
.field_default
) {
506 case PRETTY_DEFAULT_UNSET
:
507 pretty
->options
.print_trace_field
= false;
508 pretty
->options
.print_trace_hostname_field
= true;
509 pretty
->options
.print_trace_domain_field
= false;
510 pretty
->options
.print_trace_procname_field
= true;
511 pretty
->options
.print_trace_vpid_field
= true;
512 pretty
->options
.print_loglevel_field
= false;
513 pretty
->options
.print_emf_field
= false;
514 pretty
->options
.print_callsite_field
= false;
516 case PRETTY_DEFAULT_SHOW
:
517 pretty
->options
.print_trace_field
= true;
518 pretty
->options
.print_trace_hostname_field
= true;
519 pretty
->options
.print_trace_domain_field
= true;
520 pretty
->options
.print_trace_procname_field
= true;
521 pretty
->options
.print_trace_vpid_field
= true;
522 pretty
->options
.print_loglevel_field
= true;
523 pretty
->options
.print_emf_field
= true;
524 pretty
->options
.print_callsite_field
= true;
526 case PRETTY_DEFAULT_HIDE
:
527 pretty
->options
.print_trace_field
= false;
528 pretty
->options
.print_trace_hostname_field
= false;
529 pretty
->options
.print_trace_domain_field
= false;
530 pretty
->options
.print_trace_procname_field
= false;
531 pretty
->options
.print_trace_vpid_field
= false;
532 pretty
->options
.print_loglevel_field
= false;
533 pretty
->options
.print_emf_field
= false;
534 pretty
->options
.print_callsite_field
= false;
543 apply_one_bool("field-trace", params
, &value
, &found
);
545 pretty
->options
.print_trace_field
= value
;
550 apply_one_bool("field-trace:hostname", params
, &value
, &found
);
552 pretty
->options
.print_trace_hostname_field
= value
;
557 apply_one_bool("field-trace:domain", params
, &value
, &found
);
559 pretty
->options
.print_trace_domain_field
= value
;
564 apply_one_bool("field-trace:procname", params
, &value
, &found
);
566 pretty
->options
.print_trace_procname_field
= value
;
571 apply_one_bool("field-trace:vpid", params
, &value
, &found
);
573 pretty
->options
.print_trace_vpid_field
= value
;
578 apply_one_bool("field-loglevel", params
, &value
, &found
);
580 pretty
->options
.print_loglevel_field
= value
;
585 apply_one_bool("field-emf", params
, &value
, &found
);
587 pretty
->options
.print_emf_field
= value
;
592 apply_one_bool("field-callsite", params
, &value
, &found
);
594 pretty
->options
.print_callsite_field
= value
;
598 bt_value_put_ref(pretty
->plugin_opt_map
);
599 pretty
->plugin_opt_map
= NULL
;
605 void set_use_colors(struct pretty_component
*pretty
)
607 switch (pretty
->options
.color
) {
608 case PRETTY_COLOR_OPT_ALWAYS
:
609 pretty
->use_colors
= true;
611 case PRETTY_COLOR_OPT_AUTO
:
612 pretty
->use_colors
= pretty
->out
== stdout
&&
613 bt_common_colors_supported();
615 case PRETTY_COLOR_OPT_NEVER
:
616 pretty
->use_colors
= false;
622 void init_stream_packet_context_quarks(void)
624 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
625 g_quark_from_string("timestamp_begin");
626 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
627 g_quark_from_string("timestamp_begin");
628 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
629 g_quark_from_string("timestamp_end");
630 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
631 g_quark_from_string("events_discarded");
632 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
633 g_quark_from_string("content_size");
634 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
635 g_quark_from_string("packet_size");
636 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
637 g_quark_from_string("packet_seq_num");
641 bt_self_component_status
pretty_init(
642 bt_self_component_sink
*comp
,
643 const bt_value
*params
,
644 __attribute__((unused
)) void *init_method_data
)
646 bt_self_component_status ret
;
647 struct pretty_component
*pretty
= create_pretty();
650 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
654 ret
= bt_self_component_sink_add_input_port(comp
, in_port_name
,
656 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
660 pretty
->out
= stdout
;
661 pretty
->err
= stderr
;
663 pretty
->delta_cycles
= -1ULL;
664 pretty
->last_cycles_timestamp
= -1ULL;
666 pretty
->delta_real_timestamp
= -1ULL;
667 pretty
->last_real_timestamp
= -1ULL;
669 if (apply_params(pretty
, params
)) {
670 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
674 set_use_colors(pretty
);
675 bt_self_component_set_data(
676 bt_self_component_sink_as_self_component(comp
), pretty
);
677 init_stream_packet_context_quarks();
683 destroy_pretty_data(pretty
);