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"
38 const char *plugin_options
[] = {
47 "name-default", /* show/hide */
52 "field-default", /* show/hide */
54 "field-trace:hostname",
56 "field-trace:procname",
64 const char * const in_port_name
= "in";
67 void destroy_pretty_data(struct pretty_component
*pretty
)
69 bt_self_component_port_input_message_iterator_put_ref(pretty
->iterator
);
72 (void) g_string_free(pretty
->string
, TRUE
);
75 if (pretty
->tmp_string
) {
76 (void) g_string_free(pretty
->tmp_string
, TRUE
);
79 if (pretty
->out
!= stdout
) {
82 ret
= fclose(pretty
->out
);
84 perror("close output file");
87 g_free(pretty
->options
.output_path
);
92 struct pretty_component
*create_pretty(void)
94 struct pretty_component
*pretty
;
96 pretty
= g_new0(struct pretty_component
, 1);
100 pretty
->string
= g_string_new("");
101 if (!pretty
->string
) {
104 pretty
->tmp_string
= g_string_new("");
105 if (!pretty
->tmp_string
) {
117 void pretty_finalize(bt_self_component_sink
*comp
)
120 bt_self_component_get_data(
121 bt_self_component_sink_as_self_component(comp
)));
125 bt_component_class_message_iterator_next_method_status
handle_message(
126 struct pretty_component
*pretty
,
127 const bt_message
*message
)
129 bt_component_class_message_iterator_next_method_status ret
=
130 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
134 switch (bt_message_get_type(message
)) {
135 case BT_MESSAGE_TYPE_EVENT
:
136 if (pretty_print_event(pretty
, message
)) {
137 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
140 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
141 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
142 if (pretty_print_discarded_items(pretty
, message
)) {
143 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
154 bt_component_class_sink_graph_is_configured_method_status
155 pretty_graph_is_configured(bt_self_component_sink
*comp
)
157 bt_component_class_sink_graph_is_configured_method_status status
;
158 bt_self_component_port_input_message_iterator_create_from_sink_component_status
160 struct pretty_component
*pretty
;
162 pretty
= bt_self_component_get_data(
163 bt_self_component_sink_as_self_component(comp
));
165 BT_ASSERT(!pretty
->iterator
);
166 msg_iter_status
= bt_self_component_port_input_message_iterator_create_from_sink_component(
167 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
168 in_port_name
), &pretty
->iterator
);
169 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
170 status
= (int) msg_iter_status
;
174 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
181 bt_component_class_sink_consume_method_status
pretty_consume(
182 bt_self_component_sink
*comp
)
184 bt_component_class_sink_consume_method_status ret
=
185 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
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_next_status next_status
;
194 it
= pretty
->iterator
;
195 next_status
= bt_self_component_port_input_message_iterator_next(it
,
198 switch (next_status
) {
199 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
201 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
202 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
203 ret
= (int) next_status
;
205 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
206 ret
= (int) next_status
;
207 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
211 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
215 BT_ASSERT(next_status
== BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
);
217 for (i
= 0; i
< count
; i
++) {
218 ret
= (int) handle_message(pretty
, msgs
[i
]);
223 bt_message_put_ref(msgs
[i
]);
227 for (; i
< count
; i
++) {
228 bt_message_put_ref(msgs
[i
]);
235 int add_params_to_map(bt_value
*plugin_opt_map
)
240 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
241 const char *key
= plugin_options
[i
];
243 if (bt_value_map_insert_entry(plugin_opt_map
, key
,
244 bt_value_null
) < 0) {
255 bt_bool
check_param_exists(const char *key
, const bt_value
*object
,
258 struct pretty_component
*pretty
= data
;
260 if (!bt_value_map_has_entry(pretty
->plugin_opt_map
,
263 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
269 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
271 const bt_value
*value
= NULL
;
274 value
= bt_value_map_borrow_entry_value_const(params
, key
);
278 if (bt_value_is_null(value
)) {
281 str
= bt_value_string_get(value
);
282 *option
= g_strdup(str
);
289 void apply_one_bool(const char *key
, const bt_value
*params
, bool *option
,
292 const bt_value
*value
= NULL
;
295 value
= bt_value_map_borrow_entry_value_const(params
, key
);
299 bool_val
= bt_value_bool_get(value
);
300 *option
= (bool) bool_val
;
310 void warn_wrong_color_param(struct pretty_component
*pretty
)
313 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
317 int open_output_file(struct pretty_component
*pretty
)
321 if (!pretty
->options
.output_path
) {
325 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
340 int apply_params(struct pretty_component
*pretty
, const bt_value
*params
)
346 pretty
->plugin_opt_map
= bt_value_map_create();
347 if (!pretty
->plugin_opt_map
) {
351 ret
= add_params_to_map(pretty
->plugin_opt_map
);
355 /* Report unknown parameters. */
356 if (bt_value_map_foreach_entry_const(params
,
357 check_param_exists
, pretty
) < 0) {
362 /* Known parameters. */
363 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
364 if (bt_value_map_has_entry(params
, "color")) {
365 const bt_value
*color_value
;
368 color_value
= bt_value_map_borrow_entry_value_const(params
,
374 color
= bt_value_string_get(color_value
);
376 if (strcmp(color
, "never") == 0) {
377 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
378 } else if (strcmp(color
, "auto") == 0) {
379 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
380 } else if (strcmp(color
, "always") == 0) {
381 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
383 warn_wrong_color_param(pretty
);
387 apply_one_string("path", params
, &pretty
->options
.output_path
);
388 ret
= open_output_file(pretty
);
393 value
= false; /* Default. */
394 apply_one_bool("no-delta", params
, &value
, NULL
);
395 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
397 value
= false; /* Default. */
398 apply_one_bool("clock-cycles", params
, &value
, NULL
);
399 pretty
->options
.print_timestamp_cycles
= value
;
401 value
= false; /* Default. */
402 apply_one_bool("clock-seconds", params
, &value
, NULL
);
403 pretty
->options
.clock_seconds
= value
;
405 value
= false; /* Default. */
406 apply_one_bool("clock-date", params
, &value
, NULL
);
407 pretty
->options
.clock_date
= value
;
409 value
= false; /* Default. */
410 apply_one_bool("clock-gmt", params
, &value
, NULL
);
411 pretty
->options
.clock_gmt
= value
;
413 value
= false; /* Default. */
414 apply_one_bool("verbose", params
, &value
, NULL
);
415 pretty
->options
.verbose
= value
;
418 apply_one_string("name-default", params
, &str
);
420 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
421 } else if (strcmp(str
, "show") == 0) {
422 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
423 } else if (strcmp(str
, "hide") == 0) {
424 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
432 switch (pretty
->options
.name_default
) {
433 case PRETTY_DEFAULT_UNSET
:
434 pretty
->options
.print_payload_field_names
= true;
435 pretty
->options
.print_context_field_names
= true;
436 pretty
->options
.print_header_field_names
= false;
437 pretty
->options
.print_scope_field_names
= false;
439 case PRETTY_DEFAULT_SHOW
:
440 pretty
->options
.print_payload_field_names
= true;
441 pretty
->options
.print_context_field_names
= true;
442 pretty
->options
.print_header_field_names
= true;
443 pretty
->options
.print_scope_field_names
= true;
445 case PRETTY_DEFAULT_HIDE
:
446 pretty
->options
.print_payload_field_names
= false;
447 pretty
->options
.print_context_field_names
= false;
448 pretty
->options
.print_header_field_names
= false;
449 pretty
->options
.print_scope_field_names
= false;
458 apply_one_bool("name-payload", params
, &value
, &found
);
460 pretty
->options
.print_payload_field_names
= value
;
465 apply_one_bool("name-context", params
, &value
, &found
);
467 pretty
->options
.print_context_field_names
= value
;
472 apply_one_bool("name-header", params
, &value
, &found
);
474 pretty
->options
.print_header_field_names
= value
;
479 apply_one_bool("name-scope", params
, &value
, &found
);
481 pretty
->options
.print_scope_field_names
= value
;
485 apply_one_string("field-default", params
, &str
);
487 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
488 } else if (strcmp(str
, "show") == 0) {
489 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
490 } else if (strcmp(str
, "hide") == 0) {
491 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
499 switch (pretty
->options
.field_default
) {
500 case PRETTY_DEFAULT_UNSET
:
501 pretty
->options
.print_trace_field
= false;
502 pretty
->options
.print_trace_hostname_field
= true;
503 pretty
->options
.print_trace_domain_field
= false;
504 pretty
->options
.print_trace_procname_field
= true;
505 pretty
->options
.print_trace_vpid_field
= true;
506 pretty
->options
.print_loglevel_field
= false;
507 pretty
->options
.print_emf_field
= false;
508 pretty
->options
.print_callsite_field
= false;
510 case PRETTY_DEFAULT_SHOW
:
511 pretty
->options
.print_trace_field
= true;
512 pretty
->options
.print_trace_hostname_field
= true;
513 pretty
->options
.print_trace_domain_field
= true;
514 pretty
->options
.print_trace_procname_field
= true;
515 pretty
->options
.print_trace_vpid_field
= true;
516 pretty
->options
.print_loglevel_field
= true;
517 pretty
->options
.print_emf_field
= true;
518 pretty
->options
.print_callsite_field
= true;
520 case PRETTY_DEFAULT_HIDE
:
521 pretty
->options
.print_trace_field
= false;
522 pretty
->options
.print_trace_hostname_field
= false;
523 pretty
->options
.print_trace_domain_field
= false;
524 pretty
->options
.print_trace_procname_field
= false;
525 pretty
->options
.print_trace_vpid_field
= false;
526 pretty
->options
.print_loglevel_field
= false;
527 pretty
->options
.print_emf_field
= false;
528 pretty
->options
.print_callsite_field
= false;
537 apply_one_bool("field-trace", params
, &value
, &found
);
539 pretty
->options
.print_trace_field
= value
;
544 apply_one_bool("field-trace:hostname", params
, &value
, &found
);
546 pretty
->options
.print_trace_hostname_field
= value
;
551 apply_one_bool("field-trace:domain", params
, &value
, &found
);
553 pretty
->options
.print_trace_domain_field
= value
;
558 apply_one_bool("field-trace:procname", params
, &value
, &found
);
560 pretty
->options
.print_trace_procname_field
= value
;
565 apply_one_bool("field-trace:vpid", params
, &value
, &found
);
567 pretty
->options
.print_trace_vpid_field
= value
;
572 apply_one_bool("field-loglevel", params
, &value
, &found
);
574 pretty
->options
.print_loglevel_field
= value
;
579 apply_one_bool("field-emf", params
, &value
, &found
);
581 pretty
->options
.print_emf_field
= value
;
586 apply_one_bool("field-callsite", params
, &value
, &found
);
588 pretty
->options
.print_callsite_field
= value
;
592 bt_value_put_ref(pretty
->plugin_opt_map
);
593 pretty
->plugin_opt_map
= NULL
;
599 void set_use_colors(struct pretty_component
*pretty
)
601 switch (pretty
->options
.color
) {
602 case PRETTY_COLOR_OPT_ALWAYS
:
603 pretty
->use_colors
= true;
605 case PRETTY_COLOR_OPT_AUTO
:
606 pretty
->use_colors
= pretty
->out
== stdout
&&
607 bt_common_colors_supported();
609 case PRETTY_COLOR_OPT_NEVER
:
610 pretty
->use_colors
= false;
616 bt_component_class_init_method_status
pretty_init(
617 bt_self_component_sink
*comp
,
618 bt_self_component_sink_configuration
*config
,
619 const bt_value
*params
,
620 __attribute__((unused
)) void *init_method_data
)
622 bt_component_class_init_method_status ret
=
623 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
624 struct pretty_component
*pretty
= create_pretty();
627 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
631 if (bt_self_component_sink_add_input_port(comp
,
632 in_port_name
, NULL
, NULL
) < 0) {
633 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
637 pretty
->out
= stdout
;
638 pretty
->err
= stderr
;
640 pretty
->delta_cycles
= -1ULL;
641 pretty
->last_cycles_timestamp
= -1ULL;
643 pretty
->delta_real_timestamp
= -1ULL;
644 pretty
->last_real_timestamp
= -1ULL;
646 if (apply_params(pretty
, params
)) {
647 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
651 set_use_colors(pretty
);
652 bt_self_component_set_data(
653 bt_self_component_sink_as_self_component(comp
), pretty
);
659 destroy_pretty_data(pretty
);