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