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 | { |
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)) { | |
920b1c37 SM |
106 | BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, |
107 | "Failed to print one event."); | |
a3f0c7db | 108 | ret = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
d94d92ac | 109 | } |
7cdc2bab | 110 | break; |
8e53bed4 PP |
111 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
112 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
113 | if (pretty_print_discarded_items(pretty, message)) { | |
920b1c37 SM |
114 | BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, |
115 | "Failed to print discarded items."); | |
a3f0c7db | 116 | ret = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
8e53bed4 PP |
117 | } |
118 | break; | |
7cdc2bab | 119 | default: |
f42867e2 | 120 | break; |
78586d8a | 121 | } |
b5e978f4 | 122 | |
541b0a11 | 123 | return ret; |
4c1456f0 | 124 | } |
bac67f0f | 125 | |
3228cc1d | 126 | BT_HIDDEN |
d24d5663 | 127 | bt_component_class_sink_graph_is_configured_method_status |
440ffe59 | 128 | pretty_graph_is_configured(bt_self_component_sink *self_comp_sink) |
75e1829b | 129 | { |
e803df70 | 130 | bt_component_class_sink_graph_is_configured_method_status status; |
9a2c8b8e | 131 | bt_message_iterator_create_from_sink_component_status |
e803df70 | 132 | msg_iter_status; |
3228cc1d | 133 | struct pretty_component *pretty; |
440ffe59 SM |
134 | bt_self_component *self_comp = |
135 | bt_self_component_sink_as_self_component(self_comp_sink); | |
440ffe59 | 136 | bt_self_component_port_input *in_port; |
75e1829b | 137 | |
440ffe59 | 138 | pretty = bt_self_component_get_data(self_comp); |
f6ccaed9 | 139 | BT_ASSERT(pretty); |
d94d92ac | 140 | BT_ASSERT(!pretty->iterator); |
440ffe59 SM |
141 | |
142 | in_port = bt_self_component_sink_borrow_input_port_by_name(self_comp_sink, | |
143 | in_port_name); | |
144 | if (!bt_port_is_connected(bt_port_input_as_port_const( | |
145 | bt_self_component_port_input_as_port_input(in_port)))) { | |
146 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Single input port is not connected: " | |
147 | "port-name=\"%s\"", in_port_name); | |
148 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR; | |
149 | goto end; | |
150 | } | |
151 | ||
9a2c8b8e | 152 | msg_iter_status = bt_message_iterator_create_from_sink_component( |
440ffe59 | 153 | self_comp_sink, in_port, &pretty->iterator); |
9a2c8b8e | 154 | if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { |
e803df70 SM |
155 | status = (int) msg_iter_status; |
156 | goto end; | |
75e1829b | 157 | } |
72b913fb | 158 | |
e803df70 SM |
159 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; |
160 | ||
161 | end: | |
bf55043c | 162 | return status; |
75e1829b JG |
163 | } |
164 | ||
3228cc1d | 165 | BT_HIDDEN |
d24d5663 | 166 | bt_component_class_sink_consume_method_status pretty_consume( |
b19ff26f | 167 | bt_self_component_sink *comp) |
fec2a9f2 | 168 | { |
97f261eb | 169 | bt_component_class_sink_consume_method_status status; |
d6e69534 | 170 | bt_message_array_const msgs; |
9a2c8b8e | 171 | bt_message_iterator *it; |
d94d92ac | 172 | struct pretty_component *pretty = bt_self_component_get_data( |
707b7d35 | 173 | bt_self_component_sink_as_self_component(comp)); |
d24d5663 | 174 | bt_message_iterator_next_status next_status; |
d4393e08 PP |
175 | uint64_t count = 0; |
176 | uint64_t i = 0; | |
fec2a9f2 | 177 | |
d94d92ac | 178 | it = pretty->iterator; |
9a2c8b8e | 179 | next_status = bt_message_iterator_next(it, |
d6e69534 | 180 | &msgs, &count); |
97f261eb SM |
181 | if (next_status != BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) { |
182 | status = (int) next_status; | |
dfa6653c | 183 | goto end; |
fec2a9f2 JG |
184 | } |
185 | ||
d4393e08 | 186 | for (i = 0; i < count; i++) { |
97f261eb SM |
187 | status = (int) handle_message(pretty, msgs[i]); |
188 | if (status) { | |
d4393e08 PP |
189 | goto end; |
190 | } | |
191 | ||
d6e69534 | 192 | bt_message_put_ref(msgs[i]); |
d4393e08 | 193 | } |
dfa6653c | 194 | |
473e4d2a SM |
195 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; |
196 | ||
fec2a9f2 | 197 | end: |
d4393e08 | 198 | for (; i < count; i++) { |
d6e69534 | 199 | bt_message_put_ref(msgs[i]); |
d4393e08 PP |
200 | } |
201 | ||
97f261eb | 202 | return status; |
fec2a9f2 JG |
203 | } |
204 | ||
6e1bc0df | 205 | static |
b19ff26f | 206 | void apply_one_string(const char *key, const bt_value *params, char **option) |
6e1bc0df | 207 | { |
b19ff26f | 208 | const bt_value *value = NULL; |
6e1bc0df MD |
209 | const char *str; |
210 | ||
05e21286 | 211 | value = bt_value_map_borrow_entry_value_const(params, key); |
6e1bc0df MD |
212 | if (!value) { |
213 | goto end; | |
214 | } | |
88488ff5 | 215 | |
601b0d3c | 216 | str = bt_value_string_get(value); |
6e1bc0df | 217 | *option = g_strdup(str); |
601b0d3c | 218 | |
6e1bc0df | 219 | end: |
d94d92ac | 220 | return; |
6e1bc0df MD |
221 | } |
222 | ||
88488ff5 SM |
223 | /* |
224 | * Apply parameter with key `key` to `option`. Use `def` as the value, if | |
225 | * the parameter is not specified. | |
226 | */ | |
227 | ||
6e1bc0df | 228 | static |
88488ff5 SM |
229 | void apply_one_bool_with_default(const char *key, const bt_value *params, |
230 | bool *option, bool def) | |
6e1bc0df | 231 | { |
88488ff5 | 232 | const bt_value *value; |
6e1bc0df | 233 | |
05e21286 | 234 | value = bt_value_map_borrow_entry_value_const(params, key); |
88488ff5 SM |
235 | if (value) { |
236 | bt_bool bool_val = bt_value_bool_get(value); | |
a82e90e8 | 237 | |
88488ff5 SM |
238 | *option = (bool) bool_val; |
239 | } else { | |
240 | *option = def; | |
241 | } | |
6e1bc0df MD |
242 | } |
243 | ||
ad96d936 | 244 | static |
88488ff5 | 245 | void apply_one_bool_if_specified(const char *key, const bt_value *params, bool *option) |
ad96d936 | 246 | { |
88488ff5 SM |
247 | const bt_value *value; |
248 | ||
249 | value = bt_value_map_borrow_entry_value_const(params, key); | |
250 | if (value) { | |
251 | bt_bool bool_val = bt_value_bool_get(value); | |
252 | ||
253 | *option = (bool) bool_val; | |
254 | } | |
ad96d936 PP |
255 | } |
256 | ||
77986bad | 257 | static |
d94d92ac | 258 | int open_output_file(struct pretty_component *pretty) |
77986bad | 259 | { |
d94d92ac | 260 | int ret = 0; |
77986bad | 261 | |
3228cc1d | 262 | if (!pretty->options.output_path) { |
77986bad JD |
263 | goto end; |
264 | } | |
265 | ||
3228cc1d PP |
266 | pretty->out = fopen(pretty->options.output_path, "w"); |
267 | if (!pretty->out) { | |
77986bad JD |
268 | goto error; |
269 | } | |
270 | ||
271 | goto end; | |
272 | ||
273 | error: | |
d94d92ac PP |
274 | ret = -1; |
275 | ||
77986bad JD |
276 | end: |
277 | return ret; | |
278 | } | |
279 | ||
88488ff5 SM |
280 | static const char *color_choices[] = { "never", "auto", "always", NULL }; |
281 | static const char *show_hide_choices[] = { "show", "hide", NULL }; | |
282 | ||
d9120ccb | 283 | static |
88488ff5 SM |
284 | struct bt_param_validation_map_value_entry_descr pretty_params[] = { |
285 | { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
286 | .choices = color_choices, | |
287 | } } }, | |
288 | { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } }, | |
289 | { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
290 | { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
291 | { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
292 | { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
293 | { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
294 | { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
295 | ||
296 | { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
297 | .choices = show_hide_choices, | |
298 | } } }, | |
299 | { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
300 | { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
301 | { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
302 | { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
303 | ||
304 | { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
305 | .choices = show_hide_choices, | |
306 | } } }, | |
307 | { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
308 | { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
309 | { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
310 | { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
311 | { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
312 | { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
313 | { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
314 | { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
315 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END | |
316 | }; | |
317 | ||
6e1bc0df | 318 | static |
88488ff5 | 319 | bt_component_class_initialize_method_status apply_params( |
a8e4caa0 | 320 | struct pretty_component *pretty, const bt_value *params) |
6e1bc0df | 321 | { |
88488ff5 SM |
322 | int ret; |
323 | const bt_value *value; | |
324 | bt_component_class_initialize_method_status status; | |
325 | enum bt_param_validation_status validation_status; | |
326 | gchar *validate_error = NULL; | |
327 | ||
328 | validation_status = bt_param_validation_validate(params, | |
329 | pretty_params, &validate_error); | |
330 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
331 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
6e1bc0df | 332 | goto end; |
88488ff5 SM |
333 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { |
334 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
a8e4caa0 | 335 | BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, "%s", validate_error); |
6e1bc0df MD |
336 | goto end; |
337 | } | |
d24d5663 | 338 | |
6e1bc0df | 339 | /* Known parameters. */ |
3228cc1d | 340 | pretty->options.color = PRETTY_COLOR_OPT_AUTO; |
88488ff5 SM |
341 | value = bt_value_map_borrow_entry_value_const(params, "color"); |
342 | if (value) { | |
343 | const char *color = bt_value_string_get(value); | |
601b0d3c PP |
344 | |
345 | if (strcmp(color, "never") == 0) { | |
346 | pretty->options.color = PRETTY_COLOR_OPT_NEVER; | |
347 | } else if (strcmp(color, "auto") == 0) { | |
348 | pretty->options.color = PRETTY_COLOR_OPT_AUTO; | |
ad96d936 | 349 | } else { |
88488ff5 SM |
350 | BT_ASSERT(strcmp(color, "always") == 0); |
351 | pretty->options.color = PRETTY_COLOR_OPT_ALWAYS; | |
ad96d936 | 352 | } |
ad96d936 PP |
353 | } |
354 | ||
d94d92ac | 355 | apply_one_string("path", params, &pretty->options.output_path); |
3228cc1d | 356 | ret = open_output_file(pretty); |
d94d92ac | 357 | if (ret) { |
920b1c37 SM |
358 | BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, |
359 | "Failed to open output file: %s", validate_error); | |
88488ff5 | 360 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
77986bad JD |
361 | goto end; |
362 | } | |
6e1bc0df | 363 | |
88488ff5 SM |
364 | apply_one_bool_with_default("no-delta", params, |
365 | &pretty->options.print_delta_field, false); | |
366 | /* Reverse logic. */ | |
367 | pretty->options.print_delta_field = !pretty->options.print_delta_field; | |
6e1bc0df | 368 | |
88488ff5 SM |
369 | apply_one_bool_with_default("clock-cycles", params, |
370 | &pretty->options.print_timestamp_cycles, false); | |
6e1bc0df | 371 | |
88488ff5 SM |
372 | apply_one_bool_with_default("clock-seconds", params, |
373 | &pretty->options.clock_seconds , false); | |
6e1bc0df | 374 | |
88488ff5 SM |
375 | apply_one_bool_with_default("clock-date", params, |
376 | &pretty->options.clock_date, false); | |
6e1bc0df | 377 | |
88488ff5 SM |
378 | apply_one_bool_with_default("clock-gmt", params, |
379 | &pretty->options.clock_gmt, false); | |
6e1bc0df | 380 | |
88488ff5 SM |
381 | apply_one_bool_with_default("verbose", params, |
382 | &pretty->options.verbose, false); | |
a263021c | 383 | |
6e1bc0df | 384 | /* Names. */ |
88488ff5 SM |
385 | value = bt_value_map_borrow_entry_value_const(params, "name-default"); |
386 | if (value) { | |
387 | const char *str = bt_value_string_get(value); | |
388 | ||
389 | if (strcmp(str, "show") == 0) { | |
390 | pretty->options.name_default = PRETTY_DEFAULT_SHOW; | |
391 | } else { | |
392 | BT_ASSERT(strcmp(str, "hide") == 0); | |
393 | pretty->options.name_default = PRETTY_DEFAULT_HIDE; | |
394 | } | |
6e1bc0df | 395 | } else { |
88488ff5 | 396 | pretty->options.name_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 397 | } |
6e1bc0df | 398 | |
3228cc1d PP |
399 | switch (pretty->options.name_default) { |
400 | case PRETTY_DEFAULT_UNSET: | |
401 | pretty->options.print_payload_field_names = true; | |
402 | pretty->options.print_context_field_names = true; | |
403 | pretty->options.print_header_field_names = false; | |
404 | pretty->options.print_scope_field_names = false; | |
6e1bc0df | 405 | break; |
3228cc1d PP |
406 | case PRETTY_DEFAULT_SHOW: |
407 | pretty->options.print_payload_field_names = true; | |
408 | pretty->options.print_context_field_names = true; | |
409 | pretty->options.print_header_field_names = true; | |
410 | pretty->options.print_scope_field_names = true; | |
6e1bc0df | 411 | break; |
3228cc1d PP |
412 | case PRETTY_DEFAULT_HIDE: |
413 | pretty->options.print_payload_field_names = false; | |
414 | pretty->options.print_context_field_names = false; | |
415 | pretty->options.print_header_field_names = false; | |
416 | pretty->options.print_scope_field_names = false; | |
6e1bc0df MD |
417 | break; |
418 | default: | |
498e7994 | 419 | bt_common_abort(); |
6e1bc0df MD |
420 | } |
421 | ||
88488ff5 SM |
422 | apply_one_bool_if_specified("name-payload", params, |
423 | &pretty->options.print_payload_field_names); | |
6e1bc0df | 424 | |
88488ff5 SM |
425 | apply_one_bool_if_specified("name-context", params, |
426 | &pretty->options.print_context_field_names); | |
6e1bc0df | 427 | |
88488ff5 SM |
428 | apply_one_bool_if_specified("name-header", params, |
429 | &pretty->options.print_header_field_names); | |
6e1bc0df | 430 | |
88488ff5 SM |
431 | apply_one_bool_if_specified("name-scope", params, |
432 | &pretty->options.print_scope_field_names); | |
6e1bc0df MD |
433 | |
434 | /* Fields. */ | |
88488ff5 SM |
435 | value = bt_value_map_borrow_entry_value_const(params, "field-default"); |
436 | if (value) { | |
437 | const char *str = bt_value_string_get(value); | |
438 | ||
439 | if (strcmp(str, "show") == 0) { | |
440 | pretty->options.field_default = PRETTY_DEFAULT_SHOW; | |
441 | } else { | |
442 | BT_ASSERT(strcmp(str, "hide") == 0); | |
443 | pretty->options.field_default = PRETTY_DEFAULT_HIDE; | |
444 | } | |
6e1bc0df | 445 | } else { |
88488ff5 | 446 | pretty->options.field_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 447 | } |
6e1bc0df | 448 | |
3228cc1d PP |
449 | switch (pretty->options.field_default) { |
450 | case PRETTY_DEFAULT_UNSET: | |
451 | pretty->options.print_trace_field = false; | |
452 | pretty->options.print_trace_hostname_field = true; | |
453 | pretty->options.print_trace_domain_field = false; | |
454 | pretty->options.print_trace_procname_field = true; | |
455 | pretty->options.print_trace_vpid_field = true; | |
456 | pretty->options.print_loglevel_field = false; | |
457 | pretty->options.print_emf_field = false; | |
458 | pretty->options.print_callsite_field = false; | |
6e1bc0df | 459 | break; |
3228cc1d PP |
460 | case PRETTY_DEFAULT_SHOW: |
461 | pretty->options.print_trace_field = true; | |
462 | pretty->options.print_trace_hostname_field = true; | |
463 | pretty->options.print_trace_domain_field = true; | |
464 | pretty->options.print_trace_procname_field = true; | |
465 | pretty->options.print_trace_vpid_field = true; | |
466 | pretty->options.print_loglevel_field = true; | |
467 | pretty->options.print_emf_field = true; | |
468 | pretty->options.print_callsite_field = true; | |
6e1bc0df | 469 | break; |
3228cc1d PP |
470 | case PRETTY_DEFAULT_HIDE: |
471 | pretty->options.print_trace_field = false; | |
472 | pretty->options.print_trace_hostname_field = false; | |
473 | pretty->options.print_trace_domain_field = false; | |
474 | pretty->options.print_trace_procname_field = false; | |
475 | pretty->options.print_trace_vpid_field = false; | |
476 | pretty->options.print_loglevel_field = false; | |
477 | pretty->options.print_emf_field = false; | |
478 | pretty->options.print_callsite_field = false; | |
6e1bc0df MD |
479 | break; |
480 | default: | |
498e7994 | 481 | bt_common_abort(); |
6e1bc0df MD |
482 | } |
483 | ||
88488ff5 SM |
484 | apply_one_bool_if_specified("field-trace", params, |
485 | &pretty->options.print_trace_field); | |
6e1bc0df | 486 | |
88488ff5 SM |
487 | apply_one_bool_if_specified("field-trace:hostname", params, |
488 | &pretty->options.print_trace_hostname_field); | |
6e1bc0df | 489 | |
88488ff5 SM |
490 | apply_one_bool_if_specified("field-trace:domain", params, |
491 | &pretty->options.print_trace_domain_field); | |
6e1bc0df | 492 | |
88488ff5 SM |
493 | apply_one_bool_if_specified("field-trace:procname", params, |
494 | &pretty->options.print_trace_procname_field); | |
6e1bc0df | 495 | |
88488ff5 SM |
496 | apply_one_bool_if_specified("field-trace:vpid", params, |
497 | &pretty->options.print_trace_vpid_field); | |
6e1bc0df | 498 | |
88488ff5 SM |
499 | apply_one_bool_if_specified("field-loglevel", params, |
500 | &pretty->options.print_loglevel_field); | |
6e1bc0df | 501 | |
88488ff5 SM |
502 | apply_one_bool_if_specified("field-emf", params, |
503 | &pretty->options.print_emf_field); | |
6e1bc0df | 504 | |
88488ff5 SM |
505 | apply_one_bool_if_specified("field-callsite", params, |
506 | &pretty->options.print_callsite_field); | |
507 | ||
04609487 | 508 | pretty_print_init(); |
88488ff5 | 509 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
6e1bc0df | 510 | |
6e1bc0df | 511 | end: |
88488ff5 SM |
512 | g_free(validate_error); |
513 | ||
514 | return status; | |
6e1bc0df MD |
515 | } |
516 | ||
ad96d936 | 517 | static |
3228cc1d | 518 | void set_use_colors(struct pretty_component *pretty) |
ad96d936 | 519 | { |
3228cc1d PP |
520 | switch (pretty->options.color) { |
521 | case PRETTY_COLOR_OPT_ALWAYS: | |
522 | pretty->use_colors = true; | |
ad96d936 | 523 | break; |
3228cc1d PP |
524 | case PRETTY_COLOR_OPT_AUTO: |
525 | pretty->use_colors = pretty->out == stdout && | |
ad96d936 PP |
526 | bt_common_colors_supported(); |
527 | break; | |
3228cc1d PP |
528 | case PRETTY_COLOR_OPT_NEVER: |
529 | pretty->use_colors = false; | |
ad96d936 PP |
530 | break; |
531 | } | |
532 | } | |
533 | ||
3228cc1d | 534 | BT_HIDDEN |
21a9f056 | 535 | bt_component_class_initialize_method_status pretty_init( |
88488ff5 | 536 | bt_self_component_sink *self_comp_sink, |
59225a3e SM |
537 | bt_self_component_sink_configuration *config, |
538 | const bt_value *params, | |
c88dd1cb | 539 | __attribute__((unused)) void *init_method_data) |
bac67f0f | 540 | { |
88488ff5 SM |
541 | bt_component_class_initialize_method_status status; |
542 | bt_self_component_add_port_status add_port_status; | |
3228cc1d | 543 | struct pretty_component *pretty = create_pretty(); |
88488ff5 SM |
544 | bt_self_component *self_comp = |
545 | bt_self_component_sink_as_self_component(self_comp_sink); | |
546 | const bt_component *comp = bt_self_component_as_component(self_comp); | |
547 | bt_logging_level log_level = bt_component_get_logging_level(comp); | |
bac67f0f | 548 | |
3228cc1d | 549 | if (!pretty) { |
920b1c37 SM |
550 | /* |
551 | * Don't use BT_COMP_LOGE_APPEND_CAUSE, as `pretty` is not | |
552 | * initialized yet. | |
553 | */ | |
554 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, | |
555 | "Failed to allocate component."); | |
556 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( | |
557 | self_comp, "Failed to allocate component."); | |
88488ff5 SM |
558 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
559 | goto error; | |
bac67f0f JG |
560 | } |
561 | ||
a8e4caa0 SM |
562 | pretty->self_comp = self_comp; |
563 | pretty->log_level = log_level; | |
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 | |
a8e4caa0 | 581 | status = apply_params(pretty, params); |
88488ff5 | 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 | } |