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