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_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
159 struct pretty_component
*pretty
;
161 pretty
= bt_self_component_get_data(
162 bt_self_component_sink_as_self_component(comp
));
164 BT_ASSERT(!pretty
->iterator
);
165 pretty
->iterator
= bt_self_component_port_input_message_iterator_create_from_sink_component(
166 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
168 if (!pretty
->iterator
) {
169 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR
;
176 bt_component_class_sink_consume_method_status
pretty_consume(
177 bt_self_component_sink
*comp
)
179 bt_component_class_sink_consume_method_status ret
=
180 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
181 bt_message_array_const msgs
;
182 bt_self_component_port_input_message_iterator
*it
;
183 struct pretty_component
*pretty
= bt_self_component_get_data(
184 bt_self_component_sink_as_self_component(comp
));
185 bt_message_iterator_next_status next_status
;
189 it
= pretty
->iterator
;
190 next_status
= bt_self_component_port_input_message_iterator_next(it
,
193 switch (next_status
) {
194 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
196 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
197 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
198 ret
= (int) next_status
;
200 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
201 ret
= (int) next_status
;
202 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
206 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
210 BT_ASSERT(next_status
== BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
);
212 for (i
= 0; i
< count
; i
++) {
213 ret
= (int) handle_message(pretty
, msgs
[i
]);
218 bt_message_put_ref(msgs
[i
]);
222 for (; i
< count
; i
++) {
223 bt_message_put_ref(msgs
[i
]);
230 int add_params_to_map(bt_value
*plugin_opt_map
)
235 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
236 const char *key
= plugin_options
[i
];
238 if (bt_value_map_insert_entry(plugin_opt_map
, key
,
239 bt_value_null
) < 0) {
250 bt_bool
check_param_exists(const char *key
, const bt_value
*object
,
253 struct pretty_component
*pretty
= data
;
255 if (!bt_value_map_has_entry(pretty
->plugin_opt_map
,
258 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
264 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
266 const bt_value
*value
= NULL
;
269 value
= bt_value_map_borrow_entry_value_const(params
, key
);
273 if (bt_value_is_null(value
)) {
276 str
= bt_value_string_get(value
);
277 *option
= g_strdup(str
);
284 void apply_one_bool(const char *key
, const bt_value
*params
, bool *option
,
287 const bt_value
*value
= NULL
;
290 value
= bt_value_map_borrow_entry_value_const(params
, key
);
294 bool_val
= bt_value_bool_get(value
);
295 *option
= (bool) bool_val
;
305 void warn_wrong_color_param(struct pretty_component
*pretty
)
308 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
312 int open_output_file(struct pretty_component
*pretty
)
316 if (!pretty
->options
.output_path
) {
320 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
335 int apply_params(struct pretty_component
*pretty
, const bt_value
*params
)
341 pretty
->plugin_opt_map
= bt_value_map_create();
342 if (!pretty
->plugin_opt_map
) {
346 ret
= add_params_to_map(pretty
->plugin_opt_map
);
350 /* Report unknown parameters. */
351 if (bt_value_map_foreach_entry_const(params
,
352 check_param_exists
, pretty
) < 0) {
357 /* Known parameters. */
358 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
359 if (bt_value_map_has_entry(params
, "color")) {
360 const bt_value
*color_value
;
363 color_value
= bt_value_map_borrow_entry_value_const(params
,
369 color
= bt_value_string_get(color_value
);
371 if (strcmp(color
, "never") == 0) {
372 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
373 } else if (strcmp(color
, "auto") == 0) {
374 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
375 } else if (strcmp(color
, "always") == 0) {
376 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
378 warn_wrong_color_param(pretty
);
382 apply_one_string("path", params
, &pretty
->options
.output_path
);
383 ret
= open_output_file(pretty
);
388 value
= false; /* Default. */
389 apply_one_bool("no-delta", params
, &value
, NULL
);
390 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
392 value
= false; /* Default. */
393 apply_one_bool("clock-cycles", params
, &value
, NULL
);
394 pretty
->options
.print_timestamp_cycles
= value
;
396 value
= false; /* Default. */
397 apply_one_bool("clock-seconds", params
, &value
, NULL
);
398 pretty
->options
.clock_seconds
= value
;
400 value
= false; /* Default. */
401 apply_one_bool("clock-date", params
, &value
, NULL
);
402 pretty
->options
.clock_date
= value
;
404 value
= false; /* Default. */
405 apply_one_bool("clock-gmt", params
, &value
, NULL
);
406 pretty
->options
.clock_gmt
= value
;
408 value
= false; /* Default. */
409 apply_one_bool("verbose", params
, &value
, NULL
);
410 pretty
->options
.verbose
= value
;
413 apply_one_string("name-default", params
, &str
);
415 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
416 } else if (strcmp(str
, "show") == 0) {
417 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
418 } else if (strcmp(str
, "hide") == 0) {
419 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
427 switch (pretty
->options
.name_default
) {
428 case PRETTY_DEFAULT_UNSET
:
429 pretty
->options
.print_payload_field_names
= true;
430 pretty
->options
.print_context_field_names
= true;
431 pretty
->options
.print_header_field_names
= false;
432 pretty
->options
.print_scope_field_names
= false;
434 case PRETTY_DEFAULT_SHOW
:
435 pretty
->options
.print_payload_field_names
= true;
436 pretty
->options
.print_context_field_names
= true;
437 pretty
->options
.print_header_field_names
= true;
438 pretty
->options
.print_scope_field_names
= true;
440 case PRETTY_DEFAULT_HIDE
:
441 pretty
->options
.print_payload_field_names
= false;
442 pretty
->options
.print_context_field_names
= false;
443 pretty
->options
.print_header_field_names
= false;
444 pretty
->options
.print_scope_field_names
= false;
453 apply_one_bool("name-payload", params
, &value
, &found
);
455 pretty
->options
.print_payload_field_names
= value
;
460 apply_one_bool("name-context", params
, &value
, &found
);
462 pretty
->options
.print_context_field_names
= value
;
467 apply_one_bool("name-header", params
, &value
, &found
);
469 pretty
->options
.print_header_field_names
= value
;
474 apply_one_bool("name-scope", params
, &value
, &found
);
476 pretty
->options
.print_scope_field_names
= value
;
480 apply_one_string("field-default", params
, &str
);
482 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
483 } else if (strcmp(str
, "show") == 0) {
484 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
485 } else if (strcmp(str
, "hide") == 0) {
486 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
494 switch (pretty
->options
.field_default
) {
495 case PRETTY_DEFAULT_UNSET
:
496 pretty
->options
.print_trace_field
= false;
497 pretty
->options
.print_trace_hostname_field
= true;
498 pretty
->options
.print_trace_domain_field
= false;
499 pretty
->options
.print_trace_procname_field
= true;
500 pretty
->options
.print_trace_vpid_field
= true;
501 pretty
->options
.print_loglevel_field
= false;
502 pretty
->options
.print_emf_field
= false;
503 pretty
->options
.print_callsite_field
= false;
505 case PRETTY_DEFAULT_SHOW
:
506 pretty
->options
.print_trace_field
= true;
507 pretty
->options
.print_trace_hostname_field
= true;
508 pretty
->options
.print_trace_domain_field
= true;
509 pretty
->options
.print_trace_procname_field
= true;
510 pretty
->options
.print_trace_vpid_field
= true;
511 pretty
->options
.print_loglevel_field
= true;
512 pretty
->options
.print_emf_field
= true;
513 pretty
->options
.print_callsite_field
= true;
515 case PRETTY_DEFAULT_HIDE
:
516 pretty
->options
.print_trace_field
= false;
517 pretty
->options
.print_trace_hostname_field
= false;
518 pretty
->options
.print_trace_domain_field
= false;
519 pretty
->options
.print_trace_procname_field
= false;
520 pretty
->options
.print_trace_vpid_field
= false;
521 pretty
->options
.print_loglevel_field
= false;
522 pretty
->options
.print_emf_field
= false;
523 pretty
->options
.print_callsite_field
= false;
532 apply_one_bool("field-trace", params
, &value
, &found
);
534 pretty
->options
.print_trace_field
= value
;
539 apply_one_bool("field-trace:hostname", params
, &value
, &found
);
541 pretty
->options
.print_trace_hostname_field
= value
;
546 apply_one_bool("field-trace:domain", params
, &value
, &found
);
548 pretty
->options
.print_trace_domain_field
= value
;
553 apply_one_bool("field-trace:procname", params
, &value
, &found
);
555 pretty
->options
.print_trace_procname_field
= value
;
560 apply_one_bool("field-trace:vpid", params
, &value
, &found
);
562 pretty
->options
.print_trace_vpid_field
= value
;
567 apply_one_bool("field-loglevel", params
, &value
, &found
);
569 pretty
->options
.print_loglevel_field
= value
;
574 apply_one_bool("field-emf", params
, &value
, &found
);
576 pretty
->options
.print_emf_field
= value
;
581 apply_one_bool("field-callsite", params
, &value
, &found
);
583 pretty
->options
.print_callsite_field
= value
;
587 bt_value_put_ref(pretty
->plugin_opt_map
);
588 pretty
->plugin_opt_map
= NULL
;
594 void set_use_colors(struct pretty_component
*pretty
)
596 switch (pretty
->options
.color
) {
597 case PRETTY_COLOR_OPT_ALWAYS
:
598 pretty
->use_colors
= true;
600 case PRETTY_COLOR_OPT_AUTO
:
601 pretty
->use_colors
= pretty
->out
== stdout
&&
602 bt_common_colors_supported();
604 case PRETTY_COLOR_OPT_NEVER
:
605 pretty
->use_colors
= false;
611 bt_component_class_init_method_status
pretty_init(
612 bt_self_component_sink
*comp
, const bt_value
*params
,
613 __attribute__((unused
)) void *init_method_data
)
615 bt_component_class_init_method_status ret
=
616 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
617 struct pretty_component
*pretty
= create_pretty();
620 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
624 if (bt_self_component_sink_add_input_port(comp
,
625 in_port_name
, NULL
, NULL
) < 0) {
626 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
630 pretty
->out
= stdout
;
631 pretty
->err
= stderr
;
633 pretty
->delta_cycles
= -1ULL;
634 pretty
->last_cycles_timestamp
= -1ULL;
636 pretty
->delta_real_timestamp
= -1ULL;
637 pretty
->last_real_timestamp
= -1ULL;
639 if (apply_params(pretty
, params
)) {
640 ret
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
644 set_use_colors(pretty
);
645 bt_self_component_set_data(
646 bt_self_component_sink_as_self_component(comp
), pretty
);
652 destroy_pretty_data(pretty
);