Commit | Line | Data |
---|---|---|
7a278c8e | 1 | /* |
3228cc1d | 2 | * pretty.c |
7a278c8e | 3 | * |
5dac767a | 4 | * Babeltrace CTF Text Output Plugin |
7a278c8e | 5 | * |
2e339de1 | 6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
f504043c | 7 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7a278c8e JG |
8 | * |
9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | * | |
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
27 | * SOFTWARE. | |
28 | */ | |
29 | ||
9d408fca | 30 | #include <babeltrace/babeltrace.h> |
6e1bc0df | 31 | #include <babeltrace/values.h> |
3d9990ac | 32 | #include <babeltrace/compiler-internal.h> |
ad96d936 | 33 | #include <babeltrace/common-internal.h> |
7d61fa8e | 34 | #include <plugins-common.h> |
bfd20a42 | 35 | #include <stdio.h> |
39cfa40f | 36 | #include <stdbool.h> |
bac67f0f | 37 | #include <glib.h> |
f6ccaed9 | 38 | #include <babeltrace/assert-internal.h> |
6405967d | 39 | |
3228cc1d PP |
40 | #include "pretty.h" |
41 | ||
61d6f9b1 MJ |
42 | GQuark stream_packet_context_quarks[STREAM_PACKET_CONTEXT_QUARKS_LEN]; |
43 | ||
6e1bc0df MD |
44 | static |
45 | const char *plugin_options[] = { | |
ad96d936 | 46 | "color", |
a0ad501b | 47 | "path", |
6e1bc0df MD |
48 | "no-delta", |
49 | "clock-cycles", | |
50 | "clock-seconds", | |
51 | "clock-date", | |
52 | "clock-gmt", | |
a263021c | 53 | "verbose", |
6e1bc0df MD |
54 | "name-default", /* show/hide */ |
55 | "name-payload", | |
56 | "name-context", | |
57 | "name-scope", | |
58 | "name-header", | |
59 | "field-default", /* show/hide */ | |
60 | "field-trace", | |
61 | "field-trace:hostname", | |
62 | "field-trace:domain", | |
63 | "field-trace:procname", | |
64 | "field-trace:vpid", | |
65 | "field-loglevel", | |
66 | "field-emf", | |
60535549 | 67 | "field-callsite", |
6e1bc0df MD |
68 | }; |
69 | ||
bfd20a42 | 70 | static |
3228cc1d | 71 | void destroy_pretty_data(struct pretty_component *pretty) |
bac67f0f | 72 | { |
3228cc1d | 73 | bt_put(pretty->input_iterator); |
5280f742 PP |
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 | ||
3228cc1d | 83 | if (pretty->out != stdout) { |
77986bad JD |
84 | int ret; |
85 | ||
3228cc1d | 86 | ret = fclose(pretty->out); |
77986bad JD |
87 | if (ret) { |
88 | perror("close output file"); | |
89 | } | |
90 | } | |
3228cc1d | 91 | g_free(pretty->options.output_path); |
3228cc1d | 92 | g_free(pretty); |
bac67f0f JG |
93 | } |
94 | ||
b25bd455 | 95 | static |
3228cc1d | 96 | struct pretty_component *create_pretty(void) |
bac67f0f | 97 | { |
3228cc1d | 98 | struct pretty_component *pretty; |
541b0a11 | 99 | |
3228cc1d PP |
100 | pretty = g_new0(struct pretty_component, 1); |
101 | if (!pretty) { | |
541b0a11 JG |
102 | goto end; |
103 | } | |
3228cc1d PP |
104 | pretty->string = g_string_new(""); |
105 | if (!pretty->string) { | |
6a18b281 MD |
106 | goto error; |
107 | } | |
5280f742 PP |
108 | pretty->tmp_string = g_string_new(""); |
109 | if (!pretty->tmp_string) { | |
110 | goto error; | |
111 | } | |
541b0a11 | 112 | end: |
3228cc1d | 113 | return pretty; |
6a18b281 MD |
114 | |
115 | error: | |
3228cc1d | 116 | g_free(pretty); |
6a18b281 | 117 | return NULL; |
bac67f0f JG |
118 | } |
119 | ||
3228cc1d PP |
120 | BT_HIDDEN |
121 | void pretty_finalize(struct bt_private_component *component) | |
b25bd455 | 122 | { |
890882ef | 123 | void *data = bt_private_component_get_user_data(component); |
b25bd455 | 124 | |
3228cc1d | 125 | destroy_pretty_data(data); |
b25bd455 JG |
126 | } |
127 | ||
bac67f0f | 128 | static |
3228cc1d | 129 | enum bt_component_status handle_notification(struct pretty_component *pretty, |
6e1bc0df | 130 | struct bt_notification *notification) |
4c1456f0 | 131 | { |
541b0a11 | 132 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
541b0a11 | 133 | |
f6ccaed9 | 134 | BT_ASSERT(pretty); |
541b0a11 | 135 | |
7cdc2bab MD |
136 | switch (bt_notification_get_type(notification)) { |
137 | case BT_NOTIFICATION_TYPE_EVENT: | |
d9f65f09 | 138 | ret = pretty_print_event(pretty, notification); |
7cdc2bab MD |
139 | break; |
140 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
141 | fprintf(stderr, "Inactivity notification\n"); | |
142 | break; | |
08899d39 PP |
143 | case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS: |
144 | case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS: | |
145 | ret = pretty_print_discarded_elements(pretty, notification); | |
146 | break; | |
7cdc2bab | 147 | default: |
f42867e2 | 148 | break; |
78586d8a | 149 | } |
b5e978f4 | 150 | |
541b0a11 | 151 | return ret; |
4c1456f0 | 152 | } |
bac67f0f | 153 | |
3228cc1d | 154 | BT_HIDDEN |
0d8b4d8e | 155 | void pretty_port_connected( |
890882ef | 156 | struct bt_private_component *component, |
8f4799f7 PP |
157 | struct bt_private_port *self_port, |
158 | struct bt_port *other_port) | |
75e1829b | 159 | { |
73d5c1ad | 160 | enum bt_connection_status conn_status; |
890882ef | 161 | struct bt_private_connection *connection; |
3228cc1d | 162 | struct pretty_component *pretty; |
75e1829b | 163 | |
3228cc1d | 164 | pretty = bt_private_component_get_user_data(component); |
f6ccaed9 PP |
165 | BT_ASSERT(pretty); |
166 | BT_ASSERT(!pretty->input_iterator); | |
890882ef | 167 | connection = bt_private_port_get_private_connection(self_port); |
f6ccaed9 | 168 | BT_ASSERT(connection); |
73d5c1ad | 169 | conn_status = bt_private_connection_create_notification_iterator( |
f42867e2 | 170 | connection, &pretty->input_iterator); |
73d5c1ad | 171 | if (conn_status != BT_CONNECTION_STATUS_OK) { |
0d8b4d8e | 172 | pretty->error = true; |
75e1829b | 173 | } |
72b913fb PP |
174 | |
175 | bt_put(connection); | |
75e1829b JG |
176 | } |
177 | ||
3228cc1d PP |
178 | BT_HIDDEN |
179 | enum bt_component_status pretty_consume(struct bt_private_component *component) | |
fec2a9f2 JG |
180 | { |
181 | enum bt_component_status ret; | |
182 | struct bt_notification *notification = NULL; | |
183 | struct bt_notification_iterator *it; | |
3228cc1d | 184 | struct pretty_component *pretty = |
890882ef | 185 | bt_private_component_get_user_data(component); |
41a2b7ae | 186 | enum bt_notification_iterator_status it_ret; |
fec2a9f2 | 187 | |
0d8b4d8e PP |
188 | if (unlikely(pretty->error)) { |
189 | ret = BT_COMPONENT_STATUS_ERROR; | |
190 | goto end; | |
191 | } | |
fec2a9f2 | 192 | |
0d8b4d8e | 193 | it = pretty->input_iterator; |
41a2b7ae | 194 | it_ret = bt_notification_iterator_next(it); |
0d8b4d8e | 195 | |
41a2b7ae | 196 | switch (it_ret) { |
41a2b7ae PP |
197 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
198 | ret = BT_COMPONENT_STATUS_END; | |
3228cc1d | 199 | BT_PUT(pretty->input_iterator); |
41a2b7ae | 200 | goto end; |
dfa6653c PP |
201 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: |
202 | ret = BT_COMPONENT_STATUS_AGAIN; | |
203 | goto end; | |
204 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
41a2b7ae | 205 | break; |
dfa6653c PP |
206 | default: |
207 | ret = BT_COMPONENT_STATUS_ERROR; | |
208 | goto end; | |
fec2a9f2 JG |
209 | } |
210 | ||
41a2b7ae | 211 | notification = bt_notification_iterator_get_notification(it); |
f6ccaed9 | 212 | BT_ASSERT(notification); |
3228cc1d | 213 | ret = handle_notification(pretty, notification); |
dfa6653c | 214 | |
fec2a9f2 | 215 | end: |
fec2a9f2 JG |
216 | bt_put(notification); |
217 | return ret; | |
218 | } | |
219 | ||
6e1bc0df MD |
220 | static |
221 | enum bt_component_status add_params_to_map(struct bt_value *plugin_opt_map) | |
222 | { | |
223 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
224 | unsigned int i; | |
225 | ||
226 | for (i = 0; i < BT_ARRAY_SIZE(plugin_options); i++) { | |
227 | const char *key = plugin_options[i]; | |
228 | enum bt_value_status status; | |
229 | ||
230 | status = bt_value_map_insert(plugin_opt_map, key, bt_value_null); | |
231 | switch (status) { | |
232 | case BT_VALUE_STATUS_OK: | |
233 | break; | |
234 | default: | |
235 | ret = BT_COMPONENT_STATUS_ERROR; | |
236 | goto end; | |
237 | } | |
238 | } | |
239 | end: | |
240 | return ret; | |
241 | } | |
242 | ||
243 | static | |
c55a9f58 | 244 | bt_bool check_param_exists(const char *key, struct bt_value *object, void *data) |
6e1bc0df | 245 | { |
3228cc1d PP |
246 | struct pretty_component *pretty = data; |
247 | struct bt_value *plugin_opt_map = pretty->plugin_opt_map; | |
6e1bc0df MD |
248 | |
249 | if (!bt_value_map_get(plugin_opt_map, key)) { | |
3228cc1d PP |
250 | fprintf(pretty->err, |
251 | "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key); | |
6e1bc0df | 252 | } |
c55a9f58 | 253 | return BT_TRUE; |
6e1bc0df MD |
254 | } |
255 | ||
256 | static | |
257 | enum bt_component_status apply_one_string(const char *key, | |
258 | struct bt_value *params, | |
259 | char **option) | |
260 | { | |
261 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
262 | struct bt_value *value = NULL; | |
263 | enum bt_value_status status; | |
264 | const char *str; | |
265 | ||
266 | value = bt_value_map_get(params, key); | |
267 | if (!value) { | |
268 | goto end; | |
269 | } | |
270 | if (bt_value_is_null(value)) { | |
271 | goto end; | |
272 | } | |
273 | status = bt_value_string_get(value, &str); | |
274 | switch (status) { | |
275 | case BT_VALUE_STATUS_OK: | |
276 | break; | |
277 | default: | |
278 | ret = BT_COMPONENT_STATUS_ERROR; | |
279 | goto end; | |
280 | } | |
281 | *option = g_strdup(str); | |
282 | end: | |
283 | bt_put(value); | |
284 | return ret; | |
285 | } | |
286 | ||
287 | static | |
288 | enum bt_component_status apply_one_bool(const char *key, | |
289 | struct bt_value *params, | |
290 | bool *option, | |
291 | bool *found) | |
292 | { | |
293 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
294 | struct bt_value *value = NULL; | |
295 | enum bt_value_status status; | |
c55a9f58 | 296 | bt_bool bool_val; |
6e1bc0df MD |
297 | |
298 | value = bt_value_map_get(params, key); | |
299 | if (!value) { | |
300 | goto end; | |
301 | } | |
c55a9f58 | 302 | status = bt_value_bool_get(value, &bool_val); |
6e1bc0df MD |
303 | switch (status) { |
304 | case BT_VALUE_STATUS_OK: | |
305 | break; | |
306 | default: | |
307 | ret = BT_COMPONENT_STATUS_ERROR; | |
308 | goto end; | |
309 | } | |
c55a9f58 | 310 | *option = (bool) bool_val; |
6e1bc0df MD |
311 | if (found) { |
312 | *found = true; | |
313 | } | |
314 | end: | |
315 | bt_put(value); | |
316 | return ret; | |
317 | } | |
318 | ||
ad96d936 | 319 | static |
3228cc1d | 320 | void warn_wrong_color_param(struct pretty_component *pretty) |
ad96d936 | 321 | { |
3228cc1d | 322 | fprintf(pretty->err, |
ad96d936 PP |
323 | "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n"); |
324 | } | |
325 | ||
77986bad | 326 | static |
3228cc1d | 327 | enum bt_component_status open_output_file(struct pretty_component *pretty) |
77986bad JD |
328 | { |
329 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
330 | ||
3228cc1d | 331 | if (!pretty->options.output_path) { |
77986bad JD |
332 | goto end; |
333 | } | |
334 | ||
3228cc1d PP |
335 | pretty->out = fopen(pretty->options.output_path, "w"); |
336 | if (!pretty->out) { | |
77986bad JD |
337 | goto error; |
338 | } | |
339 | ||
340 | goto end; | |
341 | ||
342 | error: | |
343 | ret = BT_COMPONENT_STATUS_ERROR; | |
344 | end: | |
345 | return ret; | |
346 | } | |
347 | ||
6e1bc0df | 348 | static |
3228cc1d | 349 | enum bt_component_status apply_params(struct pretty_component *pretty, |
6e1bc0df MD |
350 | struct bt_value *params) |
351 | { | |
352 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
353 | enum bt_value_status status; | |
354 | bool value, found; | |
355 | char *str = NULL; | |
356 | ||
3228cc1d PP |
357 | pretty->plugin_opt_map = bt_value_map_create(); |
358 | if (!pretty->plugin_opt_map) { | |
6e1bc0df MD |
359 | ret = BT_COMPONENT_STATUS_ERROR; |
360 | goto end; | |
361 | } | |
3228cc1d | 362 | ret = add_params_to_map(pretty->plugin_opt_map); |
6e1bc0df MD |
363 | if (ret != BT_COMPONENT_STATUS_OK) { |
364 | goto end; | |
365 | } | |
366 | /* Report unknown parameters. */ | |
3228cc1d | 367 | status = bt_value_map_foreach(params, check_param_exists, pretty); |
6e1bc0df MD |
368 | switch (status) { |
369 | case BT_VALUE_STATUS_OK: | |
370 | break; | |
371 | default: | |
372 | ret = BT_COMPONENT_STATUS_ERROR; | |
373 | goto end; | |
374 | } | |
375 | /* Known parameters. */ | |
3228cc1d | 376 | pretty->options.color = PRETTY_COLOR_OPT_AUTO; |
ad96d936 PP |
377 | if (bt_value_map_has_key(params, "color")) { |
378 | struct bt_value *color_value; | |
379 | const char *color; | |
380 | ||
381 | color_value = bt_value_map_get(params, "color"); | |
382 | if (!color_value) { | |
383 | goto end; | |
384 | } | |
385 | ||
72b913fb PP |
386 | status = bt_value_string_get(color_value, &color); |
387 | if (status) { | |
3228cc1d | 388 | warn_wrong_color_param(pretty); |
ad96d936 PP |
389 | } else { |
390 | if (strcmp(color, "never") == 0) { | |
3228cc1d | 391 | pretty->options.color = PRETTY_COLOR_OPT_NEVER; |
ad96d936 | 392 | } else if (strcmp(color, "auto") == 0) { |
3228cc1d | 393 | pretty->options.color = PRETTY_COLOR_OPT_AUTO; |
ad96d936 | 394 | } else if (strcmp(color, "always") == 0) { |
3228cc1d | 395 | pretty->options.color = PRETTY_COLOR_OPT_ALWAYS; |
ad96d936 | 396 | } else { |
3228cc1d | 397 | warn_wrong_color_param(pretty); |
ad96d936 PP |
398 | } |
399 | } | |
400 | ||
401 | bt_put(color_value); | |
402 | } | |
403 | ||
a0ad501b | 404 | ret = apply_one_string("path", params, &pretty->options.output_path); |
6e1bc0df MD |
405 | if (ret != BT_COMPONENT_STATUS_OK) { |
406 | goto end; | |
407 | } | |
3228cc1d | 408 | ret = open_output_file(pretty); |
77986bad JD |
409 | if (ret != BT_COMPONENT_STATUS_OK) { |
410 | goto end; | |
411 | } | |
6e1bc0df | 412 | |
6e1bc0df MD |
413 | value = false; /* Default. */ |
414 | ret = apply_one_bool("no-delta", params, &value, NULL); | |
415 | if (ret != BT_COMPONENT_STATUS_OK) { | |
416 | goto end; | |
417 | } | |
3228cc1d | 418 | pretty->options.print_delta_field = !value; /* Reverse logic. */ |
6e1bc0df MD |
419 | |
420 | value = false; /* Default. */ | |
421 | ret = apply_one_bool("clock-cycles", params, &value, NULL); | |
422 | if (ret != BT_COMPONENT_STATUS_OK) { | |
423 | goto end; | |
424 | } | |
3228cc1d | 425 | pretty->options.print_timestamp_cycles = value; |
6e1bc0df MD |
426 | |
427 | value = false; /* Default. */ | |
428 | ret = apply_one_bool("clock-seconds", params, &value, NULL); | |
429 | if (ret != BT_COMPONENT_STATUS_OK) { | |
430 | goto end; | |
431 | } | |
3228cc1d | 432 | pretty->options.clock_seconds = value; |
6e1bc0df MD |
433 | |
434 | value = false; /* Default. */ | |
435 | ret = apply_one_bool("clock-date", params, &value, NULL); | |
436 | if (ret != BT_COMPONENT_STATUS_OK) { | |
437 | goto end; | |
438 | } | |
3228cc1d | 439 | pretty->options.clock_date = value; |
6e1bc0df MD |
440 | |
441 | value = false; /* Default. */ | |
442 | ret = apply_one_bool("clock-gmt", params, &value, NULL); | |
443 | if (ret != BT_COMPONENT_STATUS_OK) { | |
444 | goto end; | |
445 | } | |
3228cc1d | 446 | pretty->options.clock_gmt = value; |
6e1bc0df | 447 | |
a263021c JD |
448 | value = false; /* Default. */ |
449 | ret = apply_one_bool("verbose", params, &value, NULL); | |
450 | if (ret != BT_COMPONENT_STATUS_OK) { | |
451 | goto end; | |
452 | } | |
3228cc1d | 453 | pretty->options.verbose = value; |
a263021c | 454 | |
6e1bc0df MD |
455 | /* Names. */ |
456 | ret = apply_one_string("name-default", params, &str); | |
457 | if (ret != BT_COMPONENT_STATUS_OK) { | |
458 | goto end; | |
459 | } | |
460 | if (!str) { | |
3228cc1d | 461 | pretty->options.name_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 462 | } else if (!strcmp(str, "show")) { |
3228cc1d | 463 | pretty->options.name_default = PRETTY_DEFAULT_SHOW; |
6e1bc0df | 464 | } else if (!strcmp(str, "hide")) { |
3228cc1d | 465 | pretty->options.name_default = PRETTY_DEFAULT_HIDE; |
6e1bc0df MD |
466 | } else { |
467 | ret = BT_COMPONENT_STATUS_ERROR; | |
468 | goto end; | |
469 | } | |
470 | g_free(str); | |
471 | str = NULL; | |
472 | ||
3228cc1d PP |
473 | switch (pretty->options.name_default) { |
474 | case PRETTY_DEFAULT_UNSET: | |
475 | pretty->options.print_payload_field_names = true; | |
476 | pretty->options.print_context_field_names = true; | |
477 | pretty->options.print_header_field_names = false; | |
478 | pretty->options.print_scope_field_names = false; | |
6e1bc0df | 479 | break; |
3228cc1d PP |
480 | case PRETTY_DEFAULT_SHOW: |
481 | pretty->options.print_payload_field_names = true; | |
482 | pretty->options.print_context_field_names = true; | |
483 | pretty->options.print_header_field_names = true; | |
484 | pretty->options.print_scope_field_names = true; | |
6e1bc0df | 485 | break; |
3228cc1d PP |
486 | case PRETTY_DEFAULT_HIDE: |
487 | pretty->options.print_payload_field_names = false; | |
488 | pretty->options.print_context_field_names = false; | |
489 | pretty->options.print_header_field_names = false; | |
490 | pretty->options.print_scope_field_names = false; | |
6e1bc0df MD |
491 | break; |
492 | default: | |
493 | ret = BT_COMPONENT_STATUS_ERROR; | |
494 | goto end; | |
495 | } | |
496 | ||
497 | value = false; | |
498 | found = false; | |
499 | ret = apply_one_bool("name-payload", params, &value, &found); | |
500 | if (ret != BT_COMPONENT_STATUS_OK) { | |
501 | goto end; | |
502 | } | |
503 | if (found) { | |
3228cc1d | 504 | pretty->options.print_payload_field_names = value; |
6e1bc0df MD |
505 | } |
506 | ||
507 | value = false; | |
508 | found = false; | |
509 | ret = apply_one_bool("name-context", params, &value, &found); | |
510 | if (ret != BT_COMPONENT_STATUS_OK) { | |
511 | goto end; | |
512 | } | |
513 | if (found) { | |
3228cc1d | 514 | pretty->options.print_context_field_names = value; |
6e1bc0df MD |
515 | } |
516 | ||
517 | value = false; | |
518 | found = false; | |
519 | ret = apply_one_bool("name-header", params, &value, &found); | |
520 | if (ret != BT_COMPONENT_STATUS_OK) { | |
521 | goto end; | |
522 | } | |
523 | if (found) { | |
3228cc1d | 524 | pretty->options.print_header_field_names = value; |
6e1bc0df MD |
525 | } |
526 | ||
527 | value = false; | |
528 | found = false; | |
529 | ret = apply_one_bool("name-scope", params, &value, &found); | |
530 | if (ret != BT_COMPONENT_STATUS_OK) { | |
531 | goto end; | |
532 | } | |
533 | if (found) { | |
3228cc1d | 534 | pretty->options.print_scope_field_names = value; |
6e1bc0df MD |
535 | } |
536 | ||
537 | /* Fields. */ | |
538 | ret = apply_one_string("field-default", params, &str); | |
539 | if (ret != BT_COMPONENT_STATUS_OK) { | |
540 | goto end; | |
541 | } | |
542 | if (!str) { | |
3228cc1d | 543 | pretty->options.field_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 544 | } else if (!strcmp(str, "show")) { |
3228cc1d | 545 | pretty->options.field_default = PRETTY_DEFAULT_SHOW; |
6e1bc0df | 546 | } else if (!strcmp(str, "hide")) { |
3228cc1d | 547 | pretty->options.field_default = PRETTY_DEFAULT_HIDE; |
6e1bc0df MD |
548 | } else { |
549 | ret = BT_COMPONENT_STATUS_ERROR; | |
550 | goto end; | |
551 | } | |
552 | g_free(str); | |
553 | str = NULL; | |
554 | ||
3228cc1d PP |
555 | switch (pretty->options.field_default) { |
556 | case PRETTY_DEFAULT_UNSET: | |
557 | pretty->options.print_trace_field = false; | |
558 | pretty->options.print_trace_hostname_field = true; | |
559 | pretty->options.print_trace_domain_field = false; | |
560 | pretty->options.print_trace_procname_field = true; | |
561 | pretty->options.print_trace_vpid_field = true; | |
562 | pretty->options.print_loglevel_field = false; | |
563 | pretty->options.print_emf_field = false; | |
564 | pretty->options.print_callsite_field = false; | |
6e1bc0df | 565 | break; |
3228cc1d PP |
566 | case PRETTY_DEFAULT_SHOW: |
567 | pretty->options.print_trace_field = true; | |
568 | pretty->options.print_trace_hostname_field = true; | |
569 | pretty->options.print_trace_domain_field = true; | |
570 | pretty->options.print_trace_procname_field = true; | |
571 | pretty->options.print_trace_vpid_field = true; | |
572 | pretty->options.print_loglevel_field = true; | |
573 | pretty->options.print_emf_field = true; | |
574 | pretty->options.print_callsite_field = true; | |
6e1bc0df | 575 | break; |
3228cc1d PP |
576 | case PRETTY_DEFAULT_HIDE: |
577 | pretty->options.print_trace_field = false; | |
578 | pretty->options.print_trace_hostname_field = false; | |
579 | pretty->options.print_trace_domain_field = false; | |
580 | pretty->options.print_trace_procname_field = false; | |
581 | pretty->options.print_trace_vpid_field = false; | |
582 | pretty->options.print_loglevel_field = false; | |
583 | pretty->options.print_emf_field = false; | |
584 | pretty->options.print_callsite_field = false; | |
6e1bc0df MD |
585 | break; |
586 | default: | |
587 | ret = BT_COMPONENT_STATUS_ERROR; | |
588 | goto end; | |
589 | } | |
590 | ||
591 | value = false; | |
592 | found = false; | |
593 | ret = apply_one_bool("field-trace", params, &value, &found); | |
594 | if (ret != BT_COMPONENT_STATUS_OK) { | |
595 | goto end; | |
596 | } | |
597 | if (found) { | |
3228cc1d | 598 | pretty->options.print_trace_field = value; |
6e1bc0df MD |
599 | } |
600 | ||
601 | value = false; | |
602 | found = false; | |
603 | ret = apply_one_bool("field-trace:hostname", params, &value, &found); | |
604 | if (ret != BT_COMPONENT_STATUS_OK) { | |
605 | goto end; | |
606 | } | |
607 | if (found) { | |
3228cc1d | 608 | pretty->options.print_trace_hostname_field = value; |
6e1bc0df MD |
609 | } |
610 | ||
611 | value = false; | |
612 | found = false; | |
613 | ret = apply_one_bool("field-trace:domain", params, &value, &found); | |
614 | if (ret != BT_COMPONENT_STATUS_OK) { | |
615 | goto end; | |
616 | } | |
617 | if (found) { | |
3228cc1d | 618 | pretty->options.print_trace_domain_field = value; |
6e1bc0df MD |
619 | } |
620 | ||
621 | value = false; | |
622 | found = false; | |
623 | ret = apply_one_bool("field-trace:procname", params, &value, &found); | |
624 | if (ret != BT_COMPONENT_STATUS_OK) { | |
625 | goto end; | |
626 | } | |
627 | if (found) { | |
3228cc1d | 628 | pretty->options.print_trace_procname_field = value; |
6e1bc0df MD |
629 | } |
630 | ||
631 | value = false; | |
632 | found = false; | |
633 | ret = apply_one_bool("field-trace:vpid", params, &value, &found); | |
634 | if (ret != BT_COMPONENT_STATUS_OK) { | |
635 | goto end; | |
636 | } | |
637 | if (found) { | |
3228cc1d | 638 | pretty->options.print_trace_vpid_field = value; |
6e1bc0df MD |
639 | } |
640 | ||
641 | value = false; | |
642 | found = false; | |
643 | ret = apply_one_bool("field-loglevel", params, &value, &found); | |
644 | if (ret != BT_COMPONENT_STATUS_OK) { | |
645 | goto end; | |
646 | } | |
647 | if (found) { | |
3228cc1d | 648 | pretty->options.print_loglevel_field = value; |
6e1bc0df MD |
649 | } |
650 | ||
651 | value = false; | |
652 | found = false; | |
653 | ret = apply_one_bool("field-emf", params, &value, &found); | |
654 | if (ret != BT_COMPONENT_STATUS_OK) { | |
655 | goto end; | |
656 | } | |
657 | if (found) { | |
3228cc1d | 658 | pretty->options.print_emf_field = value; |
6e1bc0df MD |
659 | } |
660 | ||
661 | value = false; | |
662 | found = false; | |
60535549 | 663 | ret = apply_one_bool("field-callsite", params, &value, &found); |
6e1bc0df MD |
664 | if (ret != BT_COMPONENT_STATUS_OK) { |
665 | goto end; | |
666 | } | |
667 | if (found) { | |
3228cc1d | 668 | pretty->options.print_callsite_field = value; |
6e1bc0df MD |
669 | } |
670 | ||
6e1bc0df | 671 | end: |
3228cc1d PP |
672 | bt_put(pretty->plugin_opt_map); |
673 | pretty->plugin_opt_map = NULL; | |
6e1bc0df MD |
674 | g_free(str); |
675 | return ret; | |
676 | } | |
677 | ||
ad96d936 | 678 | static |
3228cc1d | 679 | void set_use_colors(struct pretty_component *pretty) |
ad96d936 | 680 | { |
3228cc1d PP |
681 | switch (pretty->options.color) { |
682 | case PRETTY_COLOR_OPT_ALWAYS: | |
683 | pretty->use_colors = true; | |
ad96d936 | 684 | break; |
3228cc1d PP |
685 | case PRETTY_COLOR_OPT_AUTO: |
686 | pretty->use_colors = pretty->out == stdout && | |
ad96d936 PP |
687 | bt_common_colors_supported(); |
688 | break; | |
3228cc1d PP |
689 | case PRETTY_COLOR_OPT_NEVER: |
690 | pretty->use_colors = false; | |
ad96d936 PP |
691 | break; |
692 | } | |
693 | } | |
694 | ||
2b4c4a7c JD |
695 | static |
696 | void init_stream_packet_context_quarks(void) | |
697 | { | |
698 | stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] = | |
699 | g_quark_from_string("timestamp_begin"); | |
700 | stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] = | |
701 | g_quark_from_string("timestamp_begin"); | |
702 | stream_packet_context_quarks[Q_TIMESTAMP_END] = | |
703 | g_quark_from_string("timestamp_end"); | |
704 | stream_packet_context_quarks[Q_EVENTS_DISCARDED] = | |
705 | g_quark_from_string("events_discarded"); | |
706 | stream_packet_context_quarks[Q_CONTENT_SIZE] = | |
707 | g_quark_from_string("content_size"); | |
708 | stream_packet_context_quarks[Q_PACKET_SIZE] = | |
709 | g_quark_from_string("packet_size"); | |
710 | stream_packet_context_quarks[Q_PACKET_SEQ_NUM] = | |
711 | g_quark_from_string("packet_seq_num"); | |
712 | } | |
713 | ||
3228cc1d PP |
714 | BT_HIDDEN |
715 | enum bt_component_status pretty_init( | |
890882ef PP |
716 | struct bt_private_component *component, |
717 | struct bt_value *params, | |
7d61fa8e | 718 | UNUSED_VAR void *init_method_data) |
bac67f0f JG |
719 | { |
720 | enum bt_component_status ret; | |
3228cc1d | 721 | struct pretty_component *pretty = create_pretty(); |
bac67f0f | 722 | |
3228cc1d | 723 | if (!pretty) { |
bac67f0f JG |
724 | ret = BT_COMPONENT_STATUS_NOMEM; |
725 | goto end; | |
726 | } | |
727 | ||
147337a3 PP |
728 | ret = bt_private_component_sink_add_input_private_port(component, |
729 | "in", NULL, NULL); | |
730 | if (ret != BT_COMPONENT_STATUS_OK) { | |
b9d103be PP |
731 | goto end; |
732 | } | |
733 | ||
3228cc1d PP |
734 | pretty->out = stdout; |
735 | pretty->err = stderr; | |
6e1bc0df | 736 | |
3228cc1d PP |
737 | pretty->delta_cycles = -1ULL; |
738 | pretty->last_cycles_timestamp = -1ULL; | |
3af83b5a | 739 | |
3228cc1d PP |
740 | pretty->delta_real_timestamp = -1ULL; |
741 | pretty->last_real_timestamp = -1ULL; | |
3af83b5a | 742 | |
3228cc1d | 743 | ret = apply_params(pretty, params); |
6e1bc0df MD |
744 | if (ret != BT_COMPONENT_STATUS_OK) { |
745 | goto error; | |
746 | } | |
747 | ||
3228cc1d | 748 | set_use_colors(pretty); |
3228cc1d | 749 | ret = bt_private_component_set_user_data(component, pretty); |
bac67f0f JG |
750 | if (ret != BT_COMPONENT_STATUS_OK) { |
751 | goto error; | |
752 | } | |
2b4c4a7c JD |
753 | |
754 | init_stream_packet_context_quarks(); | |
755 | ||
bac67f0f JG |
756 | end: |
757 | return ret; | |
758 | error: | |
3228cc1d | 759 | destroy_pretty_data(pretty); |
bac67f0f JG |
760 | return ret; |
761 | } |