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_COMP_LOG_SELF_COMP self_comp
27 #define BT_LOG_OUTPUT_LEVEL log_level
28 #define BT_LOG_TAG "PLUGIN/SINK.TEXT.PRETTY"
29 #include "logging/comp-logging.h"
31 #include <babeltrace2/babeltrace.h>
32 #include "compat/compiler.h"
33 #include "common/common.h"
38 #include "common/assert.h"
39 #include "plugins/common/param-validation/param-validation.h"
44 const char * const in_port_name
= "in";
47 void destroy_pretty_data(struct pretty_component
*pretty
)
53 bt_self_component_port_input_message_iterator_put_ref(pretty
->iterator
);
56 (void) g_string_free(pretty
->string
, TRUE
);
59 if (pretty
->tmp_string
) {
60 (void) g_string_free(pretty
->tmp_string
, TRUE
);
63 if (pretty
->out
!= stdout
) {
66 ret
= fclose(pretty
->out
);
68 perror("close output file");
71 g_free(pretty
->options
.output_path
);
79 struct pretty_component
*create_pretty(void)
81 struct pretty_component
*pretty
;
83 pretty
= g_new0(struct pretty_component
, 1);
87 pretty
->string
= g_string_new("");
88 if (!pretty
->string
) {
91 pretty
->tmp_string
= g_string_new("");
92 if (!pretty
->tmp_string
) {
104 void pretty_finalize(bt_self_component_sink
*comp
)
107 bt_self_component_get_data(
108 bt_self_component_sink_as_self_component(comp
)));
112 bt_component_class_message_iterator_next_method_status
handle_message(
113 struct pretty_component
*pretty
,
114 const bt_message
*message
)
116 bt_component_class_message_iterator_next_method_status ret
=
117 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
119 BT_ASSERT_DBG(pretty
);
121 switch (bt_message_get_type(message
)) {
122 case BT_MESSAGE_TYPE_EVENT
:
123 if (pretty_print_event(pretty
, message
)) {
124 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
127 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
128 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
129 if (pretty_print_discarded_items(pretty
, message
)) {
130 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
141 bt_component_class_sink_graph_is_configured_method_status
142 pretty_graph_is_configured(bt_self_component_sink
*comp
)
144 bt_component_class_sink_graph_is_configured_method_status status
;
145 bt_self_component_port_input_message_iterator_create_from_sink_component_status
147 struct pretty_component
*pretty
;
149 pretty
= bt_self_component_get_data(
150 bt_self_component_sink_as_self_component(comp
));
152 BT_ASSERT(!pretty
->iterator
);
153 msg_iter_status
= bt_self_component_port_input_message_iterator_create_from_sink_component(
154 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
155 in_port_name
), &pretty
->iterator
);
156 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
157 status
= (int) msg_iter_status
;
161 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
168 bt_component_class_sink_consume_method_status
pretty_consume(
169 bt_self_component_sink
*comp
)
171 bt_component_class_sink_consume_method_status ret
=
172 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
173 bt_message_array_const msgs
;
174 bt_self_component_port_input_message_iterator
*it
;
175 struct pretty_component
*pretty
= bt_self_component_get_data(
176 bt_self_component_sink_as_self_component(comp
));
177 bt_message_iterator_next_status next_status
;
181 it
= pretty
->iterator
;
182 next_status
= bt_self_component_port_input_message_iterator_next(it
,
185 switch (next_status
) {
186 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
188 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
189 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
190 ret
= (int) next_status
;
192 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
193 ret
= (int) next_status
;
194 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
198 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
202 BT_ASSERT_DBG(next_status
== BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
);
204 for (i
= 0; i
< count
; i
++) {
205 ret
= (int) handle_message(pretty
, msgs
[i
]);
210 bt_message_put_ref(msgs
[i
]);
214 for (; i
< count
; i
++) {
215 bt_message_put_ref(msgs
[i
]);
222 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
224 const bt_value
*value
= NULL
;
227 value
= bt_value_map_borrow_entry_value_const(params
, key
);
232 str
= bt_value_string_get(value
);
233 *option
= g_strdup(str
);
240 * Apply parameter with key `key` to `option`. Use `def` as the value, if
241 * the parameter is not specified.
245 void apply_one_bool_with_default(const char *key
, const bt_value
*params
,
246 bool *option
, bool def
)
248 const bt_value
*value
;
250 value
= bt_value_map_borrow_entry_value_const(params
, key
);
252 bt_bool bool_val
= bt_value_bool_get(value
);
254 *option
= (bool) bool_val
;
261 void apply_one_bool_if_specified(const char *key
, const bt_value
*params
, bool *option
)
263 const bt_value
*value
;
265 value
= bt_value_map_borrow_entry_value_const(params
, key
);
267 bt_bool bool_val
= bt_value_bool_get(value
);
269 *option
= (bool) bool_val
;
274 int open_output_file(struct pretty_component
*pretty
)
278 if (!pretty
->options
.output_path
) {
282 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
296 static const char *color_choices
[] = { "never", "auto", "always", NULL
};
297 static const char *show_hide_choices
[] = { "show", "hide", NULL
};
299 struct bt_param_validation_map_value_entry_descr pretty_params
[] = {
300 { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
301 .choices
= color_choices
,
303 { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
304 { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
305 { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
306 { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
307 { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
308 { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
309 { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
311 { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
312 .choices
= show_hide_choices
,
314 { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
315 { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
316 { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
317 { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
319 { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
320 .choices
= show_hide_choices
,
322 { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
323 { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
324 { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
325 { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
326 { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
327 { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
328 { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
329 { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
330 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
334 bt_component_class_initialize_method_status
apply_params(
335 struct pretty_component
*pretty
, const bt_value
*params
,
336 bt_self_component
*self_comp
, bt_logging_level log_level
)
339 const bt_value
*value
;
340 bt_component_class_initialize_method_status status
;
341 enum bt_param_validation_status validation_status
;
342 gchar
*validate_error
= NULL
;
344 validation_status
= bt_param_validation_validate(params
,
345 pretty_params
, &validate_error
);
346 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
347 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
349 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
350 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
351 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validate_error
);
355 /* Known parameters. */
356 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
357 value
= bt_value_map_borrow_entry_value_const(params
, "color");
359 const char *color
= bt_value_string_get(value
);
361 if (strcmp(color
, "never") == 0) {
362 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
363 } else if (strcmp(color
, "auto") == 0) {
364 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
366 BT_ASSERT(strcmp(color
, "always") == 0);
367 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
371 apply_one_string("path", params
, &pretty
->options
.output_path
);
372 ret
= open_output_file(pretty
);
374 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
378 apply_one_bool_with_default("no-delta", params
,
379 &pretty
->options
.print_delta_field
, false);
381 pretty
->options
.print_delta_field
= !pretty
->options
.print_delta_field
;
383 apply_one_bool_with_default("clock-cycles", params
,
384 &pretty
->options
.print_timestamp_cycles
, false);
386 apply_one_bool_with_default("clock-seconds", params
,
387 &pretty
->options
.clock_seconds
, false);
389 apply_one_bool_with_default("clock-date", params
,
390 &pretty
->options
.clock_date
, false);
392 apply_one_bool_with_default("clock-gmt", params
,
393 &pretty
->options
.clock_gmt
, false);
395 apply_one_bool_with_default("verbose", params
,
396 &pretty
->options
.verbose
, false);
399 value
= bt_value_map_borrow_entry_value_const(params
, "name-default");
401 const char *str
= bt_value_string_get(value
);
403 if (strcmp(str
, "show") == 0) {
404 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
406 BT_ASSERT(strcmp(str
, "hide") == 0);
407 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
410 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
413 switch (pretty
->options
.name_default
) {
414 case PRETTY_DEFAULT_UNSET
:
415 pretty
->options
.print_payload_field_names
= true;
416 pretty
->options
.print_context_field_names
= true;
417 pretty
->options
.print_header_field_names
= false;
418 pretty
->options
.print_scope_field_names
= false;
420 case PRETTY_DEFAULT_SHOW
:
421 pretty
->options
.print_payload_field_names
= true;
422 pretty
->options
.print_context_field_names
= true;
423 pretty
->options
.print_header_field_names
= true;
424 pretty
->options
.print_scope_field_names
= true;
426 case PRETTY_DEFAULT_HIDE
:
427 pretty
->options
.print_payload_field_names
= false;
428 pretty
->options
.print_context_field_names
= false;
429 pretty
->options
.print_header_field_names
= false;
430 pretty
->options
.print_scope_field_names
= false;
436 apply_one_bool_if_specified("name-payload", params
,
437 &pretty
->options
.print_payload_field_names
);
439 apply_one_bool_if_specified("name-context", params
,
440 &pretty
->options
.print_context_field_names
);
442 apply_one_bool_if_specified("name-header", params
,
443 &pretty
->options
.print_header_field_names
);
445 apply_one_bool_if_specified("name-scope", params
,
446 &pretty
->options
.print_scope_field_names
);
449 value
= bt_value_map_borrow_entry_value_const(params
, "field-default");
451 const char *str
= bt_value_string_get(value
);
453 if (strcmp(str
, "show") == 0) {
454 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
456 BT_ASSERT(strcmp(str
, "hide") == 0);
457 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
460 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
463 switch (pretty
->options
.field_default
) {
464 case PRETTY_DEFAULT_UNSET
:
465 pretty
->options
.print_trace_field
= false;
466 pretty
->options
.print_trace_hostname_field
= true;
467 pretty
->options
.print_trace_domain_field
= false;
468 pretty
->options
.print_trace_procname_field
= true;
469 pretty
->options
.print_trace_vpid_field
= true;
470 pretty
->options
.print_loglevel_field
= false;
471 pretty
->options
.print_emf_field
= false;
472 pretty
->options
.print_callsite_field
= false;
474 case PRETTY_DEFAULT_SHOW
:
475 pretty
->options
.print_trace_field
= true;
476 pretty
->options
.print_trace_hostname_field
= true;
477 pretty
->options
.print_trace_domain_field
= true;
478 pretty
->options
.print_trace_procname_field
= true;
479 pretty
->options
.print_trace_vpid_field
= true;
480 pretty
->options
.print_loglevel_field
= true;
481 pretty
->options
.print_emf_field
= true;
482 pretty
->options
.print_callsite_field
= true;
484 case PRETTY_DEFAULT_HIDE
:
485 pretty
->options
.print_trace_field
= false;
486 pretty
->options
.print_trace_hostname_field
= false;
487 pretty
->options
.print_trace_domain_field
= false;
488 pretty
->options
.print_trace_procname_field
= false;
489 pretty
->options
.print_trace_vpid_field
= false;
490 pretty
->options
.print_loglevel_field
= false;
491 pretty
->options
.print_emf_field
= false;
492 pretty
->options
.print_callsite_field
= false;
498 apply_one_bool_if_specified("field-trace", params
,
499 &pretty
->options
.print_trace_field
);
501 apply_one_bool_if_specified("field-trace:hostname", params
,
502 &pretty
->options
.print_trace_hostname_field
);
504 apply_one_bool_if_specified("field-trace:domain", params
,
505 &pretty
->options
.print_trace_domain_field
);
507 apply_one_bool_if_specified("field-trace:procname", params
,
508 &pretty
->options
.print_trace_procname_field
);
510 apply_one_bool_if_specified("field-trace:vpid", params
,
511 &pretty
->options
.print_trace_vpid_field
);
513 apply_one_bool_if_specified("field-loglevel", params
,
514 &pretty
->options
.print_loglevel_field
);
516 apply_one_bool_if_specified("field-emf", params
,
517 &pretty
->options
.print_emf_field
);
519 apply_one_bool_if_specified("field-callsite", params
,
520 &pretty
->options
.print_callsite_field
);
522 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
525 g_free(validate_error
);
531 void set_use_colors(struct pretty_component
*pretty
)
533 switch (pretty
->options
.color
) {
534 case PRETTY_COLOR_OPT_ALWAYS
:
535 pretty
->use_colors
= true;
537 case PRETTY_COLOR_OPT_AUTO
:
538 pretty
->use_colors
= pretty
->out
== stdout
&&
539 bt_common_colors_supported();
541 case PRETTY_COLOR_OPT_NEVER
:
542 pretty
->use_colors
= false;
548 bt_component_class_initialize_method_status
pretty_init(
549 bt_self_component_sink
*self_comp_sink
,
550 bt_self_component_sink_configuration
*config
,
551 const bt_value
*params
,
552 __attribute__((unused
)) void *init_method_data
)
554 bt_component_class_initialize_method_status status
;
555 bt_self_component_add_port_status add_port_status
;
556 struct pretty_component
*pretty
= create_pretty();
557 bt_self_component
*self_comp
=
558 bt_self_component_sink_as_self_component(self_comp_sink
);
559 const bt_component
*comp
= bt_self_component_as_component(self_comp
);
560 bt_logging_level log_level
= bt_component_get_logging_level(comp
);
563 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
567 add_port_status
= bt_self_component_sink_add_input_port(self_comp_sink
,
568 in_port_name
, NULL
, NULL
);
569 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
570 status
= (int) add_port_status
;
574 pretty
->out
= stdout
;
575 pretty
->err
= stderr
;
577 pretty
->delta_cycles
= -1ULL;
578 pretty
->last_cycles_timestamp
= -1ULL;
580 pretty
->delta_real_timestamp
= -1ULL;
581 pretty
->last_real_timestamp
= -1ULL;
583 status
= apply_params(pretty
, params
, self_comp
, log_level
);
584 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
588 set_use_colors(pretty
);
589 bt_self_component_set_data(self_comp
, pretty
);
591 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
595 destroy_pretty_data(pretty
);