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"
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_MESSAGE_ITERATOR_INACTIVITY
:
144 BT_LOGD_STR("Message iterator inactivity message.");
146 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
147 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
148 if (pretty_print_discarded_items(pretty
, message
)) {
149 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
160 bt_self_component_status
pretty_graph_is_configured(
161 bt_self_component_sink
*comp
)
163 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
164 struct pretty_component
*pretty
;
166 pretty
= bt_self_component_get_data(
167 bt_self_component_sink_as_self_component(comp
));
169 BT_ASSERT(!pretty
->iterator
);
170 pretty
->iterator
= bt_self_component_port_input_message_iterator_create(
171 bt_self_component_sink_borrow_input_port_by_name(comp
,
173 if (!pretty
->iterator
) {
174 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
181 bt_self_component_status
pretty_consume(
182 bt_self_component_sink
*comp
)
184 bt_self_component_status ret
;
185 bt_message_array_const msgs
;
186 bt_self_component_port_input_message_iterator
*it
;
187 struct pretty_component
*pretty
= bt_self_component_get_data(
188 bt_self_component_sink_as_self_component(comp
));
189 bt_message_iterator_status it_ret
;
193 it
= pretty
->iterator
;
194 it_ret
= bt_self_component_port_input_message_iterator_next(it
,
198 case BT_MESSAGE_ITERATOR_STATUS_OK
:
200 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
201 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
203 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
204 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
206 case BT_MESSAGE_ITERATOR_STATUS_END
:
207 ret
= BT_SELF_COMPONENT_STATUS_END
;
208 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
212 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
216 BT_ASSERT(it_ret
== BT_MESSAGE_ITERATOR_STATUS_OK
);
218 for (i
= 0; i
< count
; i
++) {
219 ret
= handle_message(pretty
, msgs
[i
]);
224 bt_message_put_ref(msgs
[i
]);
228 for (; i
< count
; i
++) {
229 bt_message_put_ref(msgs
[i
]);
236 int add_params_to_map(bt_value
*plugin_opt_map
)
241 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
242 const char *key
= plugin_options
[i
];
243 bt_value_status status
;
245 status
= bt_value_map_insert_entry(plugin_opt_map
, key
,
248 case BT_VALUE_STATUS_OK
:
260 bt_bool
check_param_exists(const char *key
, const bt_value
*object
,
263 struct pretty_component
*pretty
= data
;
265 if (!bt_value_map_has_entry(pretty
->plugin_opt_map
,
268 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
274 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
276 const bt_value
*value
= NULL
;
279 value
= bt_value_map_borrow_entry_value_const(params
, key
);
283 if (bt_value_is_null(value
)) {
286 str
= bt_value_string_get(value
);
287 *option
= g_strdup(str
);
294 void apply_one_bool(const char *key
, const bt_value
*params
, bool *option
,
297 const bt_value
*value
= NULL
;
300 value
= bt_value_map_borrow_entry_value_const(params
, key
);
304 bool_val
= bt_value_bool_get(value
);
305 *option
= (bool) bool_val
;
315 void warn_wrong_color_param(struct pretty_component
*pretty
)
318 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
322 int open_output_file(struct pretty_component
*pretty
)
326 if (!pretty
->options
.output_path
) {
330 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
345 int apply_params(struct pretty_component
*pretty
, const bt_value
*params
)
348 bt_value_status status
;
352 pretty
->plugin_opt_map
= bt_value_map_create();
353 if (!pretty
->plugin_opt_map
) {
357 ret
= add_params_to_map(pretty
->plugin_opt_map
);
361 /* Report unknown parameters. */
362 status
= bt_value_map_foreach_entry_const(params
,
363 check_param_exists
, pretty
);
365 case BT_VALUE_STATUS_OK
:
371 /* Known parameters. */
372 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
373 if (bt_value_map_has_entry(params
, "color")) {
374 const bt_value
*color_value
;
377 color_value
= bt_value_map_borrow_entry_value_const(params
,
383 color
= bt_value_string_get(color_value
);
385 if (strcmp(color
, "never") == 0) {
386 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
387 } else if (strcmp(color
, "auto") == 0) {
388 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
389 } else if (strcmp(color
, "always") == 0) {
390 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
392 warn_wrong_color_param(pretty
);
396 apply_one_string("path", params
, &pretty
->options
.output_path
);
397 ret
= open_output_file(pretty
);
402 value
= false; /* Default. */
403 apply_one_bool("no-delta", params
, &value
, NULL
);
404 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
406 value
= false; /* Default. */
407 apply_one_bool("clock-cycles", params
, &value
, NULL
);
408 pretty
->options
.print_timestamp_cycles
= value
;
410 value
= false; /* Default. */
411 apply_one_bool("clock-seconds", params
, &value
, NULL
);
412 pretty
->options
.clock_seconds
= value
;
414 value
= false; /* Default. */
415 apply_one_bool("clock-date", params
, &value
, NULL
);
416 pretty
->options
.clock_date
= value
;
418 value
= false; /* Default. */
419 apply_one_bool("clock-gmt", params
, &value
, NULL
);
420 pretty
->options
.clock_gmt
= value
;
422 value
= false; /* Default. */
423 apply_one_bool("verbose", params
, &value
, NULL
);
424 pretty
->options
.verbose
= value
;
427 apply_one_string("name-default", params
, &str
);
429 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
430 } else if (!strcmp(str
, "show")) {
431 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
432 } else if (!strcmp(str
, "hide")) {
433 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
441 switch (pretty
->options
.name_default
) {
442 case PRETTY_DEFAULT_UNSET
:
443 pretty
->options
.print_payload_field_names
= true;
444 pretty
->options
.print_context_field_names
= true;
445 pretty
->options
.print_header_field_names
= false;
446 pretty
->options
.print_scope_field_names
= false;
448 case PRETTY_DEFAULT_SHOW
:
449 pretty
->options
.print_payload_field_names
= true;
450 pretty
->options
.print_context_field_names
= true;
451 pretty
->options
.print_header_field_names
= true;
452 pretty
->options
.print_scope_field_names
= true;
454 case PRETTY_DEFAULT_HIDE
:
455 pretty
->options
.print_payload_field_names
= false;
456 pretty
->options
.print_context_field_names
= false;
457 pretty
->options
.print_header_field_names
= false;
458 pretty
->options
.print_scope_field_names
= false;
467 apply_one_bool("name-payload", params
, &value
, &found
);
469 pretty
->options
.print_payload_field_names
= value
;
474 apply_one_bool("name-context", params
, &value
, &found
);
476 pretty
->options
.print_context_field_names
= value
;
481 apply_one_bool("name-header", params
, &value
, &found
);
483 pretty
->options
.print_header_field_names
= value
;
488 apply_one_bool("name-scope", params
, &value
, &found
);
490 pretty
->options
.print_scope_field_names
= value
;
494 apply_one_string("field-default", params
, &str
);
496 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
497 } else if (!strcmp(str
, "show")) {
498 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
499 } else if (!strcmp(str
, "hide")) {
500 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
508 switch (pretty
->options
.field_default
) {
509 case PRETTY_DEFAULT_UNSET
:
510 pretty
->options
.print_trace_field
= false;
511 pretty
->options
.print_trace_hostname_field
= true;
512 pretty
->options
.print_trace_domain_field
= false;
513 pretty
->options
.print_trace_procname_field
= true;
514 pretty
->options
.print_trace_vpid_field
= true;
515 pretty
->options
.print_loglevel_field
= false;
516 pretty
->options
.print_emf_field
= false;
517 pretty
->options
.print_callsite_field
= false;
519 case PRETTY_DEFAULT_SHOW
:
520 pretty
->options
.print_trace_field
= true;
521 pretty
->options
.print_trace_hostname_field
= true;
522 pretty
->options
.print_trace_domain_field
= true;
523 pretty
->options
.print_trace_procname_field
= true;
524 pretty
->options
.print_trace_vpid_field
= true;
525 pretty
->options
.print_loglevel_field
= true;
526 pretty
->options
.print_emf_field
= true;
527 pretty
->options
.print_callsite_field
= true;
529 case PRETTY_DEFAULT_HIDE
:
530 pretty
->options
.print_trace_field
= false;
531 pretty
->options
.print_trace_hostname_field
= false;
532 pretty
->options
.print_trace_domain_field
= false;
533 pretty
->options
.print_trace_procname_field
= false;
534 pretty
->options
.print_trace_vpid_field
= false;
535 pretty
->options
.print_loglevel_field
= false;
536 pretty
->options
.print_emf_field
= false;
537 pretty
->options
.print_callsite_field
= false;
546 apply_one_bool("field-trace", params
, &value
, &found
);
548 pretty
->options
.print_trace_field
= value
;
553 apply_one_bool("field-trace:hostname", params
, &value
, &found
);
555 pretty
->options
.print_trace_hostname_field
= value
;
560 apply_one_bool("field-trace:domain", params
, &value
, &found
);
562 pretty
->options
.print_trace_domain_field
= value
;
567 apply_one_bool("field-trace:procname", params
, &value
, &found
);
569 pretty
->options
.print_trace_procname_field
= value
;
574 apply_one_bool("field-trace:vpid", params
, &value
, &found
);
576 pretty
->options
.print_trace_vpid_field
= value
;
581 apply_one_bool("field-loglevel", params
, &value
, &found
);
583 pretty
->options
.print_loglevel_field
= value
;
588 apply_one_bool("field-emf", params
, &value
, &found
);
590 pretty
->options
.print_emf_field
= value
;
595 apply_one_bool("field-callsite", params
, &value
, &found
);
597 pretty
->options
.print_callsite_field
= value
;
601 bt_value_put_ref(pretty
->plugin_opt_map
);
602 pretty
->plugin_opt_map
= NULL
;
608 void set_use_colors(struct pretty_component
*pretty
)
610 switch (pretty
->options
.color
) {
611 case PRETTY_COLOR_OPT_ALWAYS
:
612 pretty
->use_colors
= true;
614 case PRETTY_COLOR_OPT_AUTO
:
615 pretty
->use_colors
= pretty
->out
== stdout
&&
616 bt_common_colors_supported();
618 case PRETTY_COLOR_OPT_NEVER
:
619 pretty
->use_colors
= false;
625 void init_stream_packet_context_quarks(void)
627 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
628 g_quark_from_string("timestamp_begin");
629 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
630 g_quark_from_string("timestamp_begin");
631 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
632 g_quark_from_string("timestamp_end");
633 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
634 g_quark_from_string("events_discarded");
635 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
636 g_quark_from_string("content_size");
637 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
638 g_quark_from_string("packet_size");
639 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
640 g_quark_from_string("packet_seq_num");
644 bt_self_component_status
pretty_init(
645 bt_self_component_sink
*comp
,
646 const bt_value
*params
,
647 __attribute__((unused
)) void *init_method_data
)
649 bt_self_component_status ret
;
650 struct pretty_component
*pretty
= create_pretty();
653 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
657 ret
= bt_self_component_sink_add_input_port(comp
, in_port_name
,
659 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
663 pretty
->out
= stdout
;
664 pretty
->err
= stderr
;
666 pretty
->delta_cycles
= -1ULL;
667 pretty
->last_cycles_timestamp
= -1ULL;
669 pretty
->delta_real_timestamp
= -1ULL;
670 pretty
->last_real_timestamp
= -1ULL;
672 if (apply_params(pretty
, params
)) {
673 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
677 set_use_colors(pretty
);
678 bt_self_component_set_data(
679 bt_self_component_sink_as_self_component(comp
), pretty
);
680 init_stream_packet_context_quarks();
686 destroy_pretty_data(pretty
);