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 #include <babeltrace2/babeltrace.h>
27 #include "compat/compiler.h"
28 #include "common/common.h"
33 #include "common/assert.h"
37 GQuark stream_packet_context_quarks
[STREAM_PACKET_CONTEXT_QUARKS_LEN
];
40 const char *plugin_options
[] = {
49 "name-default", /* show/hide */
54 "field-default", /* show/hide */
56 "field-trace:hostname",
58 "field-trace:procname",
66 const char * const in_port_name
= "in";
69 void destroy_pretty_data(struct pretty_component
*pretty
)
71 bt_self_component_port_input_message_iterator_put_ref(pretty
->iterator
);
74 (void) g_string_free(pretty
->string
, TRUE
);
77 if (pretty
->tmp_string
) {
78 (void) g_string_free(pretty
->tmp_string
, TRUE
);
81 if (pretty
->out
!= stdout
) {
84 ret
= fclose(pretty
->out
);
86 perror("close output file");
89 g_free(pretty
->options
.output_path
);
94 struct pretty_component
*create_pretty(void)
96 struct pretty_component
*pretty
;
98 pretty
= g_new0(struct pretty_component
, 1);
102 pretty
->string
= g_string_new("");
103 if (!pretty
->string
) {
106 pretty
->tmp_string
= g_string_new("");
107 if (!pretty
->tmp_string
) {
119 void pretty_finalize(bt_self_component_sink
*comp
)
122 bt_self_component_get_data(
123 bt_self_component_sink_as_self_component(comp
)));
127 bt_component_class_message_iterator_next_method_status
handle_message(
128 struct pretty_component
*pretty
,
129 const bt_message
*message
)
131 bt_component_class_message_iterator_next_method_status ret
=
132 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
136 switch (bt_message_get_type(message
)) {
137 case BT_MESSAGE_TYPE_EVENT
:
138 if (pretty_print_event(pretty
, message
)) {
139 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
142 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
143 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
144 if (pretty_print_discarded_items(pretty
, message
)) {
145 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
156 bt_component_class_sink_graph_is_configured_method_status
157 pretty_graph_is_configured(bt_self_component_sink
*comp
)
159 bt_component_class_sink_graph_is_configured_method_status status
=
160 BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_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_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
178 bt_component_class_sink_consume_method_status
pretty_consume(
179 bt_self_component_sink
*comp
)
181 bt_component_class_sink_consume_method_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_next_status next_status
;
190 it
= pretty
->iterator
;
191 next_status
= bt_self_component_port_input_message_iterator_next(it
,
194 switch (next_status
) {
195 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
197 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
198 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
199 ret
= (int) next_status
;
201 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
202 ret
= (int) next_status
;
203 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
207 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
211 BT_ASSERT(next_status
== BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
);
213 for (i
= 0; i
< count
; i
++) {
214 ret
= handle_message(pretty
, msgs
[i
]);
219 bt_message_put_ref(msgs
[i
]);
223 for (; i
< count
; i
++) {
224 bt_message_put_ref(msgs
[i
]);
231 int add_params_to_map(bt_value
*plugin_opt_map
)
236 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
237 const char *key
= plugin_options
[i
];
239 if (bt_value_map_insert_entry(plugin_opt_map
, key
,
240 bt_value_null
) < 0) {
251 bt_bool
check_param_exists(const char *key
, const bt_value
*object
,
254 struct pretty_component
*pretty
= data
;
256 if (!bt_value_map_has_entry(pretty
->plugin_opt_map
,
259 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
265 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
267 const bt_value
*value
= NULL
;
270 value
= bt_value_map_borrow_entry_value_const(params
, key
);
274 if (bt_value_is_null(value
)) {
277 str
= bt_value_string_get(value
);
278 *option
= g_strdup(str
);
285 void apply_one_bool(const char *key
, const bt_value
*params
, bool *option
,
288 const bt_value
*value
= NULL
;
291 value
= bt_value_map_borrow_entry_value_const(params
, key
);
295 bool_val
= bt_value_bool_get(value
);
296 *option
= (bool) bool_val
;
306 void warn_wrong_color_param(struct pretty_component
*pretty
)
309 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
313 int open_output_file(struct pretty_component
*pretty
)
317 if (!pretty
->options
.output_path
) {
321 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
336 int apply_params(struct pretty_component
*pretty
, const bt_value
*params
)
342 pretty
->plugin_opt_map
= bt_value_map_create();
343 if (!pretty
->plugin_opt_map
) {
347 ret
= add_params_to_map(pretty
->plugin_opt_map
);
351 /* Report unknown parameters. */
352 if (bt_value_map_foreach_entry_const(params
,
353 check_param_exists
, pretty
) < 0) {
358 /* Known parameters. */
359 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
360 if (bt_value_map_has_entry(params
, "color")) {
361 const bt_value
*color_value
;
364 color_value
= bt_value_map_borrow_entry_value_const(params
,
370 color
= bt_value_string_get(color_value
);
372 if (strcmp(color
, "never") == 0) {
373 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
374 } else if (strcmp(color
, "auto") == 0) {
375 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
376 } else if (strcmp(color
, "always") == 0) {
377 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
379 warn_wrong_color_param(pretty
);
383 apply_one_string("path", params
, &pretty
->options
.output_path
);
384 ret
= open_output_file(pretty
);
389 value
= false; /* Default. */
390 apply_one_bool("no-delta", params
, &value
, NULL
);
391 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
393 value
= false; /* Default. */
394 apply_one_bool("clock-cycles", params
, &value
, NULL
);
395 pretty
->options
.print_timestamp_cycles
= value
;
397 value
= false; /* Default. */
398 apply_one_bool("clock-seconds", params
, &value
, NULL
);
399 pretty
->options
.clock_seconds
= value
;
401 value
= false; /* Default. */
402 apply_one_bool("clock-date", params
, &value
, NULL
);
403 pretty
->options
.clock_date
= value
;
405 value
= false; /* Default. */
406 apply_one_bool("clock-gmt", params
, &value
, NULL
);
407 pretty
->options
.clock_gmt
= value
;
409 value
= false; /* Default. */
410 apply_one_bool("verbose", params
, &value
, NULL
);
411 pretty
->options
.verbose
= value
;
414 apply_one_string("name-default", params
, &str
);
416 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
417 } else if (!strcmp(str
, "show")) {
418 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
419 } else if (!strcmp(str
, "hide")) {
420 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
428 switch (pretty
->options
.name_default
) {
429 case PRETTY_DEFAULT_UNSET
:
430 pretty
->options
.print_payload_field_names
= true;
431 pretty
->options
.print_context_field_names
= true;
432 pretty
->options
.print_header_field_names
= false;
433 pretty
->options
.print_scope_field_names
= false;
435 case PRETTY_DEFAULT_SHOW
:
436 pretty
->options
.print_payload_field_names
= true;
437 pretty
->options
.print_context_field_names
= true;
438 pretty
->options
.print_header_field_names
= true;
439 pretty
->options
.print_scope_field_names
= true;
441 case PRETTY_DEFAULT_HIDE
:
442 pretty
->options
.print_payload_field_names
= false;
443 pretty
->options
.print_context_field_names
= false;
444 pretty
->options
.print_header_field_names
= false;
445 pretty
->options
.print_scope_field_names
= false;
454 apply_one_bool("name-payload", params
, &value
, &found
);
456 pretty
->options
.print_payload_field_names
= value
;
461 apply_one_bool("name-context", params
, &value
, &found
);
463 pretty
->options
.print_context_field_names
= value
;
468 apply_one_bool("name-header", params
, &value
, &found
);
470 pretty
->options
.print_header_field_names
= value
;
475 apply_one_bool("name-scope", params
, &value
, &found
);
477 pretty
->options
.print_scope_field_names
= value
;
481 apply_one_string("field-default", params
, &str
);
483 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
484 } else if (!strcmp(str
, "show")) {
485 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
486 } else if (!strcmp(str
, "hide")) {
487 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
495 switch (pretty
->options
.field_default
) {
496 case PRETTY_DEFAULT_UNSET
:
497 pretty
->options
.print_trace_field
= false;
498 pretty
->options
.print_trace_hostname_field
= true;
499 pretty
->options
.print_trace_domain_field
= false;
500 pretty
->options
.print_trace_procname_field
= true;
501 pretty
->options
.print_trace_vpid_field
= true;
502 pretty
->options
.print_loglevel_field
= false;
503 pretty
->options
.print_emf_field
= false;
504 pretty
->options
.print_callsite_field
= false;
506 case PRETTY_DEFAULT_SHOW
:
507 pretty
->options
.print_trace_field
= true;
508 pretty
->options
.print_trace_hostname_field
= true;
509 pretty
->options
.print_trace_domain_field
= true;
510 pretty
->options
.print_trace_procname_field
= true;
511 pretty
->options
.print_trace_vpid_field
= true;
512 pretty
->options
.print_loglevel_field
= true;
513 pretty
->options
.print_emf_field
= true;
514 pretty
->options
.print_callsite_field
= true;
516 case PRETTY_DEFAULT_HIDE
:
517 pretty
->options
.print_trace_field
= false;
518 pretty
->options
.print_trace_hostname_field
= false;
519 pretty
->options
.print_trace_domain_field
= false;
520 pretty
->options
.print_trace_procname_field
= false;
521 pretty
->options
.print_trace_vpid_field
= false;
522 pretty
->options
.print_loglevel_field
= false;
523 pretty
->options
.print_emf_field
= false;
524 pretty
->options
.print_callsite_field
= false;
533 apply_one_bool("field-trace", params
, &value
, &found
);
535 pretty
->options
.print_trace_field
= value
;
540 apply_one_bool("field-trace:hostname", params
, &value
, &found
);
542 pretty
->options
.print_trace_hostname_field
= value
;
547 apply_one_bool("field-trace:domain", params
, &value
, &found
);
549 pretty
->options
.print_trace_domain_field
= value
;
554 apply_one_bool("field-trace:procname", params
, &value
, &found
);
556 pretty
->options
.print_trace_procname_field
= value
;
561 apply_one_bool("field-trace:vpid", params
, &value
, &found
);
563 pretty
->options
.print_trace_vpid_field
= value
;
568 apply_one_bool("field-loglevel", params
, &value
, &found
);
570 pretty
->options
.print_loglevel_field
= value
;
575 apply_one_bool("field-emf", params
, &value
, &found
);
577 pretty
->options
.print_emf_field
= value
;
582 apply_one_bool("field-callsite", params
, &value
, &found
);
584 pretty
->options
.print_callsite_field
= value
;
588 bt_value_put_ref(pretty
->plugin_opt_map
);
589 pretty
->plugin_opt_map
= NULL
;
595 void set_use_colors(struct pretty_component
*pretty
)
597 switch (pretty
->options
.color
) {
598 case PRETTY_COLOR_OPT_ALWAYS
:
599 pretty
->use_colors
= true;
601 case PRETTY_COLOR_OPT_AUTO
:
602 pretty
->use_colors
= pretty
->out
== stdout
&&
603 bt_common_colors_supported();
605 case PRETTY_COLOR_OPT_NEVER
:
606 pretty
->use_colors
= false;
612 void init_stream_packet_context_quarks(void)
614 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
615 g_quark_from_string("timestamp_begin");
616 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
617 g_quark_from_string("timestamp_begin");
618 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
619 g_quark_from_string("timestamp_end");
620 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
621 g_quark_from_string("events_discarded");
622 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
623 g_quark_from_string("content_size");
624 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
625 g_quark_from_string("packet_size");
626 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
627 g_quark_from_string("packet_seq_num");
631 bt_component_class_init_method_status
pretty_init(
632 bt_self_component_sink
*comp
, const bt_value
*params
,
633 __attribute__((unused
)) void *init_method_data
)
635 bt_component_class_init_method_status ret
=
636 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
637 struct pretty_component
*pretty
= create_pretty();
640 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
644 if (bt_self_component_sink_add_input_port(comp
,
645 in_port_name
, NULL
, NULL
) < 0) {
646 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
650 pretty
->out
= stdout
;
651 pretty
->err
= stderr
;
653 pretty
->delta_cycles
= -1ULL;
654 pretty
->last_cycles_timestamp
= -1ULL;
656 pretty
->delta_real_timestamp
= -1ULL;
657 pretty
->last_real_timestamp
= -1ULL;
659 if (apply_params(pretty
, params
)) {
660 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
664 set_use_colors(pretty
);
665 bt_self_component_set_data(
666 bt_self_component_sink_as_self_component(comp
), pretty
);
667 init_stream_packet_context_quarks();
673 destroy_pretty_data(pretty
);