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_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_message_iterator_class_next_method_status
handle_message(
113 struct pretty_component
*pretty
,
114 const bt_message
*message
)
116 bt_message_iterator_class_next_method_status ret
=
117 BT_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
141 bt_component_class_sink_graph_is_configured_method_status
142 pretty_graph_is_configured(bt_self_component_sink
*self_comp_sink
)
144 bt_component_class_sink_graph_is_configured_method_status status
;
145 bt_message_iterator_create_from_sink_component_status
147 struct pretty_component
*pretty
;
148 bt_self_component
*self_comp
=
149 bt_self_component_sink_as_self_component(self_comp_sink
);
150 const bt_component
*comp
= bt_self_component_as_component(self_comp
);
151 bt_self_component_port_input
*in_port
;
152 bt_logging_level log_level
= bt_component_get_logging_level(comp
);
154 pretty
= bt_self_component_get_data(self_comp
);
156 BT_ASSERT(!pretty
->iterator
);
158 in_port
= bt_self_component_sink_borrow_input_port_by_name(self_comp_sink
,
160 if (!bt_port_is_connected(bt_port_input_as_port_const(
161 bt_self_component_port_input_as_port_input(in_port
)))) {
162 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Single input port is not connected: "
163 "port-name=\"%s\"", in_port_name
);
164 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR
;
168 msg_iter_status
= bt_message_iterator_create_from_sink_component(
169 self_comp_sink
, in_port
, &pretty
->iterator
);
170 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
171 status
= (int) msg_iter_status
;
175 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
182 bt_component_class_sink_consume_method_status
pretty_consume(
183 bt_self_component_sink
*comp
)
185 bt_component_class_sink_consume_method_status ret
=
186 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
187 bt_message_array_const msgs
;
188 bt_message_iterator
*it
;
189 struct pretty_component
*pretty
= bt_self_component_get_data(
190 bt_self_component_sink_as_self_component(comp
));
191 bt_message_iterator_next_status next_status
;
195 it
= pretty
->iterator
;
196 next_status
= bt_message_iterator_next(it
,
199 switch (next_status
) {
200 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
202 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
203 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
204 ret
= (int) next_status
;
206 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
207 ret
= (int) next_status
;
208 BT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
212 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
216 BT_ASSERT_DBG(next_status
== BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
);
218 for (i
= 0; i
< count
; i
++) {
219 ret
= (int) handle_message(pretty
, msgs
[i
]);
224 bt_message_put_ref(msgs
[i
]);
228 for (; i
< count
; i
++) {
229 bt_message_put_ref(msgs
[i
]);
236 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
238 const bt_value
*value
= NULL
;
241 value
= bt_value_map_borrow_entry_value_const(params
, key
);
246 str
= bt_value_string_get(value
);
247 *option
= g_strdup(str
);
254 * Apply parameter with key `key` to `option`. Use `def` as the value, if
255 * the parameter is not specified.
259 void apply_one_bool_with_default(const char *key
, const bt_value
*params
,
260 bool *option
, bool def
)
262 const bt_value
*value
;
264 value
= bt_value_map_borrow_entry_value_const(params
, key
);
266 bt_bool bool_val
= bt_value_bool_get(value
);
268 *option
= (bool) bool_val
;
275 void apply_one_bool_if_specified(const char *key
, const bt_value
*params
, bool *option
)
277 const bt_value
*value
;
279 value
= bt_value_map_borrow_entry_value_const(params
, key
);
281 bt_bool bool_val
= bt_value_bool_get(value
);
283 *option
= (bool) bool_val
;
288 int open_output_file(struct pretty_component
*pretty
)
292 if (!pretty
->options
.output_path
) {
296 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
310 static const char *color_choices
[] = { "never", "auto", "always", NULL
};
311 static const char *show_hide_choices
[] = { "show", "hide", NULL
};
314 struct bt_param_validation_map_value_entry_descr pretty_params
[] = {
315 { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
316 .choices
= color_choices
,
318 { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
319 { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
320 { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
321 { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
322 { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
323 { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
324 { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
326 { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
327 .choices
= show_hide_choices
,
329 { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
330 { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
331 { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
332 { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
334 { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
335 .choices
= show_hide_choices
,
337 { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
338 { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
339 { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
340 { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
341 { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
342 { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
343 { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
344 { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
345 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
349 bt_component_class_initialize_method_status
apply_params(
350 struct pretty_component
*pretty
, const bt_value
*params
,
351 bt_self_component
*self_comp
, bt_logging_level log_level
)
354 const bt_value
*value
;
355 bt_component_class_initialize_method_status status
;
356 enum bt_param_validation_status validation_status
;
357 gchar
*validate_error
= NULL
;
359 validation_status
= bt_param_validation_validate(params
,
360 pretty_params
, &validate_error
);
361 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
362 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
364 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
365 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
366 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validate_error
);
370 /* Known parameters. */
371 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
372 value
= bt_value_map_borrow_entry_value_const(params
, "color");
374 const char *color
= bt_value_string_get(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
;
381 BT_ASSERT(strcmp(color
, "always") == 0);
382 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
386 apply_one_string("path", params
, &pretty
->options
.output_path
);
387 ret
= open_output_file(pretty
);
389 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
393 apply_one_bool_with_default("no-delta", params
,
394 &pretty
->options
.print_delta_field
, false);
396 pretty
->options
.print_delta_field
= !pretty
->options
.print_delta_field
;
398 apply_one_bool_with_default("clock-cycles", params
,
399 &pretty
->options
.print_timestamp_cycles
, false);
401 apply_one_bool_with_default("clock-seconds", params
,
402 &pretty
->options
.clock_seconds
, false);
404 apply_one_bool_with_default("clock-date", params
,
405 &pretty
->options
.clock_date
, false);
407 apply_one_bool_with_default("clock-gmt", params
,
408 &pretty
->options
.clock_gmt
, false);
410 apply_one_bool_with_default("verbose", params
,
411 &pretty
->options
.verbose
, false);
414 value
= bt_value_map_borrow_entry_value_const(params
, "name-default");
416 const char *str
= bt_value_string_get(value
);
418 if (strcmp(str
, "show") == 0) {
419 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
421 BT_ASSERT(strcmp(str
, "hide") == 0);
422 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
425 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
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;
451 apply_one_bool_if_specified("name-payload", params
,
452 &pretty
->options
.print_payload_field_names
);
454 apply_one_bool_if_specified("name-context", params
,
455 &pretty
->options
.print_context_field_names
);
457 apply_one_bool_if_specified("name-header", params
,
458 &pretty
->options
.print_header_field_names
);
460 apply_one_bool_if_specified("name-scope", params
,
461 &pretty
->options
.print_scope_field_names
);
464 value
= bt_value_map_borrow_entry_value_const(params
, "field-default");
466 const char *str
= bt_value_string_get(value
);
468 if (strcmp(str
, "show") == 0) {
469 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
471 BT_ASSERT(strcmp(str
, "hide") == 0);
472 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
475 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
478 switch (pretty
->options
.field_default
) {
479 case PRETTY_DEFAULT_UNSET
:
480 pretty
->options
.print_trace_field
= false;
481 pretty
->options
.print_trace_hostname_field
= true;
482 pretty
->options
.print_trace_domain_field
= false;
483 pretty
->options
.print_trace_procname_field
= true;
484 pretty
->options
.print_trace_vpid_field
= true;
485 pretty
->options
.print_loglevel_field
= false;
486 pretty
->options
.print_emf_field
= false;
487 pretty
->options
.print_callsite_field
= false;
489 case PRETTY_DEFAULT_SHOW
:
490 pretty
->options
.print_trace_field
= true;
491 pretty
->options
.print_trace_hostname_field
= true;
492 pretty
->options
.print_trace_domain_field
= true;
493 pretty
->options
.print_trace_procname_field
= true;
494 pretty
->options
.print_trace_vpid_field
= true;
495 pretty
->options
.print_loglevel_field
= true;
496 pretty
->options
.print_emf_field
= true;
497 pretty
->options
.print_callsite_field
= true;
499 case PRETTY_DEFAULT_HIDE
:
500 pretty
->options
.print_trace_field
= false;
501 pretty
->options
.print_trace_hostname_field
= false;
502 pretty
->options
.print_trace_domain_field
= false;
503 pretty
->options
.print_trace_procname_field
= false;
504 pretty
->options
.print_trace_vpid_field
= false;
505 pretty
->options
.print_loglevel_field
= false;
506 pretty
->options
.print_emf_field
= false;
507 pretty
->options
.print_callsite_field
= false;
513 apply_one_bool_if_specified("field-trace", params
,
514 &pretty
->options
.print_trace_field
);
516 apply_one_bool_if_specified("field-trace:hostname", params
,
517 &pretty
->options
.print_trace_hostname_field
);
519 apply_one_bool_if_specified("field-trace:domain", params
,
520 &pretty
->options
.print_trace_domain_field
);
522 apply_one_bool_if_specified("field-trace:procname", params
,
523 &pretty
->options
.print_trace_procname_field
);
525 apply_one_bool_if_specified("field-trace:vpid", params
,
526 &pretty
->options
.print_trace_vpid_field
);
528 apply_one_bool_if_specified("field-loglevel", params
,
529 &pretty
->options
.print_loglevel_field
);
531 apply_one_bool_if_specified("field-emf", params
,
532 &pretty
->options
.print_emf_field
);
534 apply_one_bool_if_specified("field-callsite", params
,
535 &pretty
->options
.print_callsite_field
);
538 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
541 g_free(validate_error
);
547 void set_use_colors(struct pretty_component
*pretty
)
549 switch (pretty
->options
.color
) {
550 case PRETTY_COLOR_OPT_ALWAYS
:
551 pretty
->use_colors
= true;
553 case PRETTY_COLOR_OPT_AUTO
:
554 pretty
->use_colors
= pretty
->out
== stdout
&&
555 bt_common_colors_supported();
557 case PRETTY_COLOR_OPT_NEVER
:
558 pretty
->use_colors
= false;
564 bt_component_class_initialize_method_status
pretty_init(
565 bt_self_component_sink
*self_comp_sink
,
566 bt_self_component_sink_configuration
*config
,
567 const bt_value
*params
,
568 __attribute__((unused
)) void *init_method_data
)
570 bt_component_class_initialize_method_status status
;
571 bt_self_component_add_port_status add_port_status
;
572 struct pretty_component
*pretty
= create_pretty();
573 bt_self_component
*self_comp
=
574 bt_self_component_sink_as_self_component(self_comp_sink
);
575 const bt_component
*comp
= bt_self_component_as_component(self_comp
);
576 bt_logging_level log_level
= bt_component_get_logging_level(comp
);
579 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
583 add_port_status
= bt_self_component_sink_add_input_port(self_comp_sink
,
584 in_port_name
, NULL
, NULL
);
585 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
586 status
= (int) add_port_status
;
590 pretty
->out
= stdout
;
591 pretty
->err
= stderr
;
593 pretty
->delta_cycles
= -1ULL;
594 pretty
->last_cycles_timestamp
= -1ULL;
596 pretty
->delta_real_timestamp
= -1ULL;
597 pretty
->last_real_timestamp
= -1ULL;
599 status
= apply_params(pretty
, params
, self_comp
, log_level
);
600 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
604 set_use_colors(pretty
);
605 bt_self_component_set_data(self_comp
, pretty
);
607 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
611 destroy_pretty_data(pretty
);