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 | |
fec2a9f2 | 195 | end: |
d4393e08 | 196 | for (; i < count; i++) { |
d6e69534 | 197 | bt_message_put_ref(msgs[i]); |
d4393e08 PP |
198 | } |
199 | ||
97f261eb | 200 | return status; |
fec2a9f2 JG |
201 | } |
202 | ||
6e1bc0df | 203 | static |
b19ff26f | 204 | void apply_one_string(const char *key, const bt_value *params, char **option) |
6e1bc0df | 205 | { |
b19ff26f | 206 | const bt_value *value = NULL; |
6e1bc0df MD |
207 | const char *str; |
208 | ||
05e21286 | 209 | value = bt_value_map_borrow_entry_value_const(params, key); |
6e1bc0df MD |
210 | if (!value) { |
211 | goto end; | |
212 | } | |
88488ff5 | 213 | |
601b0d3c | 214 | str = bt_value_string_get(value); |
6e1bc0df | 215 | *option = g_strdup(str); |
601b0d3c | 216 | |
6e1bc0df | 217 | end: |
d94d92ac | 218 | return; |
6e1bc0df MD |
219 | } |
220 | ||
88488ff5 SM |
221 | /* |
222 | * Apply parameter with key `key` to `option`. Use `def` as the value, if | |
223 | * the parameter is not specified. | |
224 | */ | |
225 | ||
6e1bc0df | 226 | static |
88488ff5 SM |
227 | void apply_one_bool_with_default(const char *key, const bt_value *params, |
228 | bool *option, bool def) | |
6e1bc0df | 229 | { |
88488ff5 | 230 | const bt_value *value; |
6e1bc0df | 231 | |
05e21286 | 232 | value = bt_value_map_borrow_entry_value_const(params, key); |
88488ff5 SM |
233 | if (value) { |
234 | bt_bool bool_val = bt_value_bool_get(value); | |
a82e90e8 | 235 | |
88488ff5 SM |
236 | *option = (bool) bool_val; |
237 | } else { | |
238 | *option = def; | |
239 | } | |
6e1bc0df MD |
240 | } |
241 | ||
ad96d936 | 242 | static |
88488ff5 | 243 | void apply_one_bool_if_specified(const char *key, const bt_value *params, bool *option) |
ad96d936 | 244 | { |
88488ff5 SM |
245 | const bt_value *value; |
246 | ||
247 | value = bt_value_map_borrow_entry_value_const(params, key); | |
248 | if (value) { | |
249 | bt_bool bool_val = bt_value_bool_get(value); | |
250 | ||
251 | *option = (bool) bool_val; | |
252 | } | |
ad96d936 PP |
253 | } |
254 | ||
77986bad | 255 | static |
d94d92ac | 256 | int open_output_file(struct pretty_component *pretty) |
77986bad | 257 | { |
d94d92ac | 258 | int ret = 0; |
77986bad | 259 | |
3228cc1d | 260 | if (!pretty->options.output_path) { |
77986bad JD |
261 | goto end; |
262 | } | |
263 | ||
3228cc1d PP |
264 | pretty->out = fopen(pretty->options.output_path, "w"); |
265 | if (!pretty->out) { | |
77986bad JD |
266 | goto error; |
267 | } | |
268 | ||
269 | goto end; | |
270 | ||
271 | error: | |
d94d92ac PP |
272 | ret = -1; |
273 | ||
77986bad JD |
274 | end: |
275 | return ret; | |
276 | } | |
277 | ||
88488ff5 SM |
278 | static const char *color_choices[] = { "never", "auto", "always", NULL }; |
279 | static const char *show_hide_choices[] = { "show", "hide", NULL }; | |
280 | ||
d9120ccb | 281 | static |
88488ff5 SM |
282 | struct bt_param_validation_map_value_entry_descr pretty_params[] = { |
283 | { "color", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
284 | .choices = color_choices, | |
285 | } } }, | |
286 | { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } }, | |
287 | { "no-delta", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
288 | { "clock-cycles", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
289 | { "clock-seconds", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
290 | { "clock-date", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
291 | { "clock-gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
292 | { "verbose", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
293 | ||
294 | { "name-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
295 | .choices = show_hide_choices, | |
296 | } } }, | |
297 | { "name-payload", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
298 | { "name-context", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
299 | { "name-scope", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
300 | { "name-header", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
301 | ||
302 | { "field-default", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
303 | .choices = show_hide_choices, | |
304 | } } }, | |
305 | { "field-trace", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
306 | { "field-trace:hostname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
307 | { "field-trace:domain", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
308 | { "field-trace:procname", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
309 | { "field-trace:vpid", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
310 | { "field-loglevel", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
311 | { "field-emf", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
312 | { "field-callsite", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
313 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END | |
314 | }; | |
315 | ||
6e1bc0df | 316 | static |
88488ff5 | 317 | bt_component_class_initialize_method_status apply_params( |
a8e4caa0 | 318 | struct pretty_component *pretty, const bt_value *params) |
6e1bc0df | 319 | { |
88488ff5 SM |
320 | int ret; |
321 | const bt_value *value; | |
322 | bt_component_class_initialize_method_status status; | |
323 | enum bt_param_validation_status validation_status; | |
324 | gchar *validate_error = NULL; | |
325 | ||
326 | validation_status = bt_param_validation_validate(params, | |
327 | pretty_params, &validate_error); | |
328 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
329 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
6e1bc0df | 330 | goto end; |
88488ff5 SM |
331 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { |
332 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
a8e4caa0 | 333 | BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, "%s", validate_error); |
6e1bc0df MD |
334 | goto end; |
335 | } | |
d24d5663 | 336 | |
6e1bc0df | 337 | /* Known parameters. */ |
3228cc1d | 338 | pretty->options.color = PRETTY_COLOR_OPT_AUTO; |
88488ff5 SM |
339 | value = bt_value_map_borrow_entry_value_const(params, "color"); |
340 | if (value) { | |
341 | const char *color = bt_value_string_get(value); | |
601b0d3c PP |
342 | |
343 | if (strcmp(color, "never") == 0) { | |
344 | pretty->options.color = PRETTY_COLOR_OPT_NEVER; | |
345 | } else if (strcmp(color, "auto") == 0) { | |
346 | pretty->options.color = PRETTY_COLOR_OPT_AUTO; | |
ad96d936 | 347 | } else { |
88488ff5 SM |
348 | BT_ASSERT(strcmp(color, "always") == 0); |
349 | pretty->options.color = PRETTY_COLOR_OPT_ALWAYS; | |
ad96d936 | 350 | } |
ad96d936 PP |
351 | } |
352 | ||
d94d92ac | 353 | apply_one_string("path", params, &pretty->options.output_path); |
3228cc1d | 354 | ret = open_output_file(pretty); |
d94d92ac | 355 | if (ret) { |
920b1c37 SM |
356 | BT_COMP_LOGE_APPEND_CAUSE(pretty->self_comp, |
357 | "Failed to open output file: %s", validate_error); | |
88488ff5 | 358 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
77986bad JD |
359 | goto end; |
360 | } | |
6e1bc0df | 361 | |
88488ff5 SM |
362 | apply_one_bool_with_default("no-delta", params, |
363 | &pretty->options.print_delta_field, false); | |
364 | /* Reverse logic. */ | |
365 | pretty->options.print_delta_field = !pretty->options.print_delta_field; | |
6e1bc0df | 366 | |
88488ff5 SM |
367 | apply_one_bool_with_default("clock-cycles", params, |
368 | &pretty->options.print_timestamp_cycles, false); | |
6e1bc0df | 369 | |
88488ff5 SM |
370 | apply_one_bool_with_default("clock-seconds", params, |
371 | &pretty->options.clock_seconds , false); | |
6e1bc0df | 372 | |
88488ff5 SM |
373 | apply_one_bool_with_default("clock-date", params, |
374 | &pretty->options.clock_date, false); | |
6e1bc0df | 375 | |
88488ff5 SM |
376 | apply_one_bool_with_default("clock-gmt", params, |
377 | &pretty->options.clock_gmt, false); | |
6e1bc0df | 378 | |
88488ff5 SM |
379 | apply_one_bool_with_default("verbose", params, |
380 | &pretty->options.verbose, false); | |
a263021c | 381 | |
6e1bc0df | 382 | /* Names. */ |
88488ff5 SM |
383 | value = bt_value_map_borrow_entry_value_const(params, "name-default"); |
384 | if (value) { | |
385 | const char *str = bt_value_string_get(value); | |
386 | ||
387 | if (strcmp(str, "show") == 0) { | |
388 | pretty->options.name_default = PRETTY_DEFAULT_SHOW; | |
389 | } else { | |
390 | BT_ASSERT(strcmp(str, "hide") == 0); | |
391 | pretty->options.name_default = PRETTY_DEFAULT_HIDE; | |
392 | } | |
6e1bc0df | 393 | } else { |
88488ff5 | 394 | pretty->options.name_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 395 | } |
6e1bc0df | 396 | |
3228cc1d PP |
397 | switch (pretty->options.name_default) { |
398 | case PRETTY_DEFAULT_UNSET: | |
399 | pretty->options.print_payload_field_names = true; | |
400 | pretty->options.print_context_field_names = true; | |
401 | pretty->options.print_header_field_names = false; | |
402 | pretty->options.print_scope_field_names = false; | |
6e1bc0df | 403 | break; |
3228cc1d PP |
404 | case PRETTY_DEFAULT_SHOW: |
405 | pretty->options.print_payload_field_names = true; | |
406 | pretty->options.print_context_field_names = true; | |
407 | pretty->options.print_header_field_names = true; | |
408 | pretty->options.print_scope_field_names = true; | |
6e1bc0df | 409 | break; |
3228cc1d PP |
410 | case PRETTY_DEFAULT_HIDE: |
411 | pretty->options.print_payload_field_names = false; | |
412 | pretty->options.print_context_field_names = false; | |
413 | pretty->options.print_header_field_names = false; | |
414 | pretty->options.print_scope_field_names = false; | |
6e1bc0df MD |
415 | break; |
416 | default: | |
498e7994 | 417 | bt_common_abort(); |
6e1bc0df MD |
418 | } |
419 | ||
88488ff5 SM |
420 | apply_one_bool_if_specified("name-payload", params, |
421 | &pretty->options.print_payload_field_names); | |
6e1bc0df | 422 | |
88488ff5 SM |
423 | apply_one_bool_if_specified("name-context", params, |
424 | &pretty->options.print_context_field_names); | |
6e1bc0df | 425 | |
88488ff5 SM |
426 | apply_one_bool_if_specified("name-header", params, |
427 | &pretty->options.print_header_field_names); | |
6e1bc0df | 428 | |
88488ff5 SM |
429 | apply_one_bool_if_specified("name-scope", params, |
430 | &pretty->options.print_scope_field_names); | |
6e1bc0df MD |
431 | |
432 | /* Fields. */ | |
88488ff5 SM |
433 | value = bt_value_map_borrow_entry_value_const(params, "field-default"); |
434 | if (value) { | |
435 | const char *str = bt_value_string_get(value); | |
436 | ||
437 | if (strcmp(str, "show") == 0) { | |
438 | pretty->options.field_default = PRETTY_DEFAULT_SHOW; | |
439 | } else { | |
440 | BT_ASSERT(strcmp(str, "hide") == 0); | |
441 | pretty->options.field_default = PRETTY_DEFAULT_HIDE; | |
442 | } | |
6e1bc0df | 443 | } else { |
88488ff5 | 444 | pretty->options.field_default = PRETTY_DEFAULT_UNSET; |
6e1bc0df | 445 | } |
6e1bc0df | 446 | |
3228cc1d PP |
447 | switch (pretty->options.field_default) { |
448 | case PRETTY_DEFAULT_UNSET: | |
449 | pretty->options.print_trace_field = false; | |
450 | pretty->options.print_trace_hostname_field = true; | |
451 | pretty->options.print_trace_domain_field = false; | |
452 | pretty->options.print_trace_procname_field = true; | |
453 | pretty->options.print_trace_vpid_field = true; | |
454 | pretty->options.print_loglevel_field = false; | |
455 | pretty->options.print_emf_field = false; | |
456 | pretty->options.print_callsite_field = false; | |
6e1bc0df | 457 | break; |
3228cc1d PP |
458 | case PRETTY_DEFAULT_SHOW: |
459 | pretty->options.print_trace_field = true; | |
460 | pretty->options.print_trace_hostname_field = true; | |
461 | pretty->options.print_trace_domain_field = true; | |
462 | pretty->options.print_trace_procname_field = true; | |
463 | pretty->options.print_trace_vpid_field = true; | |
464 | pretty->options.print_loglevel_field = true; | |
465 | pretty->options.print_emf_field = true; | |
466 | pretty->options.print_callsite_field = true; | |
6e1bc0df | 467 | break; |
3228cc1d PP |
468 | case PRETTY_DEFAULT_HIDE: |
469 | pretty->options.print_trace_field = false; | |
470 | pretty->options.print_trace_hostname_field = false; | |
471 | pretty->options.print_trace_domain_field = false; | |
472 | pretty->options.print_trace_procname_field = false; | |
473 | pretty->options.print_trace_vpid_field = false; | |
474 | pretty->options.print_loglevel_field = false; | |
475 | pretty->options.print_emf_field = false; | |
476 | pretty->options.print_callsite_field = false; | |
6e1bc0df MD |
477 | break; |
478 | default: | |
498e7994 | 479 | bt_common_abort(); |
6e1bc0df MD |
480 | } |
481 | ||
88488ff5 SM |
482 | apply_one_bool_if_specified("field-trace", params, |
483 | &pretty->options.print_trace_field); | |
6e1bc0df | 484 | |
88488ff5 SM |
485 | apply_one_bool_if_specified("field-trace:hostname", params, |
486 | &pretty->options.print_trace_hostname_field); | |
6e1bc0df | 487 | |
88488ff5 SM |
488 | apply_one_bool_if_specified("field-trace:domain", params, |
489 | &pretty->options.print_trace_domain_field); | |
6e1bc0df | 490 | |
88488ff5 SM |
491 | apply_one_bool_if_specified("field-trace:procname", params, |
492 | &pretty->options.print_trace_procname_field); | |
6e1bc0df | 493 | |
88488ff5 SM |
494 | apply_one_bool_if_specified("field-trace:vpid", params, |
495 | &pretty->options.print_trace_vpid_field); | |
6e1bc0df | 496 | |
88488ff5 SM |
497 | apply_one_bool_if_specified("field-loglevel", params, |
498 | &pretty->options.print_loglevel_field); | |
6e1bc0df | 499 | |
88488ff5 SM |
500 | apply_one_bool_if_specified("field-emf", params, |
501 | &pretty->options.print_emf_field); | |
6e1bc0df | 502 | |
88488ff5 SM |
503 | apply_one_bool_if_specified("field-callsite", params, |
504 | &pretty->options.print_callsite_field); | |
505 | ||
04609487 | 506 | pretty_print_init(); |
88488ff5 | 507 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
6e1bc0df | 508 | |
6e1bc0df | 509 | end: |
88488ff5 SM |
510 | g_free(validate_error); |
511 | ||
512 | return status; | |
6e1bc0df MD |
513 | } |
514 | ||
ad96d936 | 515 | static |
3228cc1d | 516 | void set_use_colors(struct pretty_component *pretty) |
ad96d936 | 517 | { |
3228cc1d PP |
518 | switch (pretty->options.color) { |
519 | case PRETTY_COLOR_OPT_ALWAYS: | |
520 | pretty->use_colors = true; | |
ad96d936 | 521 | break; |
3228cc1d PP |
522 | case PRETTY_COLOR_OPT_AUTO: |
523 | pretty->use_colors = pretty->out == stdout && | |
ad96d936 PP |
524 | bt_common_colors_supported(); |
525 | break; | |
3228cc1d PP |
526 | case PRETTY_COLOR_OPT_NEVER: |
527 | pretty->use_colors = false; | |
ad96d936 PP |
528 | break; |
529 | } | |
530 | } | |
531 | ||
3228cc1d | 532 | BT_HIDDEN |
21a9f056 | 533 | bt_component_class_initialize_method_status pretty_init( |
88488ff5 | 534 | bt_self_component_sink *self_comp_sink, |
59225a3e SM |
535 | bt_self_component_sink_configuration *config, |
536 | const bt_value *params, | |
c88dd1cb | 537 | __attribute__((unused)) void *init_method_data) |
bac67f0f | 538 | { |
88488ff5 SM |
539 | bt_component_class_initialize_method_status status; |
540 | bt_self_component_add_port_status add_port_status; | |
3228cc1d | 541 | struct pretty_component *pretty = create_pretty(); |
88488ff5 SM |
542 | bt_self_component *self_comp = |
543 | bt_self_component_sink_as_self_component(self_comp_sink); | |
544 | const bt_component *comp = bt_self_component_as_component(self_comp); | |
545 | bt_logging_level log_level = bt_component_get_logging_level(comp); | |
bac67f0f | 546 | |
3228cc1d | 547 | if (!pretty) { |
920b1c37 SM |
548 | /* |
549 | * Don't use BT_COMP_LOGE_APPEND_CAUSE, as `pretty` is not | |
550 | * initialized yet. | |
551 | */ | |
552 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, | |
553 | "Failed to allocate component."); | |
554 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( | |
555 | self_comp, "Failed to allocate component."); | |
88488ff5 SM |
556 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
557 | goto error; | |
bac67f0f JG |
558 | } |
559 | ||
a8e4caa0 SM |
560 | pretty->self_comp = self_comp; |
561 | pretty->log_level = log_level; | |
562 | ||
88488ff5 SM |
563 | add_port_status = bt_self_component_sink_add_input_port(self_comp_sink, |
564 | in_port_name, NULL, NULL); | |
565 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { | |
566 | status = (int) add_port_status; | |
d24d5663 | 567 | goto error; |
b9d103be PP |
568 | } |
569 | ||
3228cc1d PP |
570 | pretty->out = stdout; |
571 | pretty->err = stderr; | |
6e1bc0df | 572 | |
3228cc1d PP |
573 | pretty->delta_cycles = -1ULL; |
574 | pretty->last_cycles_timestamp = -1ULL; | |
3af83b5a | 575 | |
3228cc1d PP |
576 | pretty->delta_real_timestamp = -1ULL; |
577 | pretty->last_real_timestamp = -1ULL; | |
3af83b5a | 578 | |
a8e4caa0 | 579 | status = apply_params(pretty, params); |
88488ff5 | 580 | if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { |
6e1bc0df MD |
581 | goto error; |
582 | } | |
583 | ||
3228cc1d | 584 | set_use_colors(pretty); |
88488ff5 | 585 | bt_self_component_set_data(self_comp, pretty); |
2b4c4a7c | 586 | |
88488ff5 SM |
587 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
588 | goto end; | |
d94d92ac | 589 | |
bac67f0f | 590 | error: |
3228cc1d | 591 | destroy_pretty_data(pretty); |
88488ff5 SM |
592 | |
593 | end: | |
594 | return status; | |
bac67f0f | 595 | } |