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