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
)
49 bt_self_component_port_input_message_iterator_put_ref(pretty
->iterator
);
52 (void) g_string_free(pretty
->string
, TRUE
);
55 if (pretty
->tmp_string
) {
56 (void) g_string_free(pretty
->tmp_string
, TRUE
);
59 if (pretty
->out
!= stdout
) {
62 ret
= fclose(pretty
->out
);
64 perror("close output file");
67 g_free(pretty
->options
.output_path
);
72 struct pretty_component
*create_pretty(void)
74 struct pretty_component
*pretty
;
76 pretty
= g_new0(struct pretty_component
, 1);
80 pretty
->string
= g_string_new("");
81 if (!pretty
->string
) {
84 pretty
->tmp_string
= g_string_new("");
85 if (!pretty
->tmp_string
) {
97 void pretty_finalize(bt_self_component_sink
*comp
)
100 bt_self_component_get_data(
101 bt_self_component_sink_as_self_component(comp
)));
105 bt_component_class_message_iterator_next_method_status
handle_message(
106 struct pretty_component
*pretty
,
107 const bt_message
*message
)
109 bt_component_class_message_iterator_next_method_status ret
=
110 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
114 switch (bt_message_get_type(message
)) {
115 case BT_MESSAGE_TYPE_EVENT
:
116 if (pretty_print_event(pretty
, message
)) {
117 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
120 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
121 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
122 if (pretty_print_discarded_items(pretty
, message
)) {
123 ret
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
134 bt_component_class_sink_graph_is_configured_method_status
135 pretty_graph_is_configured(bt_self_component_sink
*comp
)
137 bt_component_class_sink_graph_is_configured_method_status status
;
138 bt_self_component_port_input_message_iterator_create_from_sink_component_status
140 struct pretty_component
*pretty
;
142 pretty
= bt_self_component_get_data(
143 bt_self_component_sink_as_self_component(comp
));
145 BT_ASSERT(!pretty
->iterator
);
146 msg_iter_status
= bt_self_component_port_input_message_iterator_create_from_sink_component(
147 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
148 in_port_name
), &pretty
->iterator
);
149 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
150 status
= (int) msg_iter_status
;
154 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
161 bt_component_class_sink_consume_method_status
pretty_consume(
162 bt_self_component_sink
*comp
)
164 bt_component_class_sink_consume_method_status ret
=
165 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
166 bt_message_array_const msgs
;
167 bt_self_component_port_input_message_iterator
*it
;
168 struct pretty_component
*pretty
= bt_self_component_get_data(
169 bt_self_component_sink_as_self_component(comp
));
170 bt_message_iterator_next_status next_status
;
174 it
= pretty
->iterator
;
175 next_status
= bt_self_component_port_input_message_iterator_next(it
,
178 switch (next_status
) {
179 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
181 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
182 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
183 ret
= (int) next_status
;
185 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
186 ret
= (int) next_status
;
187 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
191 ret
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR
;
195 BT_ASSERT(next_status
== BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
);
197 for (i
= 0; i
< count
; i
++) {
198 ret
= (int) handle_message(pretty
, msgs
[i
]);
203 bt_message_put_ref(msgs
[i
]);
207 for (; i
< count
; i
++) {
208 bt_message_put_ref(msgs
[i
]);
215 void apply_one_string(const char *key
, const bt_value
*params
, char **option
)
217 const bt_value
*value
= NULL
;
220 value
= bt_value_map_borrow_entry_value_const(params
, key
);
225 str
= bt_value_string_get(value
);
226 *option
= g_strdup(str
);
233 * Apply parameter with key `key` to `option`. Use `def` as the value, if
234 * the parameter is not specified.
238 void apply_one_bool_with_default(const char *key
, const bt_value
*params
,
239 bool *option
, bool def
)
241 const bt_value
*value
;
243 value
= bt_value_map_borrow_entry_value_const(params
, key
);
245 bt_bool bool_val
= bt_value_bool_get(value
);
247 *option
= (bool) bool_val
;
254 void apply_one_bool_if_specified(const char *key
, const bt_value
*params
, bool *option
)
256 const bt_value
*value
;
258 value
= bt_value_map_borrow_entry_value_const(params
, key
);
260 bt_bool bool_val
= bt_value_bool_get(value
);
262 *option
= (bool) bool_val
;
267 int open_output_file(struct pretty_component
*pretty
)
271 if (!pretty
->options
.output_path
) {
275 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
289 static const char *color_choices
[] = { "never", "auto", "always", NULL
};
290 static const char *show_hide_choices
[] = { "show", "hide", NULL
};
292 struct bt_param_validation_map_value_entry_descr pretty_params
[] = {
293 { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
294 .choices
= color_choices
,
296 { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
297 { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
298 { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
299 { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
300 { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
301 { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
302 { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
304 { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
305 .choices
= show_hide_choices
,
307 { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
308 { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
309 { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
310 { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
312 { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { BT_VALUE_TYPE_STRING
, .string
= {
313 .choices
= show_hide_choices
,
315 { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
316 { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
317 { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
318 { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
319 { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
320 { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
321 { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
322 { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
323 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
327 bt_component_class_initialize_method_status
apply_params(
328 struct pretty_component
*pretty
, const bt_value
*params
,
329 bt_self_component
*self_comp
, bt_logging_level log_level
)
332 const bt_value
*value
;
333 bt_component_class_initialize_method_status status
;
334 enum bt_param_validation_status validation_status
;
335 gchar
*validate_error
= NULL
;
337 validation_status
= bt_param_validation_validate(params
,
338 pretty_params
, &validate_error
);
339 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
340 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
342 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
343 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
344 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validate_error
);
348 /* Known parameters. */
349 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
350 value
= bt_value_map_borrow_entry_value_const(params
, "color");
352 const char *color
= bt_value_string_get(value
);
354 if (strcmp(color
, "never") == 0) {
355 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
356 } else if (strcmp(color
, "auto") == 0) {
357 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
359 BT_ASSERT(strcmp(color
, "always") == 0);
360 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
364 apply_one_string("path", params
, &pretty
->options
.output_path
);
365 ret
= open_output_file(pretty
);
367 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
371 apply_one_bool_with_default("no-delta", params
,
372 &pretty
->options
.print_delta_field
, false);
374 pretty
->options
.print_delta_field
= !pretty
->options
.print_delta_field
;
376 apply_one_bool_with_default("clock-cycles", params
,
377 &pretty
->options
.print_timestamp_cycles
, false);
379 apply_one_bool_with_default("clock-seconds", params
,
380 &pretty
->options
.clock_seconds
, false);
382 apply_one_bool_with_default("clock-date", params
,
383 &pretty
->options
.clock_date
, false);
385 apply_one_bool_with_default("clock-gmt", params
,
386 &pretty
->options
.clock_gmt
, false);
388 apply_one_bool_with_default("verbose", params
,
389 &pretty
->options
.verbose
, false);
392 value
= bt_value_map_borrow_entry_value_const(params
, "name-default");
394 const char *str
= bt_value_string_get(value
);
396 if (strcmp(str
, "show") == 0) {
397 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
399 BT_ASSERT(strcmp(str
, "hide") == 0);
400 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
403 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
406 switch (pretty
->options
.name_default
) {
407 case PRETTY_DEFAULT_UNSET
:
408 pretty
->options
.print_payload_field_names
= true;
409 pretty
->options
.print_context_field_names
= true;
410 pretty
->options
.print_header_field_names
= false;
411 pretty
->options
.print_scope_field_names
= false;
413 case PRETTY_DEFAULT_SHOW
:
414 pretty
->options
.print_payload_field_names
= true;
415 pretty
->options
.print_context_field_names
= true;
416 pretty
->options
.print_header_field_names
= true;
417 pretty
->options
.print_scope_field_names
= true;
419 case PRETTY_DEFAULT_HIDE
:
420 pretty
->options
.print_payload_field_names
= false;
421 pretty
->options
.print_context_field_names
= false;
422 pretty
->options
.print_header_field_names
= false;
423 pretty
->options
.print_scope_field_names
= false;
429 apply_one_bool_if_specified("name-payload", params
,
430 &pretty
->options
.print_payload_field_names
);
432 apply_one_bool_if_specified("name-context", params
,
433 &pretty
->options
.print_context_field_names
);
435 apply_one_bool_if_specified("name-header", params
,
436 &pretty
->options
.print_header_field_names
);
438 apply_one_bool_if_specified("name-scope", params
,
439 &pretty
->options
.print_scope_field_names
);
442 value
= bt_value_map_borrow_entry_value_const(params
, "field-default");
444 const char *str
= bt_value_string_get(value
);
446 if (strcmp(str
, "show") == 0) {
447 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
449 BT_ASSERT(strcmp(str
, "hide") == 0);
450 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
453 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
456 switch (pretty
->options
.field_default
) {
457 case PRETTY_DEFAULT_UNSET
:
458 pretty
->options
.print_trace_field
= false;
459 pretty
->options
.print_trace_hostname_field
= true;
460 pretty
->options
.print_trace_domain_field
= false;
461 pretty
->options
.print_trace_procname_field
= true;
462 pretty
->options
.print_trace_vpid_field
= true;
463 pretty
->options
.print_loglevel_field
= false;
464 pretty
->options
.print_emf_field
= false;
465 pretty
->options
.print_callsite_field
= false;
467 case PRETTY_DEFAULT_SHOW
:
468 pretty
->options
.print_trace_field
= true;
469 pretty
->options
.print_trace_hostname_field
= true;
470 pretty
->options
.print_trace_domain_field
= true;
471 pretty
->options
.print_trace_procname_field
= true;
472 pretty
->options
.print_trace_vpid_field
= true;
473 pretty
->options
.print_loglevel_field
= true;
474 pretty
->options
.print_emf_field
= true;
475 pretty
->options
.print_callsite_field
= true;
477 case PRETTY_DEFAULT_HIDE
:
478 pretty
->options
.print_trace_field
= false;
479 pretty
->options
.print_trace_hostname_field
= false;
480 pretty
->options
.print_trace_domain_field
= false;
481 pretty
->options
.print_trace_procname_field
= false;
482 pretty
->options
.print_trace_vpid_field
= false;
483 pretty
->options
.print_loglevel_field
= false;
484 pretty
->options
.print_emf_field
= false;
485 pretty
->options
.print_callsite_field
= false;
491 apply_one_bool_if_specified("field-trace", params
,
492 &pretty
->options
.print_trace_field
);
494 apply_one_bool_if_specified("field-trace:hostname", params
,
495 &pretty
->options
.print_trace_hostname_field
);
497 apply_one_bool_if_specified("field-trace:domain", params
,
498 &pretty
->options
.print_trace_domain_field
);
500 apply_one_bool_if_specified("field-trace:procname", params
,
501 &pretty
->options
.print_trace_procname_field
);
503 apply_one_bool_if_specified("field-trace:vpid", params
,
504 &pretty
->options
.print_trace_vpid_field
);
506 apply_one_bool_if_specified("field-loglevel", params
,
507 &pretty
->options
.print_loglevel_field
);
509 apply_one_bool_if_specified("field-emf", params
,
510 &pretty
->options
.print_emf_field
);
512 apply_one_bool_if_specified("field-callsite", params
,
513 &pretty
->options
.print_callsite_field
);
515 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
518 g_free(validate_error
);
524 void set_use_colors(struct pretty_component
*pretty
)
526 switch (pretty
->options
.color
) {
527 case PRETTY_COLOR_OPT_ALWAYS
:
528 pretty
->use_colors
= true;
530 case PRETTY_COLOR_OPT_AUTO
:
531 pretty
->use_colors
= pretty
->out
== stdout
&&
532 bt_common_colors_supported();
534 case PRETTY_COLOR_OPT_NEVER
:
535 pretty
->use_colors
= false;
541 bt_component_class_initialize_method_status
pretty_init(
542 bt_self_component_sink
*self_comp_sink
,
543 bt_self_component_sink_configuration
*config
,
544 const bt_value
*params
,
545 __attribute__((unused
)) void *init_method_data
)
547 bt_component_class_initialize_method_status status
;
548 bt_self_component_add_port_status add_port_status
;
549 struct pretty_component
*pretty
= create_pretty();
550 bt_self_component
*self_comp
=
551 bt_self_component_sink_as_self_component(self_comp_sink
);
552 const bt_component
*comp
= bt_self_component_as_component(self_comp
);
553 bt_logging_level log_level
= bt_component_get_logging_level(comp
);
556 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
560 add_port_status
= bt_self_component_sink_add_input_port(self_comp_sink
,
561 in_port_name
, NULL
, NULL
);
562 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
563 status
= (int) add_port_status
;
567 pretty
->out
= stdout
;
568 pretty
->err
= stderr
;
570 pretty
->delta_cycles
= -1ULL;
571 pretty
->last_cycles_timestamp
= -1ULL;
573 pretty
->delta_real_timestamp
= -1ULL;
574 pretty
->last_real_timestamp
= -1ULL;
576 status
= apply_params(pretty
, params
, self_comp
, log_level
);
577 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
581 set_use_colors(pretty
);
582 bt_self_component_set_data(self_comp
, pretty
);
584 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
588 destroy_pretty_data(pretty
);