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 | ||
a8e4caa0 SM |
8 | #define BT_COMP_LOG_SELF_COMP (pretty->self_comp) |
9 | #define BT_LOG_OUTPUT_LEVEL (pretty->log_level) | |
88488ff5 SM |
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 | { |
5288c8ae | 31 | uint64_t i; |
27a1afa7 SM |
32 | if (!pretty) { |
33 | goto end; | |
34 | } | |
35 | ||
9a2c8b8e | 36 | bt_message_iterator_put_ref(pretty->iterator); |
96eccbbc | 37 | |
5280f742 PP |
38 | if (pretty->string) { |
39 | (void) g_string_free(pretty->string, TRUE); | |
40 | } | |
41 | ||
42 | if (pretty->tmp_string) { | |
43 | (void) g_string_free(pretty->tmp_string, TRUE); | |
44 | } | |
45 | ||
3228cc1d | 46 | if (pretty->out != stdout) { |
77986bad JD |
47 | int ret; |
48 | ||
3228cc1d | 49 | ret = fclose(pretty->out); |
77986bad JD |
50 | if (ret) { |
51 | perror("close output file"); | |
52 | } | |
53 | } | |
5288c8ae GB |
54 | |
55 | for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) { | |
56 | if (pretty->enum_bit_labels[i]) { | |
57 | g_ptr_array_free(pretty->enum_bit_labels[i], true); | |
58 | } | |
59 | } | |
60 | ||
3228cc1d | 61 | g_free(pretty->options.output_path); |
3228cc1d | 62 | g_free(pretty); |
27a1afa7 SM |
63 | |
64 | end: | |
65 | return; | |
bac67f0f JG |
66 | } |
67 | ||
b25bd455 | 68 | static |
3228cc1d | 69 | struct pretty_component *create_pretty(void) |
bac67f0f | 70 | { |
3228cc1d | 71 | struct pretty_component *pretty; |
541b0a11 | 72 | |
3228cc1d PP |
73 | pretty = g_new0(struct pretty_component, 1); |
74 | if (!pretty) { | |
541b0a11 JG |
75 | goto end; |
76 | } | |
3228cc1d PP |
77 | pretty->string = g_string_new(""); |
78 | if (!pretty->string) { | |
6a18b281 MD |
79 | goto error; |
80 | } | |
5280f742 PP |
81 | pretty->tmp_string = g_string_new(""); |
82 | if (!pretty->tmp_string) { | |
83 | goto error; | |
84 | } | |
541b0a11 | 85 | end: |
3228cc1d | 86 | return pretty; |
6a18b281 MD |
87 | |
88 | error: | |
3228cc1d | 89 | g_free(pretty); |
6a18b281 | 90 | return NULL; |
bac67f0f JG |
91 | } |
92 | ||
3228cc1d | 93 | BT_HIDDEN |
b19ff26f | 94 | void pretty_finalize(bt_self_component_sink *comp) |
b25bd455 | 95 | { |
d94d92ac PP |
96 | destroy_pretty_data( |
97 | bt_self_component_get_data( | |
707b7d35 | 98 | bt_self_component_sink_as_self_component(comp))); |
b25bd455 JG |
99 | } |
100 | ||
bac67f0f | 101 | static |
a3f0c7db | 102 | bt_message_iterator_class_next_method_status handle_message( |
d94d92ac | 103 | struct pretty_component *pretty, |
d6e69534 | 104 | const bt_message *message) |
4c1456f0 | 105 | { |
a3f0c7db SM |
106 | bt_message_iterator_class_next_method_status ret = |
107 | BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
541b0a11 | 108 | |
98b15851 | 109 | BT_ASSERT_DBG(pretty); |
541b0a11 | 110 | |
d6e69534 | 111 | switch (bt_message_get_type(message)) { |
d6e69534 PP |
112 | case BT_MESSAGE_TYPE_EVENT: |
113 | if (pretty_print_event(pretty, message)) { | |
920b1c37 SM |
114 | BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, |
115 | "Failed to print one event."); | |
a3f0c7db | 116 | ret = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
d94d92ac | 117 | } |
7cdc2bab | 118 | break; |
8e53bed4 PP |
119 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
120 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
121 | if (pretty_print_discarded_items(pretty, message)) { | |
920b1c37 SM |
122 | BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, |
123 | "Failed to print discarded items."); | |
a3f0c7db | 124 | ret = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
8e53bed4 PP |
125 | } |
126 | break; | |
7cdc2bab | 127 | default: |
f42867e2 | 128 | break; |
78586d8a | 129 | } |
b5e978f4 | 130 | |
541b0a11 | 131 | return ret; |
4c1456f0 | 132 | } |
bac67f0f | 133 | |
3228cc1d | 134 | BT_HIDDEN |
d24d5663 | 135 | bt_component_class_sink_graph_is_configured_method_status |
440ffe59 | 136 | pretty_graph_is_configured(bt_self_component_sink *self_comp_sink) |
75e1829b | 137 | { |
e803df70 | 138 | bt_component_class_sink_graph_is_configured_method_status status; |
9a2c8b8e | 139 | bt_message_iterator_create_from_sink_component_status |
e803df70 | 140 | msg_iter_status; |
3228cc1d | 141 | struct pretty_component *pretty; |
440ffe59 SM |
142 | bt_self_component *self_comp = |
143 | bt_self_component_sink_as_self_component(self_comp_sink); | |
440ffe59 | 144 | bt_self_component_port_input *in_port; |
75e1829b | 145 | |
440ffe59 | 146 | pretty = bt_self_component_get_data(self_comp); |
f6ccaed9 | 147 | BT_ASSERT(pretty); |
d94d92ac | 148 | BT_ASSERT(!pretty->iterator); |
440ffe59 SM |
149 | |
150 | in_port = bt_self_component_sink_borrow_input_port_by_name(self_comp_sink, | |
151 | in_port_name); | |
152 | if (!bt_port_is_connected(bt_port_input_as_port_const( | |
153 | bt_self_component_port_input_as_port_input(in_port)))) { | |
154 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Single input port is not connected: " | |
155 | "port-name=\"%s\"", in_port_name); | |
156 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR; | |
157 | goto end; | |
158 | } | |
159 | ||
9a2c8b8e | 160 | msg_iter_status = bt_message_iterator_create_from_sink_component( |
440ffe59 | 161 | self_comp_sink, in_port, &pretty->iterator); |
9a2c8b8e | 162 | if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { |
e803df70 SM |
163 | status = (int) msg_iter_status; |
164 | goto end; | |
75e1829b | 165 | } |
72b913fb | 166 | |
e803df70 SM |
167 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; |
168 | ||
169 | end: | |
bf55043c | 170 | return status; |
75e1829b JG |
171 | } |
172 | ||
3228cc1d | 173 | BT_HIDDEN |
d24d5663 | 174 | bt_component_class_sink_consume_method_status pretty_consume( |
b19ff26f | 175 | bt_self_component_sink *comp) |
fec2a9f2 | 176 | { |
97f261eb | 177 | bt_component_class_sink_consume_method_status status; |
d6e69534 | 178 | bt_message_array_const msgs; |
9a2c8b8e | 179 | bt_message_iterator *it; |
d94d92ac | 180 | struct pretty_component *pretty = bt_self_component_get_data( |
707b7d35 | 181 | bt_self_component_sink_as_self_component(comp)); |
d24d5663 | 182 | bt_message_iterator_next_status next_status; |
d4393e08 PP |
183 | uint64_t count = 0; |
184 | uint64_t i = 0; | |
fec2a9f2 | 185 | |
d94d92ac | 186 | it = pretty->iterator; |
9a2c8b8e | 187 | next_status = bt_message_iterator_next(it, |
d6e69534 | 188 | &msgs, &count); |
97f261eb SM |
189 | if (next_status != BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) { |
190 | status = (int) next_status; | |
dfa6653c | 191 | goto end; |
fec2a9f2 JG |
192 | } |
193 | ||
d4393e08 | 194 | for (i = 0; i < count; i++) { |
97f261eb SM |
195 | status = (int) handle_message(pretty, msgs[i]); |
196 | if (status) { | |
d4393e08 PP |
197 | goto end; |
198 | } | |
199 | ||
d6e69534 | 200 | bt_message_put_ref(msgs[i]); |
d4393e08 | 201 | } |
dfa6653c | 202 | |
473e4d2a SM |
203 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; |
204 | ||
fec2a9f2 | 205 | end: |
d4393e08 | 206 | for (; i < count; i++) { |
d6e69534 | 207 | bt_message_put_ref(msgs[i]); |
d4393e08 PP |
208 | } |
209 | ||
97f261eb | 210 | return status; |
fec2a9f2 JG |
211 | } |
212 | ||
6e1bc0df | 213 | static |
b19ff26f | 214 | void apply_one_string(const char *key, const bt_value *params, char **option) |
6e1bc0df | 215 | { |
b19ff26f | 216 | const bt_value *value = NULL; |
6e1bc0df MD |
217 | const char *str; |
218 | ||
05e21286 | 219 | value = bt_value_map_borrow_entry_value_const(params, key); |
6e1bc0df MD |
220 | if (!value) { |
221 | goto end; | |
222 | } | |
88488ff5 | 223 | |
601b0d3c | 224 | str = bt_value_string_get(value); |
6e1bc0df | 225 | *option = g_strdup(str); |
601b0d3c | 226 | |
6e1bc0df | 227 | end: |
d94d92ac | 228 | return; |
6e1bc0df MD |
229 | } |
230 | ||
88488ff5 SM |
231 | /* |
232 | * Apply parameter with key `key` to `option`. Use `def` as the value, if | |
233 | * the parameter is not specified. | |
234 | */ | |
235 | ||
6e1bc0df | 236 | static |
88488ff5 SM |
237 | void apply_one_bool_with_default(const char *key, const bt_value *params, |
238 | bool *option, bool def) | |
6e1bc0df | 239 | { |
88488ff5 | 240 | const bt_value *value; |
6e1bc0df | 241 | |
05e21286 | 242 | value = bt_value_map_borrow_entry_value_const(params, key); |
88488ff5 SM |
243 | if (value) { |
244 | bt_bool bool_val = bt_value_bool_get(value); | |
a82e90e8 | 245 | |
88488ff5 SM |
246 | *option = (bool) bool_val; |
247 | } else { | |
248 | *option = def; | |
249 | } | |
6e1bc0df MD |
250 | } |
251 | ||
ad96d936 | 252 | static |
88488ff5 | 253 | void apply_one_bool_if_specified(const char *key, const bt_value *params, bool *option) |
ad96d936 | 254 | { |
88488ff5 SM |
255 | const bt_value *value; |
256 | ||
257 | value = bt_value_map_borrow_entry_value_const(params, key); | |
258 | if (value) { | |
259 | bt_bool bool_val = bt_value_bool_get(value); | |
260 | ||
261 | *option = (bool) bool_val; | |
262 | } | |
ad96d936 PP |
263 | } |
264 | ||
77986bad | 265 | static |
d94d92ac | 266 | int open_output_file(struct pretty_component *pretty) |
77986bad | 267 | { |
d94d92ac | 268 | int ret = 0; |
77986bad | 269 | |
3228cc1d | 270 | if (!pretty->options.output_path) { |
77986bad JD |
271 | goto end; |
272 | } | |
273 | ||
3228cc1d PP |
274 | pretty->out = fopen(pretty->options.output_path, "w"); |
275 | if (!pretty->out) { | |
77986bad JD |
276 | goto error; |
277 | } | |
278 | ||
279 | goto end; | |
280 | ||
281 | error: | |
d94d92ac PP |
282 | ret = -1; |
283 | ||
77986bad JD |
284 | end: |
285 | return ret; | |
286 | } | |
287 | ||
88488ff5 SM |
288 | static const char *color_choices[] = { "never", "auto", "always", NULL }; |
289 | static const char *show_hide_choices[] = { "show", "hide", NULL }; | |
290 | ||
d9120ccb | 291 | static |
88488ff5 SM |
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 } }, | |
5288c8ae | 323 | { "print-enum-flags", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, |
88488ff5 SM |
324 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
325 | }; | |
326 | ||
6e1bc0df | 327 | static |
88488ff5 | 328 | bt_component_class_initialize_method_status apply_params( |
a8e4caa0 | 329 | struct pretty_component *pretty, const bt_value *params) |
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; | |
a8e4caa0 | 344 | BT_COMP_LOGE_APPEND_CAUSE(pretty->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) { |
920b1c37 SM |
367 | BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, |
368 | "Failed to open output file: %s", validate_error); | |
88488ff5 | 369 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
77986bad JD |
370 | goto end; |
371 | } | |
6e1bc0df | 372 | |
88488ff5 SM |
373 | apply_one_bool_with_default("no-delta", params, |
374 | &pretty->options.print_delta_field, false); | |
375 | /* Reverse logic. */ | |
376 | pretty->options.print_delta_field = !pretty->options.print_delta_field; | |
6e1bc0df | 377 | |
88488ff5 SM |
378 | apply_one_bool_with_default("clock-cycles", params, |
379 | &pretty->options.print_timestamp_cycles, false); | |
6e1bc0df | 380 | |
88488ff5 SM |
381 | apply_one_bool_with_default("clock-seconds", params, |
382 | &pretty->options.clock_seconds , false); | |
6e1bc0df | 383 | |
88488ff5 SM |
384 | apply_one_bool_with_default("clock-date", params, |
385 | &pretty->options.clock_date, false); | |
6e1bc0df | 386 | |
88488ff5 SM |
387 | apply_one_bool_with_default("clock-gmt", params, |
388 | &pretty->options.clock_gmt, false); | |
6e1bc0df | 389 | |
88488ff5 SM |
390 | apply_one_bool_with_default("verbose", params, |
391 | &pretty->options.verbose, false); | |
a263021c | 392 | |
5288c8ae GB |
393 | apply_one_bool_with_default("print-enum-flags", params, |
394 | &pretty->options.print_enum_flags, false); | |
395 | ||
6e1bc0df | 396 | /* Names. */ |
88488ff5 SM |
397 | value = bt_value_map_borrow_entry_value_const(params, "name-default"); |
398 | if (value) { | |
399 | const char *str = bt_value_string_get(value); | |
400 | ||
401 | if (strcmp(str, "show") == 0) { | |
402 | pretty->options.name_default = PRETTY_DEFAULT_SHOW; | |
403 | } else { | |
404 | BT_ASSERT(strcmp(str, "hide") == 0); | |
405 | pretty->options.name_default = PRETTY_DEFAULT_HIDE; | |
406 | } | |
6e1bc0df | 407 | } else { |
88488ff5 | 408 | pretty->options.name_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 409 | } |
6e1bc0df | 410 | |
3228cc1d PP |
411 | switch (pretty->options.name_default) { |
412 | case PRETTY_DEFAULT_UNSET: | |
413 | pretty->options.print_payload_field_names = true; | |
414 | pretty->options.print_context_field_names = true; | |
415 | pretty->options.print_header_field_names = false; | |
416 | pretty->options.print_scope_field_names = false; | |
6e1bc0df | 417 | break; |
3228cc1d PP |
418 | case PRETTY_DEFAULT_SHOW: |
419 | pretty->options.print_payload_field_names = true; | |
420 | pretty->options.print_context_field_names = true; | |
421 | pretty->options.print_header_field_names = true; | |
422 | pretty->options.print_scope_field_names = true; | |
6e1bc0df | 423 | break; |
3228cc1d PP |
424 | case PRETTY_DEFAULT_HIDE: |
425 | pretty->options.print_payload_field_names = false; | |
426 | pretty->options.print_context_field_names = false; | |
427 | pretty->options.print_header_field_names = false; | |
428 | pretty->options.print_scope_field_names = false; | |
6e1bc0df MD |
429 | break; |
430 | default: | |
498e7994 | 431 | bt_common_abort(); |
6e1bc0df MD |
432 | } |
433 | ||
88488ff5 SM |
434 | apply_one_bool_if_specified("name-payload", params, |
435 | &pretty->options.print_payload_field_names); | |
6e1bc0df | 436 | |
88488ff5 SM |
437 | apply_one_bool_if_specified("name-context", params, |
438 | &pretty->options.print_context_field_names); | |
6e1bc0df | 439 | |
88488ff5 SM |
440 | apply_one_bool_if_specified("name-header", params, |
441 | &pretty->options.print_header_field_names); | |
6e1bc0df | 442 | |
88488ff5 SM |
443 | apply_one_bool_if_specified("name-scope", params, |
444 | &pretty->options.print_scope_field_names); | |
6e1bc0df MD |
445 | |
446 | /* Fields. */ | |
88488ff5 SM |
447 | value = bt_value_map_borrow_entry_value_const(params, "field-default"); |
448 | if (value) { | |
449 | const char *str = bt_value_string_get(value); | |
450 | ||
451 | if (strcmp(str, "show") == 0) { | |
452 | pretty->options.field_default = PRETTY_DEFAULT_SHOW; | |
453 | } else { | |
454 | BT_ASSERT(strcmp(str, "hide") == 0); | |
455 | pretty->options.field_default = PRETTY_DEFAULT_HIDE; | |
456 | } | |
6e1bc0df | 457 | } else { |
88488ff5 | 458 | pretty->options.field_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 459 | } |
6e1bc0df | 460 | |
3228cc1d PP |
461 | switch (pretty->options.field_default) { |
462 | case PRETTY_DEFAULT_UNSET: | |
463 | pretty->options.print_trace_field = false; | |
464 | pretty->options.print_trace_hostname_field = true; | |
465 | pretty->options.print_trace_domain_field = false; | |
466 | pretty->options.print_trace_procname_field = true; | |
467 | pretty->options.print_trace_vpid_field = true; | |
468 | pretty->options.print_loglevel_field = false; | |
469 | pretty->options.print_emf_field = false; | |
470 | pretty->options.print_callsite_field = false; | |
6e1bc0df | 471 | break; |
3228cc1d PP |
472 | case PRETTY_DEFAULT_SHOW: |
473 | pretty->options.print_trace_field = true; | |
474 | pretty->options.print_trace_hostname_field = true; | |
475 | pretty->options.print_trace_domain_field = true; | |
476 | pretty->options.print_trace_procname_field = true; | |
477 | pretty->options.print_trace_vpid_field = true; | |
478 | pretty->options.print_loglevel_field = true; | |
479 | pretty->options.print_emf_field = true; | |
480 | pretty->options.print_callsite_field = true; | |
6e1bc0df | 481 | break; |
3228cc1d PP |
482 | case PRETTY_DEFAULT_HIDE: |
483 | pretty->options.print_trace_field = false; | |
484 | pretty->options.print_trace_hostname_field = false; | |
485 | pretty->options.print_trace_domain_field = false; | |
486 | pretty->options.print_trace_procname_field = false; | |
487 | pretty->options.print_trace_vpid_field = false; | |
488 | pretty->options.print_loglevel_field = false; | |
489 | pretty->options.print_emf_field = false; | |
490 | pretty->options.print_callsite_field = false; | |
6e1bc0df MD |
491 | break; |
492 | default: | |
498e7994 | 493 | bt_common_abort(); |
6e1bc0df MD |
494 | } |
495 | ||
88488ff5 SM |
496 | apply_one_bool_if_specified("field-trace", params, |
497 | &pretty->options.print_trace_field); | |
6e1bc0df | 498 | |
88488ff5 SM |
499 | apply_one_bool_if_specified("field-trace:hostname", params, |
500 | &pretty->options.print_trace_hostname_field); | |
6e1bc0df | 501 | |
88488ff5 SM |
502 | apply_one_bool_if_specified("field-trace:domain", params, |
503 | &pretty->options.print_trace_domain_field); | |
6e1bc0df | 504 | |
88488ff5 SM |
505 | apply_one_bool_if_specified("field-trace:procname", params, |
506 | &pretty->options.print_trace_procname_field); | |
6e1bc0df | 507 | |
88488ff5 SM |
508 | apply_one_bool_if_specified("field-trace:vpid", params, |
509 | &pretty->options.print_trace_vpid_field); | |
6e1bc0df | 510 | |
88488ff5 SM |
511 | apply_one_bool_if_specified("field-loglevel", params, |
512 | &pretty->options.print_loglevel_field); | |
6e1bc0df | 513 | |
88488ff5 SM |
514 | apply_one_bool_if_specified("field-emf", params, |
515 | &pretty->options.print_emf_field); | |
6e1bc0df | 516 | |
88488ff5 SM |
517 | apply_one_bool_if_specified("field-callsite", params, |
518 | &pretty->options.print_callsite_field); | |
519 | ||
04609487 | 520 | pretty_print_init(); |
88488ff5 | 521 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
6e1bc0df | 522 | |
6e1bc0df | 523 | end: |
88488ff5 SM |
524 | g_free(validate_error); |
525 | ||
526 | return status; | |
6e1bc0df MD |
527 | } |
528 | ||
ad96d936 | 529 | static |
3228cc1d | 530 | void set_use_colors(struct pretty_component *pretty) |
ad96d936 | 531 | { |
3228cc1d PP |
532 | switch (pretty->options.color) { |
533 | case PRETTY_COLOR_OPT_ALWAYS: | |
534 | pretty->use_colors = true; | |
ad96d936 | 535 | break; |
3228cc1d PP |
536 | case PRETTY_COLOR_OPT_AUTO: |
537 | pretty->use_colors = pretty->out == stdout && | |
ad96d936 PP |
538 | bt_common_colors_supported(); |
539 | break; | |
3228cc1d PP |
540 | case PRETTY_COLOR_OPT_NEVER: |
541 | pretty->use_colors = false; | |
ad96d936 PP |
542 | break; |
543 | } | |
544 | } | |
545 | ||
3228cc1d | 546 | BT_HIDDEN |
21a9f056 | 547 | bt_component_class_initialize_method_status pretty_init( |
88488ff5 | 548 | bt_self_component_sink *self_comp_sink, |
59225a3e SM |
549 | bt_self_component_sink_configuration *config, |
550 | const bt_value *params, | |
c88dd1cb | 551 | __attribute__((unused)) void *init_method_data) |
bac67f0f | 552 | { |
88488ff5 SM |
553 | bt_component_class_initialize_method_status status; |
554 | bt_self_component_add_port_status add_port_status; | |
3228cc1d | 555 | struct pretty_component *pretty = create_pretty(); |
88488ff5 SM |
556 | bt_self_component *self_comp = |
557 | bt_self_component_sink_as_self_component(self_comp_sink); | |
558 | const bt_component *comp = bt_self_component_as_component(self_comp); | |
559 | bt_logging_level log_level = bt_component_get_logging_level(comp); | |
bac67f0f | 560 | |
3228cc1d | 561 | if (!pretty) { |
920b1c37 SM |
562 | /* |
563 | * Don't use BT_COMP_LOGE_APPEND_CAUSE, as `pretty` is not | |
564 | * initialized yet. | |
565 | */ | |
566 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, | |
567 | "Failed to allocate component."); | |
568 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( | |
569 | self_comp, "Failed to allocate component."); | |
88488ff5 SM |
570 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
571 | goto error; | |
bac67f0f JG |
572 | } |
573 | ||
a8e4caa0 SM |
574 | pretty->self_comp = self_comp; |
575 | pretty->log_level = log_level; | |
576 | ||
88488ff5 SM |
577 | add_port_status = bt_self_component_sink_add_input_port(self_comp_sink, |
578 | in_port_name, NULL, NULL); | |
579 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { | |
580 | status = (int) add_port_status; | |
d24d5663 | 581 | goto error; |
b9d103be PP |
582 | } |
583 | ||
3228cc1d PP |
584 | pretty->out = stdout; |
585 | pretty->err = stderr; | |
6e1bc0df | 586 | |
3228cc1d PP |
587 | pretty->delta_cycles = -1ULL; |
588 | pretty->last_cycles_timestamp = -1ULL; | |
3af83b5a | 589 | |
3228cc1d PP |
590 | pretty->delta_real_timestamp = -1ULL; |
591 | pretty->last_real_timestamp = -1ULL; | |
3af83b5a | 592 | |
a8e4caa0 | 593 | status = apply_params(pretty, params); |
88488ff5 | 594 | if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { |
6e1bc0df MD |
595 | goto error; |
596 | } | |
597 | ||
3228cc1d | 598 | set_use_colors(pretty); |
5288c8ae GB |
599 | |
600 | if (pretty->options.print_enum_flags) { | |
601 | uint64_t i; | |
602 | /* | |
603 | * Allocate all label arrays during the initialization of the | |
604 | * component and reuse the same set of arrays for all | |
605 | * enumerations. | |
606 | */ | |
607 | for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) { | |
608 | pretty->enum_bit_labels[i] = g_ptr_array_new(); | |
609 | } | |
610 | } | |
88488ff5 | 611 | bt_self_component_set_data(self_comp, pretty); |
2b4c4a7c | 612 | |
88488ff5 SM |
613 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
614 | goto end; | |
d94d92ac | 615 | |
bac67f0f | 616 | error: |
3228cc1d | 617 | destroy_pretty_data(pretty); |
88488ff5 SM |
618 | |
619 | end: | |
620 | return status; | |
bac67f0f | 621 | } |