lib: strictly type function return status enumerations
[babeltrace.git] / src / plugins / text / pretty / pretty.c
1 /*
2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * SOFTWARE.
24 */
25
26 #include <babeltrace2/babeltrace.h>
27 #include "compat/compiler.h"
28 #include "common/common.h"
29 #include <stdio.h>
30 #include <stdbool.h>
31 #include <glib.h>
32 #include <string.h>
33 #include "common/assert.h"
34
35 #include "pretty.h"
36
37 GQuark stream_packet_context_quarks[STREAM_PACKET_CONTEXT_QUARKS_LEN];
38
39 static
40 const char *plugin_options[] = {
41 "color",
42 "path",
43 "no-delta",
44 "clock-cycles",
45 "clock-seconds",
46 "clock-date",
47 "clock-gmt",
48 "verbose",
49 "name-default", /* show/hide */
50 "name-payload",
51 "name-context",
52 "name-scope",
53 "name-header",
54 "field-default", /* show/hide */
55 "field-trace",
56 "field-trace:hostname",
57 "field-trace:domain",
58 "field-trace:procname",
59 "field-trace:vpid",
60 "field-loglevel",
61 "field-emf",
62 "field-callsite",
63 };
64
65 static
66 const char * const in_port_name = "in";
67
68 static
69 void destroy_pretty_data(struct pretty_component *pretty)
70 {
71 bt_self_component_port_input_message_iterator_put_ref(pretty->iterator);
72
73 if (pretty->string) {
74 (void) g_string_free(pretty->string, TRUE);
75 }
76
77 if (pretty->tmp_string) {
78 (void) g_string_free(pretty->tmp_string, TRUE);
79 }
80
81 if (pretty->out != stdout) {
82 int ret;
83
84 ret = fclose(pretty->out);
85 if (ret) {
86 perror("close output file");
87 }
88 }
89 g_free(pretty->options.output_path);
90 g_free(pretty);
91 }
92
93 static
94 struct pretty_component *create_pretty(void)
95 {
96 struct pretty_component *pretty;
97
98 pretty = g_new0(struct pretty_component, 1);
99 if (!pretty) {
100 goto end;
101 }
102 pretty->string = g_string_new("");
103 if (!pretty->string) {
104 goto error;
105 }
106 pretty->tmp_string = g_string_new("");
107 if (!pretty->tmp_string) {
108 goto error;
109 }
110 end:
111 return pretty;
112
113 error:
114 g_free(pretty);
115 return NULL;
116 }
117
118 BT_HIDDEN
119 void pretty_finalize(bt_self_component_sink *comp)
120 {
121 destroy_pretty_data(
122 bt_self_component_get_data(
123 bt_self_component_sink_as_self_component(comp)));
124 }
125
126 static
127 bt_component_class_message_iterator_next_method_status handle_message(
128 struct pretty_component *pretty,
129 const bt_message *message)
130 {
131 bt_component_class_message_iterator_next_method_status ret =
132 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
133
134 BT_ASSERT(pretty);
135
136 switch (bt_message_get_type(message)) {
137 case BT_MESSAGE_TYPE_EVENT:
138 if (pretty_print_event(pretty, message)) {
139 ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
140 }
141 break;
142 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
143 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
144 if (pretty_print_discarded_items(pretty, message)) {
145 ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
146 }
147 break;
148 default:
149 break;
150 }
151
152 return ret;
153 }
154
155 BT_HIDDEN
156 bt_component_class_sink_graph_is_configured_method_status
157 pretty_graph_is_configured(bt_self_component_sink *comp)
158 {
159 bt_component_class_sink_graph_is_configured_method_status status =
160 BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
161 struct pretty_component *pretty;
162
163 pretty = bt_self_component_get_data(
164 bt_self_component_sink_as_self_component(comp));
165 BT_ASSERT(pretty);
166 BT_ASSERT(!pretty->iterator);
167 pretty->iterator = bt_self_component_port_input_message_iterator_create(
168 bt_self_component_sink_borrow_input_port_by_name(comp,
169 in_port_name));
170 if (!pretty->iterator) {
171 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR;
172 }
173
174 return status;
175 }
176
177 BT_HIDDEN
178 bt_component_class_sink_consume_method_status pretty_consume(
179 bt_self_component_sink *comp)
180 {
181 bt_component_class_sink_consume_method_status ret;
182 bt_message_array_const msgs;
183 bt_self_component_port_input_message_iterator *it;
184 struct pretty_component *pretty = bt_self_component_get_data(
185 bt_self_component_sink_as_self_component(comp));
186 bt_message_iterator_next_status next_status;
187 uint64_t count = 0;
188 uint64_t i = 0;
189
190 it = pretty->iterator;
191 next_status = bt_self_component_port_input_message_iterator_next(it,
192 &msgs, &count);
193
194 switch (next_status) {
195 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
196 break;
197 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
198 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
199 ret = (int) next_status;
200 goto end;
201 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
202 ret = (int) next_status;
203 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
204 pretty->iterator);
205 goto end;
206 default:
207 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
208 goto end;
209 }
210
211 BT_ASSERT(next_status == BT_MESSAGE_ITERATOR_NEXT_STATUS_OK);
212
213 for (i = 0; i < count; i++) {
214 ret = handle_message(pretty, msgs[i]);
215 if (ret) {
216 goto end;
217 }
218
219 bt_message_put_ref(msgs[i]);
220 }
221
222 end:
223 for (; i < count; i++) {
224 bt_message_put_ref(msgs[i]);
225 }
226
227 return ret;
228 }
229
230 static
231 int add_params_to_map(bt_value *plugin_opt_map)
232 {
233 int ret = 0;
234 unsigned int i;
235
236 for (i = 0; i < BT_ARRAY_SIZE(plugin_options); i++) {
237 const char *key = plugin_options[i];
238
239 if (bt_value_map_insert_entry(plugin_opt_map, key,
240 bt_value_null) < 0) {
241 ret = -1;
242 goto end;
243 }
244 }
245
246 end:
247 return ret;
248 }
249
250 static
251 bt_bool check_param_exists(const char *key, const bt_value *object,
252 void *data)
253 {
254 struct pretty_component *pretty = data;
255
256 if (!bt_value_map_has_entry(pretty->plugin_opt_map,
257 key)) {
258 fprintf(pretty->err,
259 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key);
260 }
261 return BT_TRUE;
262 }
263
264 static
265 void apply_one_string(const char *key, const bt_value *params, char **option)
266 {
267 const bt_value *value = NULL;
268 const char *str;
269
270 value = bt_value_map_borrow_entry_value_const(params, key);
271 if (!value) {
272 goto end;
273 }
274 if (bt_value_is_null(value)) {
275 goto end;
276 }
277 str = bt_value_string_get(value);
278 *option = g_strdup(str);
279
280 end:
281 return;
282 }
283
284 static
285 void apply_one_bool(const char *key, const bt_value *params, bool *option,
286 bool *found)
287 {
288 const bt_value *value = NULL;
289 bt_bool bool_val;
290
291 value = bt_value_map_borrow_entry_value_const(params, key);
292 if (!value) {
293 goto end;
294 }
295 bool_val = bt_value_bool_get(value);
296 *option = (bool) bool_val;
297 if (found) {
298 *found = true;
299 }
300
301 end:
302 return;
303 }
304
305 static
306 void warn_wrong_color_param(struct pretty_component *pretty)
307 {
308 fprintf(pretty->err,
309 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
310 }
311
312 static
313 int open_output_file(struct pretty_component *pretty)
314 {
315 int ret = 0;
316
317 if (!pretty->options.output_path) {
318 goto end;
319 }
320
321 pretty->out = fopen(pretty->options.output_path, "w");
322 if (!pretty->out) {
323 goto error;
324 }
325
326 goto end;
327
328 error:
329 ret = -1;
330
331 end:
332 return ret;
333 }
334
335 static
336 int apply_params(struct pretty_component *pretty, const bt_value *params)
337 {
338 int ret = 0;
339 bool value, found;
340 char *str = NULL;
341
342 pretty->plugin_opt_map = bt_value_map_create();
343 if (!pretty->plugin_opt_map) {
344 ret = -1;
345 goto end;
346 }
347 ret = add_params_to_map(pretty->plugin_opt_map);
348 if (ret) {
349 goto end;
350 }
351 /* Report unknown parameters. */
352 if (bt_value_map_foreach_entry_const(params,
353 check_param_exists, pretty) < 0) {
354 ret = -1;
355 goto end;
356 }
357
358 /* Known parameters. */
359 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
360 if (bt_value_map_has_entry(params, "color")) {
361 const bt_value *color_value;
362 const char *color;
363
364 color_value = bt_value_map_borrow_entry_value_const(params,
365 "color");
366 if (!color_value) {
367 goto end;
368 }
369
370 color = bt_value_string_get(color_value);
371
372 if (strcmp(color, "never") == 0) {
373 pretty->options.color = PRETTY_COLOR_OPT_NEVER;
374 } else if (strcmp(color, "auto") == 0) {
375 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
376 } else if (strcmp(color, "always") == 0) {
377 pretty->options.color = PRETTY_COLOR_OPT_ALWAYS;
378 } else {
379 warn_wrong_color_param(pretty);
380 }
381 }
382
383 apply_one_string("path", params, &pretty->options.output_path);
384 ret = open_output_file(pretty);
385 if (ret) {
386 goto end;
387 }
388
389 value = false; /* Default. */
390 apply_one_bool("no-delta", params, &value, NULL);
391 pretty->options.print_delta_field = !value; /* Reverse logic. */
392
393 value = false; /* Default. */
394 apply_one_bool("clock-cycles", params, &value, NULL);
395 pretty->options.print_timestamp_cycles = value;
396
397 value = false; /* Default. */
398 apply_one_bool("clock-seconds", params, &value, NULL);
399 pretty->options.clock_seconds = value;
400
401 value = false; /* Default. */
402 apply_one_bool("clock-date", params, &value, NULL);
403 pretty->options.clock_date = value;
404
405 value = false; /* Default. */
406 apply_one_bool("clock-gmt", params, &value, NULL);
407 pretty->options.clock_gmt = value;
408
409 value = false; /* Default. */
410 apply_one_bool("verbose", params, &value, NULL);
411 pretty->options.verbose = value;
412
413 /* Names. */
414 apply_one_string("name-default", params, &str);
415 if (!str) {
416 pretty->options.name_default = PRETTY_DEFAULT_UNSET;
417 } else if (!strcmp(str, "show")) {
418 pretty->options.name_default = PRETTY_DEFAULT_SHOW;
419 } else if (!strcmp(str, "hide")) {
420 pretty->options.name_default = PRETTY_DEFAULT_HIDE;
421 } else {
422 ret = -1;
423 goto end;
424 }
425 g_free(str);
426 str = NULL;
427
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;
434 break;
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;
440 break;
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;
446 break;
447 default:
448 ret = -1;
449 goto end;
450 }
451
452 value = false;
453 found = false;
454 apply_one_bool("name-payload", params, &value, &found);
455 if (found) {
456 pretty->options.print_payload_field_names = value;
457 }
458
459 value = false;
460 found = false;
461 apply_one_bool("name-context", params, &value, &found);
462 if (found) {
463 pretty->options.print_context_field_names = value;
464 }
465
466 value = false;
467 found = false;
468 apply_one_bool("name-header", params, &value, &found);
469 if (found) {
470 pretty->options.print_header_field_names = value;
471 }
472
473 value = false;
474 found = false;
475 apply_one_bool("name-scope", params, &value, &found);
476 if (found) {
477 pretty->options.print_scope_field_names = value;
478 }
479
480 /* Fields. */
481 apply_one_string("field-default", params, &str);
482 if (!str) {
483 pretty->options.field_default = PRETTY_DEFAULT_UNSET;
484 } else if (!strcmp(str, "show")) {
485 pretty->options.field_default = PRETTY_DEFAULT_SHOW;
486 } else if (!strcmp(str, "hide")) {
487 pretty->options.field_default = PRETTY_DEFAULT_HIDE;
488 } else {
489 ret = -1;
490 goto end;
491 }
492 g_free(str);
493 str = NULL;
494
495 switch (pretty->options.field_default) {
496 case PRETTY_DEFAULT_UNSET:
497 pretty->options.print_trace_field = false;
498 pretty->options.print_trace_hostname_field = true;
499 pretty->options.print_trace_domain_field = false;
500 pretty->options.print_trace_procname_field = true;
501 pretty->options.print_trace_vpid_field = true;
502 pretty->options.print_loglevel_field = false;
503 pretty->options.print_emf_field = false;
504 pretty->options.print_callsite_field = false;
505 break;
506 case PRETTY_DEFAULT_SHOW:
507 pretty->options.print_trace_field = true;
508 pretty->options.print_trace_hostname_field = true;
509 pretty->options.print_trace_domain_field = true;
510 pretty->options.print_trace_procname_field = true;
511 pretty->options.print_trace_vpid_field = true;
512 pretty->options.print_loglevel_field = true;
513 pretty->options.print_emf_field = true;
514 pretty->options.print_callsite_field = true;
515 break;
516 case PRETTY_DEFAULT_HIDE:
517 pretty->options.print_trace_field = false;
518 pretty->options.print_trace_hostname_field = false;
519 pretty->options.print_trace_domain_field = false;
520 pretty->options.print_trace_procname_field = false;
521 pretty->options.print_trace_vpid_field = false;
522 pretty->options.print_loglevel_field = false;
523 pretty->options.print_emf_field = false;
524 pretty->options.print_callsite_field = false;
525 break;
526 default:
527 ret = -1;
528 goto end;
529 }
530
531 value = false;
532 found = false;
533 apply_one_bool("field-trace", params, &value, &found);
534 if (found) {
535 pretty->options.print_trace_field = value;
536 }
537
538 value = false;
539 found = false;
540 apply_one_bool("field-trace:hostname", params, &value, &found);
541 if (found) {
542 pretty->options.print_trace_hostname_field = value;
543 }
544
545 value = false;
546 found = false;
547 apply_one_bool("field-trace:domain", params, &value, &found);
548 if (found) {
549 pretty->options.print_trace_domain_field = value;
550 }
551
552 value = false;
553 found = false;
554 apply_one_bool("field-trace:procname", params, &value, &found);
555 if (found) {
556 pretty->options.print_trace_procname_field = value;
557 }
558
559 value = false;
560 found = false;
561 apply_one_bool("field-trace:vpid", params, &value, &found);
562 if (found) {
563 pretty->options.print_trace_vpid_field = value;
564 }
565
566 value = false;
567 found = false;
568 apply_one_bool("field-loglevel", params, &value, &found);
569 if (found) {
570 pretty->options.print_loglevel_field = value;
571 }
572
573 value = false;
574 found = false;
575 apply_one_bool("field-emf", params, &value, &found);
576 if (found) {
577 pretty->options.print_emf_field = value;
578 }
579
580 value = false;
581 found = false;
582 apply_one_bool("field-callsite", params, &value, &found);
583 if (found) {
584 pretty->options.print_callsite_field = value;
585 }
586
587 end:
588 bt_value_put_ref(pretty->plugin_opt_map);
589 pretty->plugin_opt_map = NULL;
590 g_free(str);
591 return ret;
592 }
593
594 static
595 void set_use_colors(struct pretty_component *pretty)
596 {
597 switch (pretty->options.color) {
598 case PRETTY_COLOR_OPT_ALWAYS:
599 pretty->use_colors = true;
600 break;
601 case PRETTY_COLOR_OPT_AUTO:
602 pretty->use_colors = pretty->out == stdout &&
603 bt_common_colors_supported();
604 break;
605 case PRETTY_COLOR_OPT_NEVER:
606 pretty->use_colors = false;
607 break;
608 }
609 }
610
611 static
612 void init_stream_packet_context_quarks(void)
613 {
614 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
615 g_quark_from_string("timestamp_begin");
616 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
617 g_quark_from_string("timestamp_begin");
618 stream_packet_context_quarks[Q_TIMESTAMP_END] =
619 g_quark_from_string("timestamp_end");
620 stream_packet_context_quarks[Q_EVENTS_DISCARDED] =
621 g_quark_from_string("events_discarded");
622 stream_packet_context_quarks[Q_CONTENT_SIZE] =
623 g_quark_from_string("content_size");
624 stream_packet_context_quarks[Q_PACKET_SIZE] =
625 g_quark_from_string("packet_size");
626 stream_packet_context_quarks[Q_PACKET_SEQ_NUM] =
627 g_quark_from_string("packet_seq_num");
628 }
629
630 BT_HIDDEN
631 bt_component_class_init_method_status pretty_init(
632 bt_self_component_sink *comp, const bt_value *params,
633 __attribute__((unused)) void *init_method_data)
634 {
635 bt_component_class_init_method_status ret =
636 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
637 struct pretty_component *pretty = create_pretty();
638
639 if (!pretty) {
640 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
641 goto end;
642 }
643
644 if (bt_self_component_sink_add_input_port(comp,
645 in_port_name, NULL, NULL) < 0) {
646 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
647 goto error;
648 }
649
650 pretty->out = stdout;
651 pretty->err = stderr;
652
653 pretty->delta_cycles = -1ULL;
654 pretty->last_cycles_timestamp = -1ULL;
655
656 pretty->delta_real_timestamp = -1ULL;
657 pretty->last_real_timestamp = -1ULL;
658
659 if (apply_params(pretty, params)) {
660 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
661 goto error;
662 }
663
664 set_use_colors(pretty);
665 bt_self_component_set_data(
666 bt_self_component_sink_as_self_component(comp), pretty);
667 init_stream_packet_context_quarks();
668
669 end:
670 return ret;
671
672 error:
673 destroy_pretty_data(pretty);
674 return ret;
675 }
This page took 0.042646 seconds and 4 git commands to generate.