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