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