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