48c7a9a002d6fc4066d2c668c4dfc69f618bcc48
[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_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_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_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
183 bt_message_array_const msgs;
184 bt_self_component_port_input_message_iterator *it;
185 struct pretty_component *pretty = bt_self_component_get_data(
186 bt_self_component_sink_as_self_component(comp));
187 bt_message_iterator_next_status next_status;
188 uint64_t count = 0;
189 uint64_t i = 0;
190
191 it = pretty->iterator;
192 next_status = bt_self_component_port_input_message_iterator_next(it,
193 &msgs, &count);
194
195 switch (next_status) {
196 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
197 break;
198 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
199 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
200 ret = (int) next_status;
201 goto end;
202 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
203 ret = (int) next_status;
204 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
205 pretty->iterator);
206 goto end;
207 default:
208 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
209 goto end;
210 }
211
212 BT_ASSERT(next_status == BT_MESSAGE_ITERATOR_NEXT_STATUS_OK);
213
214 for (i = 0; i < count; i++) {
215 ret = (int) handle_message(pretty, msgs[i]);
216 if (ret) {
217 goto end;
218 }
219
220 bt_message_put_ref(msgs[i]);
221 }
222
223 end:
224 for (; i < count; i++) {
225 bt_message_put_ref(msgs[i]);
226 }
227
228 return ret;
229 }
230
231 static
232 int add_params_to_map(bt_value *plugin_opt_map)
233 {
234 int ret = 0;
235 unsigned int i;
236
237 for (i = 0; i < BT_ARRAY_SIZE(plugin_options); i++) {
238 const char *key = plugin_options[i];
239
240 if (bt_value_map_insert_entry(plugin_opt_map, key,
241 bt_value_null) < 0) {
242 ret = -1;
243 goto end;
244 }
245 }
246
247 end:
248 return ret;
249 }
250
251 static
252 bt_bool check_param_exists(const char *key, const bt_value *object,
253 void *data)
254 {
255 struct pretty_component *pretty = data;
256
257 if (!bt_value_map_has_entry(pretty->plugin_opt_map,
258 key)) {
259 fprintf(pretty->err,
260 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key);
261 }
262 return BT_TRUE;
263 }
264
265 static
266 void apply_one_string(const char *key, const bt_value *params, char **option)
267 {
268 const bt_value *value = NULL;
269 const char *str;
270
271 value = bt_value_map_borrow_entry_value_const(params, key);
272 if (!value) {
273 goto end;
274 }
275 if (bt_value_is_null(value)) {
276 goto end;
277 }
278 str = bt_value_string_get(value);
279 *option = g_strdup(str);
280
281 end:
282 return;
283 }
284
285 static
286 void apply_one_bool(const char *key, const bt_value *params, bool *option,
287 bool *found)
288 {
289 const bt_value *value = NULL;
290 bt_bool bool_val;
291
292 value = bt_value_map_borrow_entry_value_const(params, key);
293 if (!value) {
294 goto end;
295 }
296 bool_val = bt_value_bool_get(value);
297 *option = (bool) bool_val;
298 if (found) {
299 *found = true;
300 }
301
302 end:
303 return;
304 }
305
306 static
307 void warn_wrong_color_param(struct pretty_component *pretty)
308 {
309 fprintf(pretty->err,
310 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
311 }
312
313 static
314 int open_output_file(struct pretty_component *pretty)
315 {
316 int ret = 0;
317
318 if (!pretty->options.output_path) {
319 goto end;
320 }
321
322 pretty->out = fopen(pretty->options.output_path, "w");
323 if (!pretty->out) {
324 goto error;
325 }
326
327 goto end;
328
329 error:
330 ret = -1;
331
332 end:
333 return ret;
334 }
335
336 static
337 int apply_params(struct pretty_component *pretty, const bt_value *params)
338 {
339 int ret = 0;
340 bool value, found;
341 char *str = NULL;
342
343 pretty->plugin_opt_map = bt_value_map_create();
344 if (!pretty->plugin_opt_map) {
345 ret = -1;
346 goto end;
347 }
348 ret = add_params_to_map(pretty->plugin_opt_map);
349 if (ret) {
350 goto end;
351 }
352 /* Report unknown parameters. */
353 if (bt_value_map_foreach_entry_const(params,
354 check_param_exists, pretty) < 0) {
355 ret = -1;
356 goto end;
357 }
358
359 /* Known parameters. */
360 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
361 if (bt_value_map_has_entry(params, "color")) {
362 const bt_value *color_value;
363 const char *color;
364
365 color_value = bt_value_map_borrow_entry_value_const(params,
366 "color");
367 if (!color_value) {
368 goto end;
369 }
370
371 color = bt_value_string_get(color_value);
372
373 if (strcmp(color, "never") == 0) {
374 pretty->options.color = PRETTY_COLOR_OPT_NEVER;
375 } else if (strcmp(color, "auto") == 0) {
376 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
377 } else if (strcmp(color, "always") == 0) {
378 pretty->options.color = PRETTY_COLOR_OPT_ALWAYS;
379 } else {
380 warn_wrong_color_param(pretty);
381 }
382 }
383
384 apply_one_string("path", params, &pretty->options.output_path);
385 ret = open_output_file(pretty);
386 if (ret) {
387 goto end;
388 }
389
390 value = false; /* Default. */
391 apply_one_bool("no-delta", params, &value, NULL);
392 pretty->options.print_delta_field = !value; /* Reverse logic. */
393
394 value = false; /* Default. */
395 apply_one_bool("clock-cycles", params, &value, NULL);
396 pretty->options.print_timestamp_cycles = value;
397
398 value = false; /* Default. */
399 apply_one_bool("clock-seconds", params, &value, NULL);
400 pretty->options.clock_seconds = value;
401
402 value = false; /* Default. */
403 apply_one_bool("clock-date", params, &value, NULL);
404 pretty->options.clock_date = value;
405
406 value = false; /* Default. */
407 apply_one_bool("clock-gmt", params, &value, NULL);
408 pretty->options.clock_gmt = value;
409
410 value = false; /* Default. */
411 apply_one_bool("verbose", params, &value, NULL);
412 pretty->options.verbose = value;
413
414 /* Names. */
415 apply_one_string("name-default", params, &str);
416 if (!str) {
417 pretty->options.name_default = PRETTY_DEFAULT_UNSET;
418 } else if (strcmp(str, "show") == 0) {
419 pretty->options.name_default = PRETTY_DEFAULT_SHOW;
420 } else if (strcmp(str, "hide") == 0) {
421 pretty->options.name_default = PRETTY_DEFAULT_HIDE;
422 } else {
423 ret = -1;
424 goto end;
425 }
426 g_free(str);
427 str = NULL;
428
429 switch (pretty->options.name_default) {
430 case PRETTY_DEFAULT_UNSET:
431 pretty->options.print_payload_field_names = true;
432 pretty->options.print_context_field_names = true;
433 pretty->options.print_header_field_names = false;
434 pretty->options.print_scope_field_names = false;
435 break;
436 case PRETTY_DEFAULT_SHOW:
437 pretty->options.print_payload_field_names = true;
438 pretty->options.print_context_field_names = true;
439 pretty->options.print_header_field_names = true;
440 pretty->options.print_scope_field_names = true;
441 break;
442 case PRETTY_DEFAULT_HIDE:
443 pretty->options.print_payload_field_names = false;
444 pretty->options.print_context_field_names = false;
445 pretty->options.print_header_field_names = false;
446 pretty->options.print_scope_field_names = false;
447 break;
448 default:
449 ret = -1;
450 goto end;
451 }
452
453 value = false;
454 found = false;
455 apply_one_bool("name-payload", params, &value, &found);
456 if (found) {
457 pretty->options.print_payload_field_names = value;
458 }
459
460 value = false;
461 found = false;
462 apply_one_bool("name-context", params, &value, &found);
463 if (found) {
464 pretty->options.print_context_field_names = value;
465 }
466
467 value = false;
468 found = false;
469 apply_one_bool("name-header", params, &value, &found);
470 if (found) {
471 pretty->options.print_header_field_names = value;
472 }
473
474 value = false;
475 found = false;
476 apply_one_bool("name-scope", params, &value, &found);
477 if (found) {
478 pretty->options.print_scope_field_names = value;
479 }
480
481 /* Fields. */
482 apply_one_string("field-default", params, &str);
483 if (!str) {
484 pretty->options.field_default = PRETTY_DEFAULT_UNSET;
485 } else if (strcmp(str, "show") == 0) {
486 pretty->options.field_default = PRETTY_DEFAULT_SHOW;
487 } else if (strcmp(str, "hide") == 0) {
488 pretty->options.field_default = PRETTY_DEFAULT_HIDE;
489 } else {
490 ret = -1;
491 goto end;
492 }
493 g_free(str);
494 str = NULL;
495
496 switch (pretty->options.field_default) {
497 case PRETTY_DEFAULT_UNSET:
498 pretty->options.print_trace_field = false;
499 pretty->options.print_trace_hostname_field = true;
500 pretty->options.print_trace_domain_field = false;
501 pretty->options.print_trace_procname_field = true;
502 pretty->options.print_trace_vpid_field = true;
503 pretty->options.print_loglevel_field = false;
504 pretty->options.print_emf_field = false;
505 pretty->options.print_callsite_field = false;
506 break;
507 case PRETTY_DEFAULT_SHOW:
508 pretty->options.print_trace_field = true;
509 pretty->options.print_trace_hostname_field = true;
510 pretty->options.print_trace_domain_field = true;
511 pretty->options.print_trace_procname_field = true;
512 pretty->options.print_trace_vpid_field = true;
513 pretty->options.print_loglevel_field = true;
514 pretty->options.print_emf_field = true;
515 pretty->options.print_callsite_field = true;
516 break;
517 case PRETTY_DEFAULT_HIDE:
518 pretty->options.print_trace_field = false;
519 pretty->options.print_trace_hostname_field = false;
520 pretty->options.print_trace_domain_field = false;
521 pretty->options.print_trace_procname_field = false;
522 pretty->options.print_trace_vpid_field = false;
523 pretty->options.print_loglevel_field = false;
524 pretty->options.print_emf_field = false;
525 pretty->options.print_callsite_field = false;
526 break;
527 default:
528 ret = -1;
529 goto end;
530 }
531
532 value = false;
533 found = false;
534 apply_one_bool("field-trace", params, &value, &found);
535 if (found) {
536 pretty->options.print_trace_field = value;
537 }
538
539 value = false;
540 found = false;
541 apply_one_bool("field-trace:hostname", params, &value, &found);
542 if (found) {
543 pretty->options.print_trace_hostname_field = value;
544 }
545
546 value = false;
547 found = false;
548 apply_one_bool("field-trace:domain", params, &value, &found);
549 if (found) {
550 pretty->options.print_trace_domain_field = value;
551 }
552
553 value = false;
554 found = false;
555 apply_one_bool("field-trace:procname", params, &value, &found);
556 if (found) {
557 pretty->options.print_trace_procname_field = value;
558 }
559
560 value = false;
561 found = false;
562 apply_one_bool("field-trace:vpid", params, &value, &found);
563 if (found) {
564 pretty->options.print_trace_vpid_field = value;
565 }
566
567 value = false;
568 found = false;
569 apply_one_bool("field-loglevel", params, &value, &found);
570 if (found) {
571 pretty->options.print_loglevel_field = value;
572 }
573
574 value = false;
575 found = false;
576 apply_one_bool("field-emf", params, &value, &found);
577 if (found) {
578 pretty->options.print_emf_field = value;
579 }
580
581 value = false;
582 found = false;
583 apply_one_bool("field-callsite", params, &value, &found);
584 if (found) {
585 pretty->options.print_callsite_field = value;
586 }
587
588 end:
589 bt_value_put_ref(pretty->plugin_opt_map);
590 pretty->plugin_opt_map = NULL;
591 g_free(str);
592 return ret;
593 }
594
595 static
596 void set_use_colors(struct pretty_component *pretty)
597 {
598 switch (pretty->options.color) {
599 case PRETTY_COLOR_OPT_ALWAYS:
600 pretty->use_colors = true;
601 break;
602 case PRETTY_COLOR_OPT_AUTO:
603 pretty->use_colors = pretty->out == stdout &&
604 bt_common_colors_supported();
605 break;
606 case PRETTY_COLOR_OPT_NEVER:
607 pretty->use_colors = false;
608 break;
609 }
610 }
611
612 static
613 void init_stream_packet_context_quarks(void)
614 {
615 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
616 g_quark_from_string("timestamp_begin");
617 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
618 g_quark_from_string("timestamp_begin");
619 stream_packet_context_quarks[Q_TIMESTAMP_END] =
620 g_quark_from_string("timestamp_end");
621 stream_packet_context_quarks[Q_EVENTS_DISCARDED] =
622 g_quark_from_string("events_discarded");
623 stream_packet_context_quarks[Q_CONTENT_SIZE] =
624 g_quark_from_string("content_size");
625 stream_packet_context_quarks[Q_PACKET_SIZE] =
626 g_quark_from_string("packet_size");
627 stream_packet_context_quarks[Q_PACKET_SEQ_NUM] =
628 g_quark_from_string("packet_seq_num");
629 }
630
631 BT_HIDDEN
632 bt_component_class_init_method_status pretty_init(
633 bt_self_component_sink *comp, const bt_value *params,
634 __attribute__((unused)) void *init_method_data)
635 {
636 bt_component_class_init_method_status ret =
637 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
638 struct pretty_component *pretty = create_pretty();
639
640 if (!pretty) {
641 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
642 goto end;
643 }
644
645 if (bt_self_component_sink_add_input_port(comp,
646 in_port_name, NULL, NULL) < 0) {
647 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
648 goto error;
649 }
650
651 pretty->out = stdout;
652 pretty->err = stderr;
653
654 pretty->delta_cycles = -1ULL;
655 pretty->last_cycles_timestamp = -1ULL;
656
657 pretty->delta_real_timestamp = -1ULL;
658 pretty->last_real_timestamp = -1ULL;
659
660 if (apply_params(pretty, params)) {
661 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
662 goto error;
663 }
664
665 set_use_colors(pretty);
666 bt_self_component_set_data(
667 bt_self_component_sink_as_self_component(comp), pretty);
668 init_stream_packet_context_quarks();
669
670 end:
671 return ret;
672
673 error:
674 destroy_pretty_data(pretty);
675 return ret;
676 }
This page took 0.041492 seconds and 3 git commands to generate.