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_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_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_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
183 bt_message_array_const msgs
;
184 bt_self_component_port_input_message_iterator
*it
;
185 struct pretty_component
*pretty
= bt_self_component_get_data(
186 bt_self_component_sink_as_self_component(comp
));
187 bt_message_iterator_next_status next_status
;
191 it
= pretty
->iterator
;
192 next_status
= bt_self_component_port_input_message_iterator_next(it
,
195 switch (next_status
) {
196 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
198 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
199 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
200 ret
= (int) next_status
;
202 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
203 ret
= (int) next_status
;
204 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
208 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
212 BT_ASSERT(next_status
== BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
);
214 for (i
= 0; i
< count
; i
++) {
215 ret
= (int) handle_message(pretty
, msgs
[i
]);
220 bt_message_put_ref(msgs
[i
]);
224 for (; i
< count
; i
++) {
225 bt_message_put_ref(msgs
[i
]);
232 int add_params_to_map(bt_value
*plugin_opt_map
)
237 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
238 const char *key
= plugin_options
[i
];
240 if (bt_value_map_insert_entry(plugin_opt_map
, key
,
241 bt_value_null
) < 0) {
252 bt_bool
check_param_exists(const char *key
, const bt_value
*object
,
255 struct pretty_component
*pretty
= data
;
257 if (!bt_value_map_has_entry(pretty
->plugin_opt_map
,
260 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
266 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
268 const bt_value
*value
= NULL
;
271 value
= bt_value_map_borrow_entry_value_const(params
, key
);
275 if (bt_value_is_null(value
)) {
278 str
= bt_value_string_get(value
);
279 *option
= g_strdup(str
);
286 void apply_one_bool(const char *key
, const bt_value
*params
, bool *option
,
289 const bt_value
*value
= NULL
;
292 value
= bt_value_map_borrow_entry_value_const(params
, key
);
296 bool_val
= bt_value_bool_get(value
);
297 *option
= (bool) bool_val
;
307 void warn_wrong_color_param(struct pretty_component
*pretty
)
310 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
314 int open_output_file(struct pretty_component
*pretty
)
318 if (!pretty
->options
.output_path
) {
322 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
337 int apply_params(struct pretty_component
*pretty
, const bt_value
*params
)
343 pretty
->plugin_opt_map
= bt_value_map_create();
344 if (!pretty
->plugin_opt_map
) {
348 ret
= add_params_to_map(pretty
->plugin_opt_map
);
352 /* Report unknown parameters. */
353 if (bt_value_map_foreach_entry_const(params
,
354 check_param_exists
, pretty
) < 0) {
359 /* Known parameters. */
360 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
361 if (bt_value_map_has_entry(params
, "color")) {
362 const bt_value
*color_value
;
365 color_value
= bt_value_map_borrow_entry_value_const(params
,
371 color
= bt_value_string_get(color_value
);
373 if (strcmp(color
, "never") == 0) {
374 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
375 } else if (strcmp(color
, "auto") == 0) {
376 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
377 } else if (strcmp(color
, "always") == 0) {
378 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
380 warn_wrong_color_param(pretty
);
384 apply_one_string("path", params
, &pretty
->options
.output_path
);
385 ret
= open_output_file(pretty
);
390 value
= false; /* Default. */
391 apply_one_bool("no-delta", params
, &value
, NULL
);
392 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
394 value
= false; /* Default. */
395 apply_one_bool("clock-cycles", params
, &value
, NULL
);
396 pretty
->options
.print_timestamp_cycles
= value
;
398 value
= false; /* Default. */
399 apply_one_bool("clock-seconds", params
, &value
, NULL
);
400 pretty
->options
.clock_seconds
= value
;
402 value
= false; /* Default. */
403 apply_one_bool("clock-date", params
, &value
, NULL
);
404 pretty
->options
.clock_date
= value
;
406 value
= false; /* Default. */
407 apply_one_bool("clock-gmt", params
, &value
, NULL
);
408 pretty
->options
.clock_gmt
= value
;
410 value
= false; /* Default. */
411 apply_one_bool("verbose", params
, &value
, NULL
);
412 pretty
->options
.verbose
= value
;
415 apply_one_string("name-default", params
, &str
);
417 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
418 } else if (strcmp(str
, "show") == 0) {
419 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
420 } else if (strcmp(str
, "hide") == 0) {
421 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
429 switch (pretty
->options
.name_default
) {
430 case PRETTY_DEFAULT_UNSET
:
431 pretty
->options
.print_payload_field_names
= true;
432 pretty
->options
.print_context_field_names
= true;
433 pretty
->options
.print_header_field_names
= false;
434 pretty
->options
.print_scope_field_names
= false;
436 case PRETTY_DEFAULT_SHOW
:
437 pretty
->options
.print_payload_field_names
= true;
438 pretty
->options
.print_context_field_names
= true;
439 pretty
->options
.print_header_field_names
= true;
440 pretty
->options
.print_scope_field_names
= true;
442 case PRETTY_DEFAULT_HIDE
:
443 pretty
->options
.print_payload_field_names
= false;
444 pretty
->options
.print_context_field_names
= false;
445 pretty
->options
.print_header_field_names
= false;
446 pretty
->options
.print_scope_field_names
= false;
455 apply_one_bool("name-payload", params
, &value
, &found
);
457 pretty
->options
.print_payload_field_names
= value
;
462 apply_one_bool("name-context", params
, &value
, &found
);
464 pretty
->options
.print_context_field_names
= value
;
469 apply_one_bool("name-header", params
, &value
, &found
);
471 pretty
->options
.print_header_field_names
= value
;
476 apply_one_bool("name-scope", params
, &value
, &found
);
478 pretty
->options
.print_scope_field_names
= value
;
482 apply_one_string("field-default", params
, &str
);
484 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
485 } else if (strcmp(str
, "show") == 0) {
486 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
487 } else if (strcmp(str
, "hide") == 0) {
488 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
496 switch (pretty
->options
.field_default
) {
497 case PRETTY_DEFAULT_UNSET
:
498 pretty
->options
.print_trace_field
= false;
499 pretty
->options
.print_trace_hostname_field
= true;
500 pretty
->options
.print_trace_domain_field
= false;
501 pretty
->options
.print_trace_procname_field
= true;
502 pretty
->options
.print_trace_vpid_field
= true;
503 pretty
->options
.print_loglevel_field
= false;
504 pretty
->options
.print_emf_field
= false;
505 pretty
->options
.print_callsite_field
= false;
507 case PRETTY_DEFAULT_SHOW
:
508 pretty
->options
.print_trace_field
= true;
509 pretty
->options
.print_trace_hostname_field
= true;
510 pretty
->options
.print_trace_domain_field
= true;
511 pretty
->options
.print_trace_procname_field
= true;
512 pretty
->options
.print_trace_vpid_field
= true;
513 pretty
->options
.print_loglevel_field
= true;
514 pretty
->options
.print_emf_field
= true;
515 pretty
->options
.print_callsite_field
= true;
517 case PRETTY_DEFAULT_HIDE
:
518 pretty
->options
.print_trace_field
= false;
519 pretty
->options
.print_trace_hostname_field
= false;
520 pretty
->options
.print_trace_domain_field
= false;
521 pretty
->options
.print_trace_procname_field
= false;
522 pretty
->options
.print_trace_vpid_field
= false;
523 pretty
->options
.print_loglevel_field
= false;
524 pretty
->options
.print_emf_field
= false;
525 pretty
->options
.print_callsite_field
= false;
534 apply_one_bool("field-trace", params
, &value
, &found
);
536 pretty
->options
.print_trace_field
= value
;
541 apply_one_bool("field-trace:hostname", params
, &value
, &found
);
543 pretty
->options
.print_trace_hostname_field
= value
;
548 apply_one_bool("field-trace:domain", params
, &value
, &found
);
550 pretty
->options
.print_trace_domain_field
= value
;
555 apply_one_bool("field-trace:procname", params
, &value
, &found
);
557 pretty
->options
.print_trace_procname_field
= value
;
562 apply_one_bool("field-trace:vpid", params
, &value
, &found
);
564 pretty
->options
.print_trace_vpid_field
= value
;
569 apply_one_bool("field-loglevel", params
, &value
, &found
);
571 pretty
->options
.print_loglevel_field
= value
;
576 apply_one_bool("field-emf", params
, &value
, &found
);
578 pretty
->options
.print_emf_field
= value
;
583 apply_one_bool("field-callsite", params
, &value
, &found
);
585 pretty
->options
.print_callsite_field
= value
;
589 bt_value_put_ref(pretty
->plugin_opt_map
);
590 pretty
->plugin_opt_map
= NULL
;
596 void set_use_colors(struct pretty_component
*pretty
)
598 switch (pretty
->options
.color
) {
599 case PRETTY_COLOR_OPT_ALWAYS
:
600 pretty
->use_colors
= true;
602 case PRETTY_COLOR_OPT_AUTO
:
603 pretty
->use_colors
= pretty
->out
== stdout
&&
604 bt_common_colors_supported();
606 case PRETTY_COLOR_OPT_NEVER
:
607 pretty
->use_colors
= false;
613 void init_stream_packet_context_quarks(void)
615 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
616 g_quark_from_string("timestamp_begin");
617 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
618 g_quark_from_string("timestamp_begin");
619 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
620 g_quark_from_string("timestamp_end");
621 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
622 g_quark_from_string("events_discarded");
623 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
624 g_quark_from_string("content_size");
625 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
626 g_quark_from_string("packet_size");
627 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
628 g_quark_from_string("packet_seq_num");
632 bt_component_class_init_method_status
pretty_init(
633 bt_self_component_sink
*comp
, const bt_value
*params
,
634 __attribute__((unused
)) void *init_method_data
)
636 bt_component_class_init_method_status ret
=
637 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
638 struct pretty_component
*pretty
= create_pretty();
641 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
645 if (bt_self_component_sink_add_input_port(comp
,
646 in_port_name
, NULL
, NULL
) < 0) {
647 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
651 pretty
->out
= stdout
;
652 pretty
->err
= stderr
;
654 pretty
->delta_cycles
= -1ULL;
655 pretty
->last_cycles_timestamp
= -1ULL;
657 pretty
->delta_real_timestamp
= -1ULL;
658 pretty
->last_real_timestamp
= -1ULL;
660 if (apply_params(pretty
, params
)) {
661 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
665 set_use_colors(pretty
);
666 bt_self_component_set_data(
667 bt_self_component_sink_as_self_component(comp
), pretty
);
668 init_stream_packet_context_quarks();
674 destroy_pretty_data(pretty
);