Commit | Line | Data |
---|---|---|
7a278c8e | 1 | /* |
2e339de1 | 2 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
f504043c | 3 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7a278c8e JG |
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 | ||
88488ff5 SM |
26 | #define BT_COMP_LOG_SELF_COMP self_comp |
27 | #define BT_LOG_OUTPUT_LEVEL log_level | |
28 | #define BT_LOG_TAG "PLUGIN/SINK.TEXT.PRETTY" | |
29 | #include "logging/comp-logging.h" | |
30 | ||
3fadfbc0 | 31 | #include <babeltrace2/babeltrace.h> |
578e048b MJ |
32 | #include "compat/compiler.h" |
33 | #include "common/common.h" | |
bfd20a42 | 34 | #include <stdio.h> |
39cfa40f | 35 | #include <stdbool.h> |
bac67f0f | 36 | #include <glib.h> |
9957b01a | 37 | #include <string.h> |
578e048b | 38 | #include "common/assert.h" |
88488ff5 | 39 | #include "plugins/common/param-validation/param-validation.h" |
6405967d | 40 | |
3228cc1d PP |
41 | #include "pretty.h" |
42 | ||
5badd463 PP |
43 | static |
44 | const char * const in_port_name = "in"; | |
45 | ||
bfd20a42 | 46 | static |
3228cc1d | 47 | void destroy_pretty_data(struct pretty_component *pretty) |
bac67f0f | 48 | { |
27a1afa7 SM |
49 | if (!pretty) { |
50 | goto end; | |
51 | } | |
52 | ||
96eccbbc SM |
53 | bt_self_component_port_input_message_iterator_put_ref(pretty->iterator); |
54 | ||
5280f742 PP |
55 | if (pretty->string) { |
56 | (void) g_string_free(pretty->string, TRUE); | |
57 | } | |
58 | ||
59 | if (pretty->tmp_string) { | |
60 | (void) g_string_free(pretty->tmp_string, TRUE); | |
61 | } | |
62 | ||
3228cc1d | 63 | if (pretty->out != stdout) { |
77986bad JD |
64 | int ret; |
65 | ||
3228cc1d | 66 | ret = fclose(pretty->out); |
77986bad JD |
67 | if (ret) { |
68 | perror("close output file"); | |
69 | } | |
70 | } | |
3228cc1d | 71 | g_free(pretty->options.output_path); |
3228cc1d | 72 | g_free(pretty); |
27a1afa7 SM |
73 | |
74 | end: | |
75 | return; | |
bac67f0f JG |
76 | } |
77 | ||
b25bd455 | 78 | static |
3228cc1d | 79 | struct pretty_component *create_pretty(void) |
bac67f0f | 80 | { |
3228cc1d | 81 | struct pretty_component *pretty; |
541b0a11 | 82 | |
3228cc1d PP |
83 | pretty = g_new0(struct pretty_component, 1); |
84 | if (!pretty) { | |
541b0a11 JG |
85 | goto end; |
86 | } | |
3228cc1d PP |
87 | pretty->string = g_string_new(""); |
88 | if (!pretty->string) { | |
6a18b281 MD |
89 | goto error; |
90 | } | |
5280f742 PP |
91 | pretty->tmp_string = g_string_new(""); |
92 | if (!pretty->tmp_string) { | |
93 | goto error; | |
94 | } | |
541b0a11 | 95 | end: |
3228cc1d | 96 | return pretty; |
6a18b281 MD |
97 | |
98 | error: | |
3228cc1d | 99 | g_free(pretty); |
6a18b281 | 100 | return NULL; |
bac67f0f JG |
101 | } |
102 | ||
3228cc1d | 103 | BT_HIDDEN |
b19ff26f | 104 | void pretty_finalize(bt_self_component_sink *comp) |
b25bd455 | 105 | { |
d94d92ac PP |
106 | destroy_pretty_data( |
107 | bt_self_component_get_data( | |
707b7d35 | 108 | bt_self_component_sink_as_self_component(comp))); |
b25bd455 JG |
109 | } |
110 | ||
bac67f0f | 111 | static |
d24d5663 | 112 | bt_component_class_message_iterator_next_method_status handle_message( |
d94d92ac | 113 | struct pretty_component *pretty, |
d6e69534 | 114 | const bt_message *message) |
4c1456f0 | 115 | { |
d24d5663 PP |
116 | bt_component_class_message_iterator_next_method_status ret = |
117 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
541b0a11 | 118 | |
98b15851 | 119 | BT_ASSERT_DBG(pretty); |
541b0a11 | 120 | |
d6e69534 | 121 | switch (bt_message_get_type(message)) { |
d6e69534 PP |
122 | case BT_MESSAGE_TYPE_EVENT: |
123 | if (pretty_print_event(pretty, message)) { | |
d24d5663 | 124 | ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
d94d92ac | 125 | } |
7cdc2bab | 126 | break; |
8e53bed4 PP |
127 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
128 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
129 | if (pretty_print_discarded_items(pretty, message)) { | |
d24d5663 | 130 | ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
8e53bed4 PP |
131 | } |
132 | break; | |
7cdc2bab | 133 | default: |
f42867e2 | 134 | break; |
78586d8a | 135 | } |
b5e978f4 | 136 | |
541b0a11 | 137 | return ret; |
4c1456f0 | 138 | } |
bac67f0f | 139 | |
3228cc1d | 140 | BT_HIDDEN |
d24d5663 PP |
141 | bt_component_class_sink_graph_is_configured_method_status |
142 | pretty_graph_is_configured(bt_self_component_sink *comp) | |
75e1829b | 143 | { |
e803df70 SM |
144 | bt_component_class_sink_graph_is_configured_method_status status; |
145 | bt_self_component_port_input_message_iterator_create_from_sink_component_status | |
146 | msg_iter_status; | |
3228cc1d | 147 | struct pretty_component *pretty; |
75e1829b | 148 | |
d94d92ac | 149 | pretty = bt_self_component_get_data( |
707b7d35 | 150 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 151 | BT_ASSERT(pretty); |
d94d92ac | 152 | BT_ASSERT(!pretty->iterator); |
e803df70 | 153 | msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component( |
ca02df0a | 154 | comp, bt_self_component_sink_borrow_input_port_by_name(comp, |
e803df70 SM |
155 | in_port_name), &pretty->iterator); |
156 | if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { | |
157 | status = (int) msg_iter_status; | |
158 | goto end; | |
75e1829b | 159 | } |
72b913fb | 160 | |
e803df70 SM |
161 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; |
162 | ||
163 | end: | |
bf55043c | 164 | return status; |
75e1829b JG |
165 | } |
166 | ||
3228cc1d | 167 | BT_HIDDEN |
d24d5663 | 168 | bt_component_class_sink_consume_method_status pretty_consume( |
b19ff26f | 169 | bt_self_component_sink *comp) |
fec2a9f2 | 170 | { |
b51acf23 JR |
171 | bt_component_class_sink_consume_method_status ret = |
172 | BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; | |
d6e69534 PP |
173 | bt_message_array_const msgs; |
174 | bt_self_component_port_input_message_iterator *it; | |
d94d92ac | 175 | struct pretty_component *pretty = bt_self_component_get_data( |
707b7d35 | 176 | bt_self_component_sink_as_self_component(comp)); |
d24d5663 | 177 | bt_message_iterator_next_status next_status; |
d4393e08 PP |
178 | uint64_t count = 0; |
179 | uint64_t i = 0; | |
fec2a9f2 | 180 | |
d94d92ac | 181 | it = pretty->iterator; |
d24d5663 | 182 | next_status = bt_self_component_port_input_message_iterator_next(it, |
d6e69534 | 183 | &msgs, &count); |
0d8b4d8e | 184 | |
d24d5663 PP |
185 | switch (next_status) { |
186 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK: | |
d94d92ac | 187 | break; |
d24d5663 PP |
188 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR: |
189 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN: | |
190 | ret = (int) next_status; | |
d94d92ac | 191 | goto end; |
d24d5663 PP |
192 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: |
193 | ret = (int) next_status; | |
d6e69534 | 194 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET( |
c5b9b441 | 195 | pretty->iterator); |
dfa6653c | 196 | goto end; |
dfa6653c | 197 | default: |
d24d5663 | 198 | ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; |
dfa6653c | 199 | goto end; |
fec2a9f2 JG |
200 | } |
201 | ||
98b15851 | 202 | BT_ASSERT_DBG(next_status == BT_MESSAGE_ITERATOR_NEXT_STATUS_OK); |
d4393e08 PP |
203 | |
204 | for (i = 0; i < count; i++) { | |
9275bef4 | 205 | ret = (int) handle_message(pretty, msgs[i]); |
d4393e08 PP |
206 | if (ret) { |
207 | goto end; | |
208 | } | |
209 | ||
d6e69534 | 210 | bt_message_put_ref(msgs[i]); |
d4393e08 | 211 | } |
dfa6653c | 212 | |
fec2a9f2 | 213 | end: |
d4393e08 | 214 | for (; i < count; i++) { |
d6e69534 | 215 | bt_message_put_ref(msgs[i]); |
d4393e08 PP |
216 | } |
217 | ||
fec2a9f2 JG |
218 | return ret; |
219 | } | |
220 | ||
6e1bc0df | 221 | static |
b19ff26f | 222 | void apply_one_string(const char *key, const bt_value *params, char **option) |
6e1bc0df | 223 | { |
b19ff26f | 224 | const bt_value *value = NULL; |
6e1bc0df MD |
225 | const char *str; |
226 | ||
05e21286 | 227 | value = bt_value_map_borrow_entry_value_const(params, key); |
6e1bc0df MD |
228 | if (!value) { |
229 | goto end; | |
230 | } | |
88488ff5 | 231 | |
601b0d3c | 232 | str = bt_value_string_get(value); |
6e1bc0df | 233 | *option = g_strdup(str); |
601b0d3c | 234 | |
6e1bc0df | 235 | end: |
d94d92ac | 236 | return; |
6e1bc0df MD |
237 | } |
238 | ||
88488ff5 SM |
239 | /* |
240 | * Apply parameter with key `key` to `option`. Use `def` as the value, if | |
241 | * the parameter is not specified. | |
242 | */ | |
243 | ||
6e1bc0df | 244 | static |
88488ff5 SM |
245 | void apply_one_bool_with_default(const char *key, const bt_value *params, |
246 | bool *option, bool def) | |
6e1bc0df | 247 | { |
88488ff5 | 248 | const bt_value *value; |
6e1bc0df | 249 | |
05e21286 | 250 | value = bt_value_map_borrow_entry_value_const(params, key); |
88488ff5 SM |
251 | if (value) { |
252 | bt_bool bool_val = bt_value_bool_get(value); | |
a82e90e8 | 253 | |
88488ff5 SM |
254 | *option = (bool) bool_val; |
255 | } else { | |
256 | *option = def; | |
257 | } | |
6e1bc0df MD |
258 | } |
259 | ||
ad96d936 | 260 | static |
88488ff5 | 261 | void apply_one_bool_if_specified(const char *key, const bt_value *params, bool *option) |
ad96d936 | 262 | { |
88488ff5 SM |
263 | const bt_value *value; |
264 | ||
265 | value = bt_value_map_borrow_entry_value_const(params, key); | |
266 | if (value) { | |
267 | bt_bool bool_val = bt_value_bool_get(value); | |
268 | ||
269 | *option = (bool) bool_val; | |
270 | } | |
ad96d936 PP |
271 | } |
272 | ||
77986bad | 273 | static |
d94d92ac | 274 | int open_output_file(struct pretty_component *pretty) |
77986bad | 275 | { |
d94d92ac | 276 | int ret = 0; |
77986bad | 277 | |
3228cc1d | 278 | if (!pretty->options.output_path) { |
77986bad JD |
279 | goto end; |
280 | } | |
281 | ||
3228cc1d PP |
282 | pretty->out = fopen(pretty->options.output_path, "w"); |
283 | if (!pretty->out) { | |
77986bad JD |
284 | goto error; |
285 | } | |
286 | ||
287 | goto end; | |
288 | ||
289 | error: | |
d94d92ac PP |
290 | ret = -1; |
291 | ||
77986bad JD |
292 | end: |
293 | return ret; | |
294 | } | |
295 | ||
88488ff5 SM |
296 | static const char *color_choices[] = { "never", "auto", "always", NULL }; |
297 | static const char *show_hide_choices[] = { "show", "hide", NULL }; | |
298 | ||
299 | struct bt_param_validation_map_value_entry_descr pretty_params[] = { | |
300 | { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
301 | .choices = color_choices, | |
302 | } } }, | |
303 | { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } }, | |
304 | { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
305 | { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
306 | { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
307 | { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
308 | { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
309 | { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
310 | ||
311 | { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
312 | .choices = show_hide_choices, | |
313 | } } }, | |
314 | { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
315 | { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
316 | { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
317 | { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
318 | ||
319 | { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
320 | .choices = show_hide_choices, | |
321 | } } }, | |
322 | { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
323 | { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
324 | { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
325 | { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
326 | { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
327 | { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
328 | { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
329 | { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
330 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END | |
331 | }; | |
332 | ||
6e1bc0df | 333 | static |
88488ff5 SM |
334 | bt_component_class_initialize_method_status apply_params( |
335 | struct pretty_component *pretty, const bt_value *params, | |
336 | bt_self_component *self_comp, bt_logging_level log_level) | |
6e1bc0df | 337 | { |
88488ff5 SM |
338 | int ret; |
339 | const bt_value *value; | |
340 | bt_component_class_initialize_method_status status; | |
341 | enum bt_param_validation_status validation_status; | |
342 | gchar *validate_error = NULL; | |
343 | ||
344 | validation_status = bt_param_validation_validate(params, | |
345 | pretty_params, &validate_error); | |
346 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
347 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
6e1bc0df | 348 | goto end; |
88488ff5 SM |
349 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { |
350 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
351 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "%s", validate_error); | |
6e1bc0df MD |
352 | goto end; |
353 | } | |
d24d5663 | 354 | |
6e1bc0df | 355 | /* Known parameters. */ |
3228cc1d | 356 | pretty->options.color = PRETTY_COLOR_OPT_AUTO; |
88488ff5 SM |
357 | value = bt_value_map_borrow_entry_value_const(params, "color"); |
358 | if (value) { | |
359 | const char *color = bt_value_string_get(value); | |
601b0d3c PP |
360 | |
361 | if (strcmp(color, "never") == 0) { | |
362 | pretty->options.color = PRETTY_COLOR_OPT_NEVER; | |
363 | } else if (strcmp(color, "auto") == 0) { | |
364 | pretty->options.color = PRETTY_COLOR_OPT_AUTO; | |
ad96d936 | 365 | } else { |
88488ff5 SM |
366 | BT_ASSERT(strcmp(color, "always") == 0); |
367 | pretty->options.color = PRETTY_COLOR_OPT_ALWAYS; | |
ad96d936 | 368 | } |
ad96d936 PP |
369 | } |
370 | ||
d94d92ac | 371 | apply_one_string("path", params, &pretty->options.output_path); |
3228cc1d | 372 | ret = open_output_file(pretty); |
d94d92ac | 373 | if (ret) { |
88488ff5 | 374 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
77986bad JD |
375 | goto end; |
376 | } | |
6e1bc0df | 377 | |
88488ff5 SM |
378 | apply_one_bool_with_default("no-delta", params, |
379 | &pretty->options.print_delta_field, false); | |
380 | /* Reverse logic. */ | |
381 | pretty->options.print_delta_field = !pretty->options.print_delta_field; | |
6e1bc0df | 382 | |
88488ff5 SM |
383 | apply_one_bool_with_default("clock-cycles", params, |
384 | &pretty->options.print_timestamp_cycles, false); | |
6e1bc0df | 385 | |
88488ff5 SM |
386 | apply_one_bool_with_default("clock-seconds", params, |
387 | &pretty->options.clock_seconds , false); | |
6e1bc0df | 388 | |
88488ff5 SM |
389 | apply_one_bool_with_default("clock-date", params, |
390 | &pretty->options.clock_date, false); | |
6e1bc0df | 391 | |
88488ff5 SM |
392 | apply_one_bool_with_default("clock-gmt", params, |
393 | &pretty->options.clock_gmt, false); | |
6e1bc0df | 394 | |
88488ff5 SM |
395 | apply_one_bool_with_default("verbose", params, |
396 | &pretty->options.verbose, false); | |
a263021c | 397 | |
6e1bc0df | 398 | /* Names. */ |
88488ff5 SM |
399 | value = bt_value_map_borrow_entry_value_const(params, "name-default"); |
400 | if (value) { | |
401 | const char *str = bt_value_string_get(value); | |
402 | ||
403 | if (strcmp(str, "show") == 0) { | |
404 | pretty->options.name_default = PRETTY_DEFAULT_SHOW; | |
405 | } else { | |
406 | BT_ASSERT(strcmp(str, "hide") == 0); | |
407 | pretty->options.name_default = PRETTY_DEFAULT_HIDE; | |
408 | } | |
6e1bc0df | 409 | } else { |
88488ff5 | 410 | pretty->options.name_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 411 | } |
6e1bc0df | 412 | |
3228cc1d PP |
413 | switch (pretty->options.name_default) { |
414 | case PRETTY_DEFAULT_UNSET: | |
415 | pretty->options.print_payload_field_names = true; | |
416 | pretty->options.print_context_field_names = true; | |
417 | pretty->options.print_header_field_names = false; | |
418 | pretty->options.print_scope_field_names = false; | |
6e1bc0df | 419 | break; |
3228cc1d PP |
420 | case PRETTY_DEFAULT_SHOW: |
421 | pretty->options.print_payload_field_names = true; | |
422 | pretty->options.print_context_field_names = true; | |
423 | pretty->options.print_header_field_names = true; | |
424 | pretty->options.print_scope_field_names = true; | |
6e1bc0df | 425 | break; |
3228cc1d PP |
426 | case PRETTY_DEFAULT_HIDE: |
427 | pretty->options.print_payload_field_names = false; | |
428 | pretty->options.print_context_field_names = false; | |
429 | pretty->options.print_header_field_names = false; | |
430 | pretty->options.print_scope_field_names = false; | |
6e1bc0df MD |
431 | break; |
432 | default: | |
498e7994 | 433 | bt_common_abort(); |
6e1bc0df MD |
434 | } |
435 | ||
88488ff5 SM |
436 | apply_one_bool_if_specified("name-payload", params, |
437 | &pretty->options.print_payload_field_names); | |
6e1bc0df | 438 | |
88488ff5 SM |
439 | apply_one_bool_if_specified("name-context", params, |
440 | &pretty->options.print_context_field_names); | |
6e1bc0df | 441 | |
88488ff5 SM |
442 | apply_one_bool_if_specified("name-header", params, |
443 | &pretty->options.print_header_field_names); | |
6e1bc0df | 444 | |
88488ff5 SM |
445 | apply_one_bool_if_specified("name-scope", params, |
446 | &pretty->options.print_scope_field_names); | |
6e1bc0df MD |
447 | |
448 | /* Fields. */ | |
88488ff5 SM |
449 | value = bt_value_map_borrow_entry_value_const(params, "field-default"); |
450 | if (value) { | |
451 | const char *str = bt_value_string_get(value); | |
452 | ||
453 | if (strcmp(str, "show") == 0) { | |
454 | pretty->options.field_default = PRETTY_DEFAULT_SHOW; | |
455 | } else { | |
456 | BT_ASSERT(strcmp(str, "hide") == 0); | |
457 | pretty->options.field_default = PRETTY_DEFAULT_HIDE; | |
458 | } | |
6e1bc0df | 459 | } else { |
88488ff5 | 460 | pretty->options.field_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 461 | } |
6e1bc0df | 462 | |
3228cc1d PP |
463 | switch (pretty->options.field_default) { |
464 | case PRETTY_DEFAULT_UNSET: | |
465 | pretty->options.print_trace_field = false; | |
466 | pretty->options.print_trace_hostname_field = true; | |
467 | pretty->options.print_trace_domain_field = false; | |
468 | pretty->options.print_trace_procname_field = true; | |
469 | pretty->options.print_trace_vpid_field = true; | |
470 | pretty->options.print_loglevel_field = false; | |
471 | pretty->options.print_emf_field = false; | |
472 | pretty->options.print_callsite_field = false; | |
6e1bc0df | 473 | break; |
3228cc1d PP |
474 | case PRETTY_DEFAULT_SHOW: |
475 | pretty->options.print_trace_field = true; | |
476 | pretty->options.print_trace_hostname_field = true; | |
477 | pretty->options.print_trace_domain_field = true; | |
478 | pretty->options.print_trace_procname_field = true; | |
479 | pretty->options.print_trace_vpid_field = true; | |
480 | pretty->options.print_loglevel_field = true; | |
481 | pretty->options.print_emf_field = true; | |
482 | pretty->options.print_callsite_field = true; | |
6e1bc0df | 483 | break; |
3228cc1d PP |
484 | case PRETTY_DEFAULT_HIDE: |
485 | pretty->options.print_trace_field = false; | |
486 | pretty->options.print_trace_hostname_field = false; | |
487 | pretty->options.print_trace_domain_field = false; | |
488 | pretty->options.print_trace_procname_field = false; | |
489 | pretty->options.print_trace_vpid_field = false; | |
490 | pretty->options.print_loglevel_field = false; | |
491 | pretty->options.print_emf_field = false; | |
492 | pretty->options.print_callsite_field = false; | |
6e1bc0df MD |
493 | break; |
494 | default: | |
498e7994 | 495 | bt_common_abort(); |
6e1bc0df MD |
496 | } |
497 | ||
88488ff5 SM |
498 | apply_one_bool_if_specified("field-trace", params, |
499 | &pretty->options.print_trace_field); | |
6e1bc0df | 500 | |
88488ff5 SM |
501 | apply_one_bool_if_specified("field-trace:hostname", params, |
502 | &pretty->options.print_trace_hostname_field); | |
6e1bc0df | 503 | |
88488ff5 SM |
504 | apply_one_bool_if_specified("field-trace:domain", params, |
505 | &pretty->options.print_trace_domain_field); | |
6e1bc0df | 506 | |
88488ff5 SM |
507 | apply_one_bool_if_specified("field-trace:procname", params, |
508 | &pretty->options.print_trace_procname_field); | |
6e1bc0df | 509 | |
88488ff5 SM |
510 | apply_one_bool_if_specified("field-trace:vpid", params, |
511 | &pretty->options.print_trace_vpid_field); | |
6e1bc0df | 512 | |
88488ff5 SM |
513 | apply_one_bool_if_specified("field-loglevel", params, |
514 | &pretty->options.print_loglevel_field); | |
6e1bc0df | 515 | |
88488ff5 SM |
516 | apply_one_bool_if_specified("field-emf", params, |
517 | &pretty->options.print_emf_field); | |
6e1bc0df | 518 | |
88488ff5 SM |
519 | apply_one_bool_if_specified("field-callsite", params, |
520 | &pretty->options.print_callsite_field); | |
521 | ||
522 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
6e1bc0df | 523 | |
6e1bc0df | 524 | end: |
88488ff5 SM |
525 | g_free(validate_error); |
526 | ||
527 | return status; | |
6e1bc0df MD |
528 | } |
529 | ||
ad96d936 | 530 | static |
3228cc1d | 531 | void set_use_colors(struct pretty_component *pretty) |
ad96d936 | 532 | { |
3228cc1d PP |
533 | switch (pretty->options.color) { |
534 | case PRETTY_COLOR_OPT_ALWAYS: | |
535 | pretty->use_colors = true; | |
ad96d936 | 536 | break; |
3228cc1d PP |
537 | case PRETTY_COLOR_OPT_AUTO: |
538 | pretty->use_colors = pretty->out == stdout && | |
ad96d936 PP |
539 | bt_common_colors_supported(); |
540 | break; | |
3228cc1d PP |
541 | case PRETTY_COLOR_OPT_NEVER: |
542 | pretty->use_colors = false; | |
ad96d936 PP |
543 | break; |
544 | } | |
545 | } | |
546 | ||
3228cc1d | 547 | BT_HIDDEN |
21a9f056 | 548 | bt_component_class_initialize_method_status pretty_init( |
88488ff5 | 549 | bt_self_component_sink *self_comp_sink, |
59225a3e SM |
550 | bt_self_component_sink_configuration *config, |
551 | const bt_value *params, | |
c88dd1cb | 552 | __attribute__((unused)) void *init_method_data) |
bac67f0f | 553 | { |
88488ff5 SM |
554 | bt_component_class_initialize_method_status status; |
555 | bt_self_component_add_port_status add_port_status; | |
3228cc1d | 556 | struct pretty_component *pretty = create_pretty(); |
88488ff5 SM |
557 | bt_self_component *self_comp = |
558 | bt_self_component_sink_as_self_component(self_comp_sink); | |
559 | const bt_component *comp = bt_self_component_as_component(self_comp); | |
560 | bt_logging_level log_level = bt_component_get_logging_level(comp); | |
bac67f0f | 561 | |
3228cc1d | 562 | if (!pretty) { |
88488ff5 SM |
563 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
564 | goto error; | |
bac67f0f JG |
565 | } |
566 | ||
88488ff5 SM |
567 | add_port_status = bt_self_component_sink_add_input_port(self_comp_sink, |
568 | in_port_name, NULL, NULL); | |
569 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { | |
570 | status = (int) add_port_status; | |
d24d5663 | 571 | goto error; |
b9d103be PP |
572 | } |
573 | ||
3228cc1d PP |
574 | pretty->out = stdout; |
575 | pretty->err = stderr; | |
6e1bc0df | 576 | |
3228cc1d PP |
577 | pretty->delta_cycles = -1ULL; |
578 | pretty->last_cycles_timestamp = -1ULL; | |
3af83b5a | 579 | |
3228cc1d PP |
580 | pretty->delta_real_timestamp = -1ULL; |
581 | pretty->last_real_timestamp = -1ULL; | |
3af83b5a | 582 | |
88488ff5 SM |
583 | status = apply_params(pretty, params, self_comp, log_level); |
584 | if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { | |
6e1bc0df MD |
585 | goto error; |
586 | } | |
587 | ||
3228cc1d | 588 | set_use_colors(pretty); |
88488ff5 | 589 | bt_self_component_set_data(self_comp, pretty); |
2b4c4a7c | 590 | |
88488ff5 SM |
591 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
592 | goto end; | |
d94d92ac | 593 | |
bac67f0f | 594 | error: |
3228cc1d | 595 | destroy_pretty_data(pretty); |
88488ff5 SM |
596 | |
597 | end: | |
598 | return status; | |
bac67f0f | 599 | } |