lib: pass config objects to component init methods
[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_self_component_port_input_message_iterator_create_from_sink_component_status
159 msg_iter_status;
160 struct pretty_component *pretty;
161
162 pretty = bt_self_component_get_data(
163 bt_self_component_sink_as_self_component(comp));
164 BT_ASSERT(pretty);
165 BT_ASSERT(!pretty->iterator);
166 msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component(
167 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
168 in_port_name), &pretty->iterator);
169 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
170 status = (int) msg_iter_status;
171 goto end;
172 }
173
174 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
175
176 end:
177 return status;
178 }
179
180 BT_HIDDEN
181 bt_component_class_sink_consume_method_status pretty_consume(
182 bt_self_component_sink *comp)
183 {
184 bt_component_class_sink_consume_method_status ret =
185 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
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_next_status next_status;
191 uint64_t count = 0;
192 uint64_t i = 0;
193
194 it = pretty->iterator;
195 next_status = bt_self_component_port_input_message_iterator_next(it,
196 &msgs, &count);
197
198 switch (next_status) {
199 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
200 break;
201 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
202 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
203 ret = (int) next_status;
204 goto end;
205 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
206 ret = (int) next_status;
207 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
208 pretty->iterator);
209 goto end;
210 default:
211 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
212 goto end;
213 }
214
215 BT_ASSERT(next_status == BT_MESSAGE_ITERATOR_NEXT_STATUS_OK);
216
217 for (i = 0; i < count; i++) {
218 ret = (int) handle_message(pretty, msgs[i]);
219 if (ret) {
220 goto end;
221 }
222
223 bt_message_put_ref(msgs[i]);
224 }
225
226 end:
227 for (; i < count; i++) {
228 bt_message_put_ref(msgs[i]);
229 }
230
231 return ret;
232 }
233
234 static
235 int add_params_to_map(bt_value *plugin_opt_map)
236 {
237 int ret = 0;
238 unsigned int i;
239
240 for (i = 0; i < BT_ARRAY_SIZE(plugin_options); i++) {
241 const char *key = plugin_options[i];
242
243 if (bt_value_map_insert_entry(plugin_opt_map, key,
244 bt_value_null) < 0) {
245 ret = -1;
246 goto end;
247 }
248 }
249
250 end:
251 return ret;
252 }
253
254 static
255 bt_bool check_param_exists(const char *key, const bt_value *object,
256 void *data)
257 {
258 struct pretty_component *pretty = data;
259
260 if (!bt_value_map_has_entry(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, const bt_value *params, char **option)
270 {
271 const bt_value *value = NULL;
272 const char *str;
273
274 value = bt_value_map_borrow_entry_value_const(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, const bt_value *params, bool *option,
290 bool *found)
291 {
292 const bt_value *value = NULL;
293 bt_bool bool_val;
294
295 value = bt_value_map_borrow_entry_value_const(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, const bt_value *params)
341 {
342 int ret = 0;
343 bool value, found;
344 char *str = NULL;
345
346 pretty->plugin_opt_map = bt_value_map_create();
347 if (!pretty->plugin_opt_map) {
348 ret = -1;
349 goto end;
350 }
351 ret = add_params_to_map(pretty->plugin_opt_map);
352 if (ret) {
353 goto end;
354 }
355 /* Report unknown parameters. */
356 if (bt_value_map_foreach_entry_const(params,
357 check_param_exists, pretty) < 0) {
358 ret = -1;
359 goto end;
360 }
361
362 /* Known parameters. */
363 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
364 if (bt_value_map_has_entry(params, "color")) {
365 const bt_value *color_value;
366 const char *color;
367
368 color_value = bt_value_map_borrow_entry_value_const(params,
369 "color");
370 if (!color_value) {
371 goto end;
372 }
373
374 color = bt_value_string_get(color_value);
375
376 if (strcmp(color, "never") == 0) {
377 pretty->options.color = PRETTY_COLOR_OPT_NEVER;
378 } else if (strcmp(color, "auto") == 0) {
379 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
380 } else if (strcmp(color, "always") == 0) {
381 pretty->options.color = PRETTY_COLOR_OPT_ALWAYS;
382 } else {
383 warn_wrong_color_param(pretty);
384 }
385 }
386
387 apply_one_string("path", params, &pretty->options.output_path);
388 ret = open_output_file(pretty);
389 if (ret) {
390 goto end;
391 }
392
393 value = false; /* Default. */
394 apply_one_bool("no-delta", params, &value, NULL);
395 pretty->options.print_delta_field = !value; /* Reverse logic. */
396
397 value = false; /* Default. */
398 apply_one_bool("clock-cycles", params, &value, NULL);
399 pretty->options.print_timestamp_cycles = value;
400
401 value = false; /* Default. */
402 apply_one_bool("clock-seconds", params, &value, NULL);
403 pretty->options.clock_seconds = value;
404
405 value = false; /* Default. */
406 apply_one_bool("clock-date", params, &value, NULL);
407 pretty->options.clock_date = value;
408
409 value = false; /* Default. */
410 apply_one_bool("clock-gmt", params, &value, NULL);
411 pretty->options.clock_gmt = value;
412
413 value = false; /* Default. */
414 apply_one_bool("verbose", params, &value, NULL);
415 pretty->options.verbose = value;
416
417 /* Names. */
418 apply_one_string("name-default", params, &str);
419 if (!str) {
420 pretty->options.name_default = PRETTY_DEFAULT_UNSET;
421 } else if (strcmp(str, "show") == 0) {
422 pretty->options.name_default = PRETTY_DEFAULT_SHOW;
423 } else if (strcmp(str, "hide") == 0) {
424 pretty->options.name_default = PRETTY_DEFAULT_HIDE;
425 } else {
426 ret = -1;
427 goto end;
428 }
429 g_free(str);
430 str = NULL;
431
432 switch (pretty->options.name_default) {
433 case PRETTY_DEFAULT_UNSET:
434 pretty->options.print_payload_field_names = true;
435 pretty->options.print_context_field_names = true;
436 pretty->options.print_header_field_names = false;
437 pretty->options.print_scope_field_names = false;
438 break;
439 case PRETTY_DEFAULT_SHOW:
440 pretty->options.print_payload_field_names = true;
441 pretty->options.print_context_field_names = true;
442 pretty->options.print_header_field_names = true;
443 pretty->options.print_scope_field_names = true;
444 break;
445 case PRETTY_DEFAULT_HIDE:
446 pretty->options.print_payload_field_names = false;
447 pretty->options.print_context_field_names = false;
448 pretty->options.print_header_field_names = false;
449 pretty->options.print_scope_field_names = false;
450 break;
451 default:
452 ret = -1;
453 goto end;
454 }
455
456 value = false;
457 found = false;
458 apply_one_bool("name-payload", params, &value, &found);
459 if (found) {
460 pretty->options.print_payload_field_names = value;
461 }
462
463 value = false;
464 found = false;
465 apply_one_bool("name-context", params, &value, &found);
466 if (found) {
467 pretty->options.print_context_field_names = value;
468 }
469
470 value = false;
471 found = false;
472 apply_one_bool("name-header", params, &value, &found);
473 if (found) {
474 pretty->options.print_header_field_names = value;
475 }
476
477 value = false;
478 found = false;
479 apply_one_bool("name-scope", params, &value, &found);
480 if (found) {
481 pretty->options.print_scope_field_names = value;
482 }
483
484 /* Fields. */
485 apply_one_string("field-default", params, &str);
486 if (!str) {
487 pretty->options.field_default = PRETTY_DEFAULT_UNSET;
488 } else if (strcmp(str, "show") == 0) {
489 pretty->options.field_default = PRETTY_DEFAULT_SHOW;
490 } else if (strcmp(str, "hide") == 0) {
491 pretty->options.field_default = PRETTY_DEFAULT_HIDE;
492 } else {
493 ret = -1;
494 goto end;
495 }
496 g_free(str);
497 str = NULL;
498
499 switch (pretty->options.field_default) {
500 case PRETTY_DEFAULT_UNSET:
501 pretty->options.print_trace_field = false;
502 pretty->options.print_trace_hostname_field = true;
503 pretty->options.print_trace_domain_field = false;
504 pretty->options.print_trace_procname_field = true;
505 pretty->options.print_trace_vpid_field = true;
506 pretty->options.print_loglevel_field = false;
507 pretty->options.print_emf_field = false;
508 pretty->options.print_callsite_field = false;
509 break;
510 case PRETTY_DEFAULT_SHOW:
511 pretty->options.print_trace_field = true;
512 pretty->options.print_trace_hostname_field = true;
513 pretty->options.print_trace_domain_field = true;
514 pretty->options.print_trace_procname_field = true;
515 pretty->options.print_trace_vpid_field = true;
516 pretty->options.print_loglevel_field = true;
517 pretty->options.print_emf_field = true;
518 pretty->options.print_callsite_field = true;
519 break;
520 case PRETTY_DEFAULT_HIDE:
521 pretty->options.print_trace_field = false;
522 pretty->options.print_trace_hostname_field = false;
523 pretty->options.print_trace_domain_field = false;
524 pretty->options.print_trace_procname_field = false;
525 pretty->options.print_trace_vpid_field = false;
526 pretty->options.print_loglevel_field = false;
527 pretty->options.print_emf_field = false;
528 pretty->options.print_callsite_field = false;
529 break;
530 default:
531 ret = -1;
532 goto end;
533 }
534
535 value = false;
536 found = false;
537 apply_one_bool("field-trace", params, &value, &found);
538 if (found) {
539 pretty->options.print_trace_field = value;
540 }
541
542 value = false;
543 found = false;
544 apply_one_bool("field-trace:hostname", params, &value, &found);
545 if (found) {
546 pretty->options.print_trace_hostname_field = value;
547 }
548
549 value = false;
550 found = false;
551 apply_one_bool("field-trace:domain", params, &value, &found);
552 if (found) {
553 pretty->options.print_trace_domain_field = value;
554 }
555
556 value = false;
557 found = false;
558 apply_one_bool("field-trace:procname", params, &value, &found);
559 if (found) {
560 pretty->options.print_trace_procname_field = value;
561 }
562
563 value = false;
564 found = false;
565 apply_one_bool("field-trace:vpid", params, &value, &found);
566 if (found) {
567 pretty->options.print_trace_vpid_field = value;
568 }
569
570 value = false;
571 found = false;
572 apply_one_bool("field-loglevel", params, &value, &found);
573 if (found) {
574 pretty->options.print_loglevel_field = value;
575 }
576
577 value = false;
578 found = false;
579 apply_one_bool("field-emf", params, &value, &found);
580 if (found) {
581 pretty->options.print_emf_field = value;
582 }
583
584 value = false;
585 found = false;
586 apply_one_bool("field-callsite", params, &value, &found);
587 if (found) {
588 pretty->options.print_callsite_field = value;
589 }
590
591 end:
592 bt_value_put_ref(pretty->plugin_opt_map);
593 pretty->plugin_opt_map = NULL;
594 g_free(str);
595 return ret;
596 }
597
598 static
599 void set_use_colors(struct pretty_component *pretty)
600 {
601 switch (pretty->options.color) {
602 case PRETTY_COLOR_OPT_ALWAYS:
603 pretty->use_colors = true;
604 break;
605 case PRETTY_COLOR_OPT_AUTO:
606 pretty->use_colors = pretty->out == stdout &&
607 bt_common_colors_supported();
608 break;
609 case PRETTY_COLOR_OPT_NEVER:
610 pretty->use_colors = false;
611 break;
612 }
613 }
614
615 BT_HIDDEN
616 bt_component_class_init_method_status pretty_init(
617 bt_self_component_sink *comp,
618 bt_self_component_sink_configuration *config,
619 const bt_value *params,
620 __attribute__((unused)) void *init_method_data)
621 {
622 bt_component_class_init_method_status ret =
623 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
624 struct pretty_component *pretty = create_pretty();
625
626 if (!pretty) {
627 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
628 goto end;
629 }
630
631 if (bt_self_component_sink_add_input_port(comp,
632 in_port_name, NULL, NULL) < 0) {
633 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
634 goto error;
635 }
636
637 pretty->out = stdout;
638 pretty->err = stderr;
639
640 pretty->delta_cycles = -1ULL;
641 pretty->last_cycles_timestamp = -1ULL;
642
643 pretty->delta_real_timestamp = -1ULL;
644 pretty->last_real_timestamp = -1ULL;
645
646 if (apply_params(pretty, params)) {
647 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
648 goto error;
649 }
650
651 set_use_colors(pretty);
652 bt_self_component_set_data(
653 bt_self_component_sink_as_self_component(comp), pretty);
654
655 end:
656 return ret;
657
658 error:
659 destroy_pretty_data(pretty);
660 return ret;
661 }
This page took 0.044847 seconds and 4 git commands to generate.