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