Fix: sink.text.pretty: assign status at the end of pretty_consume
[babeltrace.git] / src / plugins / text / pretty / pretty.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8 #define BT_COMP_LOG_SELF_COMP (pretty->self_comp)
9 #define BT_LOG_OUTPUT_LEVEL (pretty->log_level)
10 #define BT_LOG_TAG "PLUGIN/SINK.TEXT.PRETTY"
11 #include "logging/comp-logging.h"
12
13 #include <babeltrace2/babeltrace.h>
14 #include "compat/compiler.h"
15 #include "common/common.h"
16 #include <stdio.h>
17 #include <stdbool.h>
18 #include <glib.h>
19 #include <string.h>
20 #include "common/assert.h"
21 #include "plugins/common/param-validation/param-validation.h"
22
23 #include "pretty.h"
24
25 static
26 const char * const in_port_name = "in";
27
28 static
29 void destroy_pretty_data(struct pretty_component *pretty)
30 {
31 if (!pretty) {
32 goto end;
33 }
34
35 bt_message_iterator_put_ref(pretty->iterator);
36
37 if (pretty->string) {
38 (void) g_string_free(pretty->string, TRUE);
39 }
40
41 if (pretty->tmp_string) {
42 (void) g_string_free(pretty->tmp_string, TRUE);
43 }
44
45 if (pretty->out != stdout) {
46 int ret;
47
48 ret = fclose(pretty->out);
49 if (ret) {
50 perror("close output file");
51 }
52 }
53 g_free(pretty->options.output_path);
54 g_free(pretty);
55
56 end:
57 return;
58 }
59
60 static
61 struct pretty_component *create_pretty(void)
62 {
63 struct pretty_component *pretty;
64
65 pretty = g_new0(struct pretty_component, 1);
66 if (!pretty) {
67 goto end;
68 }
69 pretty->string = g_string_new("");
70 if (!pretty->string) {
71 goto error;
72 }
73 pretty->tmp_string = g_string_new("");
74 if (!pretty->tmp_string) {
75 goto error;
76 }
77 end:
78 return pretty;
79
80 error:
81 g_free(pretty);
82 return NULL;
83 }
84
85 BT_HIDDEN
86 void pretty_finalize(bt_self_component_sink *comp)
87 {
88 destroy_pretty_data(
89 bt_self_component_get_data(
90 bt_self_component_sink_as_self_component(comp)));
91 }
92
93 static
94 bt_message_iterator_class_next_method_status handle_message(
95 struct pretty_component *pretty,
96 const bt_message *message)
97 {
98 bt_message_iterator_class_next_method_status ret =
99 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
100
101 BT_ASSERT_DBG(pretty);
102
103 switch (bt_message_get_type(message)) {
104 case BT_MESSAGE_TYPE_EVENT:
105 if (pretty_print_event(pretty, message)) {
106 BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp,
107 "Failed to print one event.");
108 ret = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
109 }
110 break;
111 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
112 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
113 if (pretty_print_discarded_items(pretty, message)) {
114 BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp,
115 "Failed to print discarded items.");
116 ret = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
117 }
118 break;
119 default:
120 break;
121 }
122
123 return ret;
124 }
125
126 BT_HIDDEN
127 bt_component_class_sink_graph_is_configured_method_status
128 pretty_graph_is_configured(bt_self_component_sink *self_comp_sink)
129 {
130 bt_component_class_sink_graph_is_configured_method_status status;
131 bt_message_iterator_create_from_sink_component_status
132 msg_iter_status;
133 struct pretty_component *pretty;
134 bt_self_component *self_comp =
135 bt_self_component_sink_as_self_component(self_comp_sink);
136 bt_self_component_port_input *in_port;
137
138 pretty = bt_self_component_get_data(self_comp);
139 BT_ASSERT(pretty);
140 BT_ASSERT(!pretty->iterator);
141
142 in_port = bt_self_component_sink_borrow_input_port_by_name(self_comp_sink,
143 in_port_name);
144 if (!bt_port_is_connected(bt_port_input_as_port_const(
145 bt_self_component_port_input_as_port_input(in_port)))) {
146 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Single input port is not connected: "
147 "port-name=\"%s\"", in_port_name);
148 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
149 goto end;
150 }
151
152 msg_iter_status = bt_message_iterator_create_from_sink_component(
153 self_comp_sink, in_port, &pretty->iterator);
154 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
155 status = (int) msg_iter_status;
156 goto end;
157 }
158
159 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
160
161 end:
162 return status;
163 }
164
165 BT_HIDDEN
166 bt_component_class_sink_consume_method_status pretty_consume(
167 bt_self_component_sink *comp)
168 {
169 bt_component_class_sink_consume_method_status status;
170 bt_message_array_const msgs;
171 bt_message_iterator *it;
172 struct pretty_component *pretty = bt_self_component_get_data(
173 bt_self_component_sink_as_self_component(comp));
174 bt_message_iterator_next_status next_status;
175 uint64_t count = 0;
176 uint64_t i = 0;
177
178 it = pretty->iterator;
179 next_status = bt_message_iterator_next(it,
180 &msgs, &count);
181 if (next_status != BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) {
182 status = (int) next_status;
183 goto end;
184 }
185
186 for (i = 0; i < count; i++) {
187 status = (int) handle_message(pretty, msgs[i]);
188 if (status) {
189 goto end;
190 }
191
192 bt_message_put_ref(msgs[i]);
193 }
194
195 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
196
197 end:
198 for (; i < count; i++) {
199 bt_message_put_ref(msgs[i]);
200 }
201
202 return status;
203 }
204
205 static
206 void apply_one_string(const char *key, const bt_value *params, char **option)
207 {
208 const bt_value *value = NULL;
209 const char *str;
210
211 value = bt_value_map_borrow_entry_value_const(params, key);
212 if (!value) {
213 goto end;
214 }
215
216 str = bt_value_string_get(value);
217 *option = g_strdup(str);
218
219 end:
220 return;
221 }
222
223 /*
224 * Apply parameter with key `key` to `option`. Use `def` as the value, if
225 * the parameter is not specified.
226 */
227
228 static
229 void apply_one_bool_with_default(const char *key, const bt_value *params,
230 bool *option, bool def)
231 {
232 const bt_value *value;
233
234 value = bt_value_map_borrow_entry_value_const(params, key);
235 if (value) {
236 bt_bool bool_val = bt_value_bool_get(value);
237
238 *option = (bool) bool_val;
239 } else {
240 *option = def;
241 }
242 }
243
244 static
245 void apply_one_bool_if_specified(const char *key, const bt_value *params, bool *option)
246 {
247 const bt_value *value;
248
249 value = bt_value_map_borrow_entry_value_const(params, key);
250 if (value) {
251 bt_bool bool_val = bt_value_bool_get(value);
252
253 *option = (bool) bool_val;
254 }
255 }
256
257 static
258 int open_output_file(struct pretty_component *pretty)
259 {
260 int ret = 0;
261
262 if (!pretty->options.output_path) {
263 goto end;
264 }
265
266 pretty->out = fopen(pretty->options.output_path, "w");
267 if (!pretty->out) {
268 goto error;
269 }
270
271 goto end;
272
273 error:
274 ret = -1;
275
276 end:
277 return ret;
278 }
279
280 static const char *color_choices[] = { "never", "auto", "always", NULL };
281 static const char *show_hide_choices[] = { "show", "hide", NULL };
282
283 static
284 struct bt_param_validation_map_value_entry_descr pretty_params[] = {
285 { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = {
286 .choices = color_choices,
287 } } },
288 { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } },
289 { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
290 { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
291 { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
292 { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
293 { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
294 { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
295
296 { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = {
297 .choices = show_hide_choices,
298 } } },
299 { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
300 { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
301 { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
302 { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
303
304 { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = {
305 .choices = show_hide_choices,
306 } } },
307 { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
308 { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
309 { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
310 { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
311 { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
312 { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
313 { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
314 { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
315 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
316 };
317
318 static
319 bt_component_class_initialize_method_status apply_params(
320 struct pretty_component *pretty, const bt_value *params)
321 {
322 int ret;
323 const bt_value *value;
324 bt_component_class_initialize_method_status status;
325 enum bt_param_validation_status validation_status;
326 gchar *validate_error = NULL;
327
328 validation_status = bt_param_validation_validate(params,
329 pretty_params, &validate_error);
330 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
331 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
332 goto end;
333 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
334 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
335 BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, "%s", validate_error);
336 goto end;
337 }
338
339 /* Known parameters. */
340 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
341 value = bt_value_map_borrow_entry_value_const(params, "color");
342 if (value) {
343 const char *color = bt_value_string_get(value);
344
345 if (strcmp(color, "never") == 0) {
346 pretty->options.color = PRETTY_COLOR_OPT_NEVER;
347 } else if (strcmp(color, "auto") == 0) {
348 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
349 } else {
350 BT_ASSERT(strcmp(color, "always") == 0);
351 pretty->options.color = PRETTY_COLOR_OPT_ALWAYS;
352 }
353 }
354
355 apply_one_string("path", params, &pretty->options.output_path);
356 ret = open_output_file(pretty);
357 if (ret) {
358 BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp,
359 "Failed to open output file: %s", validate_error);
360 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
361 goto end;
362 }
363
364 apply_one_bool_with_default("no-delta", params,
365 &pretty->options.print_delta_field, false);
366 /* Reverse logic. */
367 pretty->options.print_delta_field = !pretty->options.print_delta_field;
368
369 apply_one_bool_with_default("clock-cycles", params,
370 &pretty->options.print_timestamp_cycles, false);
371
372 apply_one_bool_with_default("clock-seconds", params,
373 &pretty->options.clock_seconds , false);
374
375 apply_one_bool_with_default("clock-date", params,
376 &pretty->options.clock_date, false);
377
378 apply_one_bool_with_default("clock-gmt", params,
379 &pretty->options.clock_gmt, false);
380
381 apply_one_bool_with_default("verbose", params,
382 &pretty->options.verbose, false);
383
384 /* Names. */
385 value = bt_value_map_borrow_entry_value_const(params, "name-default");
386 if (value) {
387 const char *str = bt_value_string_get(value);
388
389 if (strcmp(str, "show") == 0) {
390 pretty->options.name_default = PRETTY_DEFAULT_SHOW;
391 } else {
392 BT_ASSERT(strcmp(str, "hide") == 0);
393 pretty->options.name_default = PRETTY_DEFAULT_HIDE;
394 }
395 } else {
396 pretty->options.name_default = PRETTY_DEFAULT_UNSET;
397 }
398
399 switch (pretty->options.name_default) {
400 case PRETTY_DEFAULT_UNSET:
401 pretty->options.print_payload_field_names = true;
402 pretty->options.print_context_field_names = true;
403 pretty->options.print_header_field_names = false;
404 pretty->options.print_scope_field_names = false;
405 break;
406 case PRETTY_DEFAULT_SHOW:
407 pretty->options.print_payload_field_names = true;
408 pretty->options.print_context_field_names = true;
409 pretty->options.print_header_field_names = true;
410 pretty->options.print_scope_field_names = true;
411 break;
412 case PRETTY_DEFAULT_HIDE:
413 pretty->options.print_payload_field_names = false;
414 pretty->options.print_context_field_names = false;
415 pretty->options.print_header_field_names = false;
416 pretty->options.print_scope_field_names = false;
417 break;
418 default:
419 bt_common_abort();
420 }
421
422 apply_one_bool_if_specified("name-payload", params,
423 &pretty->options.print_payload_field_names);
424
425 apply_one_bool_if_specified("name-context", params,
426 &pretty->options.print_context_field_names);
427
428 apply_one_bool_if_specified("name-header", params,
429 &pretty->options.print_header_field_names);
430
431 apply_one_bool_if_specified("name-scope", params,
432 &pretty->options.print_scope_field_names);
433
434 /* Fields. */
435 value = bt_value_map_borrow_entry_value_const(params, "field-default");
436 if (value) {
437 const char *str = bt_value_string_get(value);
438
439 if (strcmp(str, "show") == 0) {
440 pretty->options.field_default = PRETTY_DEFAULT_SHOW;
441 } else {
442 BT_ASSERT(strcmp(str, "hide") == 0);
443 pretty->options.field_default = PRETTY_DEFAULT_HIDE;
444 }
445 } else {
446 pretty->options.field_default = PRETTY_DEFAULT_UNSET;
447 }
448
449 switch (pretty->options.field_default) {
450 case PRETTY_DEFAULT_UNSET:
451 pretty->options.print_trace_field = false;
452 pretty->options.print_trace_hostname_field = true;
453 pretty->options.print_trace_domain_field = false;
454 pretty->options.print_trace_procname_field = true;
455 pretty->options.print_trace_vpid_field = true;
456 pretty->options.print_loglevel_field = false;
457 pretty->options.print_emf_field = false;
458 pretty->options.print_callsite_field = false;
459 break;
460 case PRETTY_DEFAULT_SHOW:
461 pretty->options.print_trace_field = true;
462 pretty->options.print_trace_hostname_field = true;
463 pretty->options.print_trace_domain_field = true;
464 pretty->options.print_trace_procname_field = true;
465 pretty->options.print_trace_vpid_field = true;
466 pretty->options.print_loglevel_field = true;
467 pretty->options.print_emf_field = true;
468 pretty->options.print_callsite_field = true;
469 break;
470 case PRETTY_DEFAULT_HIDE:
471 pretty->options.print_trace_field = false;
472 pretty->options.print_trace_hostname_field = false;
473 pretty->options.print_trace_domain_field = false;
474 pretty->options.print_trace_procname_field = false;
475 pretty->options.print_trace_vpid_field = false;
476 pretty->options.print_loglevel_field = false;
477 pretty->options.print_emf_field = false;
478 pretty->options.print_callsite_field = false;
479 break;
480 default:
481 bt_common_abort();
482 }
483
484 apply_one_bool_if_specified("field-trace", params,
485 &pretty->options.print_trace_field);
486
487 apply_one_bool_if_specified("field-trace:hostname", params,
488 &pretty->options.print_trace_hostname_field);
489
490 apply_one_bool_if_specified("field-trace:domain", params,
491 &pretty->options.print_trace_domain_field);
492
493 apply_one_bool_if_specified("field-trace:procname", params,
494 &pretty->options.print_trace_procname_field);
495
496 apply_one_bool_if_specified("field-trace:vpid", params,
497 &pretty->options.print_trace_vpid_field);
498
499 apply_one_bool_if_specified("field-loglevel", params,
500 &pretty->options.print_loglevel_field);
501
502 apply_one_bool_if_specified("field-emf", params,
503 &pretty->options.print_emf_field);
504
505 apply_one_bool_if_specified("field-callsite", params,
506 &pretty->options.print_callsite_field);
507
508 pretty_print_init();
509 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
510
511 end:
512 g_free(validate_error);
513
514 return status;
515 }
516
517 static
518 void set_use_colors(struct pretty_component *pretty)
519 {
520 switch (pretty->options.color) {
521 case PRETTY_COLOR_OPT_ALWAYS:
522 pretty->use_colors = true;
523 break;
524 case PRETTY_COLOR_OPT_AUTO:
525 pretty->use_colors = pretty->out == stdout &&
526 bt_common_colors_supported();
527 break;
528 case PRETTY_COLOR_OPT_NEVER:
529 pretty->use_colors = false;
530 break;
531 }
532 }
533
534 BT_HIDDEN
535 bt_component_class_initialize_method_status pretty_init(
536 bt_self_component_sink *self_comp_sink,
537 bt_self_component_sink_configuration *config,
538 const bt_value *params,
539 __attribute__((unused)) void *init_method_data)
540 {
541 bt_component_class_initialize_method_status status;
542 bt_self_component_add_port_status add_port_status;
543 struct pretty_component *pretty = create_pretty();
544 bt_self_component *self_comp =
545 bt_self_component_sink_as_self_component(self_comp_sink);
546 const bt_component *comp = bt_self_component_as_component(self_comp);
547 bt_logging_level log_level = bt_component_get_logging_level(comp);
548
549 if (!pretty) {
550 /*
551 * Don't use BT_COMP_LOGE_APPEND_CAUSE, as `pretty` is not
552 * initialized yet.
553 */
554 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
555 "Failed to allocate component.");
556 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
557 self_comp, "Failed to allocate component.");
558 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
559 goto error;
560 }
561
562 pretty->self_comp = self_comp;
563 pretty->log_level = log_level;
564
565 add_port_status = bt_self_component_sink_add_input_port(self_comp_sink,
566 in_port_name, NULL, NULL);
567 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
568 status = (int) add_port_status;
569 goto error;
570 }
571
572 pretty->out = stdout;
573 pretty->err = stderr;
574
575 pretty->delta_cycles = -1ULL;
576 pretty->last_cycles_timestamp = -1ULL;
577
578 pretty->delta_real_timestamp = -1ULL;
579 pretty->last_real_timestamp = -1ULL;
580
581 status = apply_params(pretty, params);
582 if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
583 goto error;
584 }
585
586 set_use_colors(pretty);
587 bt_self_component_set_data(self_comp, pretty);
588
589 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
590 goto end;
591
592 error:
593 destroy_pretty_data(pretty);
594
595 end:
596 return status;
597 }
This page took 0.042129 seconds and 4 git commands to generate.