Commit | Line | Data |
---|---|---|
55478183 PP |
1 | /* |
2 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
d400b9e6 | 23 | #define BT_COMP_LOG_SELF_COMP (details_comp->self_comp) |
6c868a3a | 24 | #define BT_LOG_OUTPUT_LEVEL (details_comp->log_level) |
350ad6c1 | 25 | #define BT_LOG_TAG "PLUGIN/SINK.TEXT.DETAILS" |
d9c39b0a | 26 | #include "logging/comp-logging.h" |
55478183 PP |
27 | |
28 | #include <babeltrace2/babeltrace.h> | |
29 | ||
30 | #include "common/common.h" | |
31 | #include "common/assert.h" | |
32 | #include "details.h" | |
33 | #include "write.h" | |
34 | ||
35 | #define LOG_WRONG_PARAM_TYPE(_name, _value, _exp_type) \ | |
36 | do { \ | |
d400b9e6 | 37 | BT_COMP_LOGE("Wrong `%s` parameter type: type=%s, " \ |
55478183 PP |
38 | "expected-type=%s", \ |
39 | (_name), bt_common_value_type_string( \ | |
40 | bt_value_get_type(_value)), \ | |
41 | bt_common_value_type_string(_exp_type)); \ | |
42 | } while (0) | |
43 | ||
44 | static | |
45 | const char * const in_port_name = "in"; | |
46 | ||
47 | static | |
48 | const char * const color_param_name = "color"; | |
49 | ||
50 | static | |
51 | const char * const with_metadata_param_name = "with-metadata"; | |
52 | ||
53 | static | |
54 | const char * const with_time_param_name = "with-time"; | |
55 | ||
55478183 PP |
56 | static |
57 | const char * const with_trace_name_param_name = "with-trace-name"; | |
58 | ||
59 | static | |
60 | const char * const with_stream_class_name_param_name = "with-stream-class-name"; | |
61 | ||
62 | static | |
63 | const char * const with_stream_name_param_name = "with-stream-name"; | |
64 | ||
65 | static | |
66 | const char * const with_uuid_param_name = "with-uuid"; | |
67 | ||
68 | static | |
69 | const char * const compact_param_name = "compact"; | |
70 | ||
71 | BT_HIDDEN | |
72 | void details_destroy_details_trace_class_meta( | |
73 | struct details_trace_class_meta *details_tc_meta) | |
74 | { | |
75 | if (!details_tc_meta) { | |
76 | goto end; | |
77 | } | |
78 | ||
79 | if (details_tc_meta->objects) { | |
80 | g_hash_table_destroy(details_tc_meta->objects); | |
81 | details_tc_meta->objects = NULL; | |
82 | } | |
83 | ||
84 | g_free(details_tc_meta); | |
85 | ||
86 | end: | |
87 | return; | |
88 | } | |
89 | ||
90 | BT_HIDDEN | |
91 | struct details_trace_class_meta *details_create_details_trace_class_meta(void) | |
92 | { | |
93 | struct details_trace_class_meta *details_tc_meta = | |
94 | g_new0(struct details_trace_class_meta, 1); | |
95 | ||
96 | if (!details_tc_meta) { | |
97 | goto end; | |
98 | } | |
99 | ||
100 | details_tc_meta->objects = g_hash_table_new( | |
101 | g_direct_hash, g_direct_equal); | |
102 | if (!details_tc_meta->objects) { | |
103 | details_destroy_details_trace_class_meta(details_tc_meta); | |
104 | details_tc_meta = NULL; | |
105 | goto end; | |
106 | } | |
107 | ||
108 | details_tc_meta->tc_destruction_listener_id = UINT64_C(-1); | |
109 | ||
110 | end: | |
111 | return details_tc_meta; | |
112 | } | |
113 | ||
114 | static | |
115 | void destroy_details_comp(struct details_comp *details_comp) | |
116 | { | |
117 | GHashTableIter iter; | |
118 | gpointer key, value; | |
119 | ||
120 | if (!details_comp) { | |
121 | goto end; | |
122 | } | |
123 | ||
124 | if (details_comp->meta) { | |
125 | /* | |
126 | * Remove trace class destruction listeners, because | |
127 | * otherwise, when they are called, `details_comp` | |
128 | * (their user data) won't exist anymore (we're | |
129 | * destroying it here). | |
130 | */ | |
131 | g_hash_table_iter_init(&iter, details_comp->meta); | |
132 | ||
133 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
134 | struct details_trace_class_meta *details_tc_meta = | |
135 | value; | |
136 | ||
137 | if (details_tc_meta->tc_destruction_listener_id != | |
138 | UINT64_C(-1)) { | |
b80991f6 PP |
139 | if (bt_trace_class_remove_destruction_listener( |
140 | (const void *) key, | |
141 | details_tc_meta->tc_destruction_listener_id)) { | |
142 | bt_current_thread_clear_error(); | |
143 | } | |
55478183 PP |
144 | } |
145 | } | |
146 | ||
147 | g_hash_table_destroy(details_comp->meta); | |
148 | details_comp->meta = NULL; | |
149 | } | |
150 | ||
151 | if (details_comp->traces) { | |
152 | /* | |
153 | * Remove trace destruction listeners, because | |
154 | * otherwise, when they are called, `details_comp` won't | |
155 | * exist anymore (we're destroying it here). | |
156 | */ | |
157 | g_hash_table_iter_init(&iter, details_comp->traces); | |
158 | ||
159 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
160 | struct details_trace *details_trace = value; | |
161 | ||
b80991f6 PP |
162 | if (bt_trace_remove_destruction_listener( |
163 | (const void *) key, | |
164 | details_trace->trace_destruction_listener_id)) { | |
165 | bt_current_thread_clear_error(); | |
166 | } | |
55478183 PP |
167 | } |
168 | ||
169 | g_hash_table_destroy(details_comp->traces); | |
170 | details_comp->traces = NULL; | |
171 | } | |
172 | ||
173 | if (details_comp->str) { | |
174 | g_string_free(details_comp->str, TRUE); | |
175 | details_comp->str = NULL; | |
176 | } | |
177 | ||
178 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET( | |
179 | details_comp->msg_iter); | |
180 | g_free(details_comp); | |
181 | ||
182 | end: | |
183 | return; | |
184 | } | |
185 | ||
186 | static | |
6c868a3a PP |
187 | struct details_comp *create_details_comp( |
188 | bt_self_component_sink *self_comp_sink) | |
55478183 PP |
189 | { |
190 | struct details_comp *details_comp = g_new0(struct details_comp, 1); | |
6c868a3a PP |
191 | bt_self_component *self_comp = |
192 | bt_self_component_sink_as_self_component(self_comp_sink); | |
55478183 PP |
193 | |
194 | if (!details_comp) { | |
195 | goto error; | |
196 | } | |
197 | ||
6c868a3a PP |
198 | details_comp->log_level = bt_component_get_logging_level( |
199 | bt_self_component_as_component(self_comp)); | |
d400b9e6 | 200 | details_comp->self_comp = self_comp; |
55478183 PP |
201 | details_comp->meta = g_hash_table_new_full(g_direct_hash, |
202 | g_direct_equal, NULL, | |
203 | (GDestroyNotify) details_destroy_details_trace_class_meta); | |
204 | if (!details_comp->meta) { | |
205 | goto error; | |
206 | } | |
207 | ||
208 | details_comp->traces = g_hash_table_new_full(g_direct_hash, | |
209 | g_direct_equal, NULL, g_free); | |
210 | if (!details_comp->traces) { | |
211 | goto error; | |
212 | } | |
213 | ||
214 | details_comp->str = g_string_new(NULL); | |
215 | if (!details_comp->str) { | |
216 | goto error; | |
217 | } | |
218 | ||
219 | goto end; | |
220 | ||
221 | error: | |
222 | destroy_details_comp(details_comp); | |
223 | details_comp = NULL; | |
224 | ||
225 | end: | |
226 | return details_comp; | |
227 | } | |
228 | ||
229 | BT_HIDDEN | |
230 | void details_finalize(bt_self_component_sink *comp) | |
231 | { | |
232 | struct details_comp *details_comp; | |
233 | ||
234 | BT_ASSERT(comp); | |
235 | details_comp = bt_self_component_get_data( | |
236 | bt_self_component_sink_as_self_component(comp)); | |
237 | BT_ASSERT(details_comp); | |
238 | destroy_details_comp(details_comp); | |
239 | } | |
240 | ||
241 | static | |
242 | int configure_bool_opt(struct details_comp *details_comp, | |
243 | const bt_value *params, const char *param_name, | |
244 | bool default_value, bool *opt_value) | |
245 | { | |
246 | int ret = 0; | |
247 | const bt_value *value; | |
248 | ||
249 | *opt_value = default_value; | |
250 | value = bt_value_map_borrow_entry_value_const(params, param_name); | |
251 | if (value) { | |
252 | if (!bt_value_is_bool(value)) { | |
253 | LOG_WRONG_PARAM_TYPE(param_name, value, | |
254 | BT_VALUE_TYPE_BOOL); | |
255 | ret = -1; | |
256 | goto end; | |
257 | } | |
258 | ||
259 | *opt_value = (bool) bt_value_bool_get(value); | |
260 | } | |
261 | ||
262 | end: | |
263 | return ret; | |
264 | } | |
265 | ||
266 | static | |
267 | int configure_details_comp(struct details_comp *details_comp, | |
268 | const bt_value *params) | |
269 | { | |
270 | int ret = 0; | |
271 | const bt_value *value; | |
272 | const char *str; | |
273 | ||
274 | /* Colorize output? */ | |
275 | details_comp->cfg.with_color = bt_common_colors_supported(); | |
276 | value = bt_value_map_borrow_entry_value_const(params, color_param_name); | |
277 | if (value) { | |
278 | if (!bt_value_is_string(value)) { | |
279 | LOG_WRONG_PARAM_TYPE(color_param_name, value, | |
280 | BT_VALUE_TYPE_STRING); | |
281 | goto error; | |
282 | } | |
283 | ||
284 | str = bt_value_string_get(value); | |
285 | ||
286 | if (strcmp(str, "never") == 0) { | |
287 | details_comp->cfg.with_color = false; | |
288 | } else if (strcmp(str, "auto") == 0) { | |
289 | details_comp->cfg.with_color = | |
290 | bt_common_colors_supported(); | |
291 | } else if (strcmp(str, "always") == 0) { | |
292 | details_comp->cfg.with_color = true; | |
293 | } else { | |
d400b9e6 | 294 | BT_COMP_LOGE("Invalid `%s` parameter: unknown value " |
55478183 PP |
295 | "(expecting `never`, `auto`, or `always`): " |
296 | "value=\"%s\"", color_param_name, str); | |
297 | goto error; | |
298 | } | |
299 | } | |
300 | ||
301 | /* With metadata objects? */ | |
302 | ret = configure_bool_opt(details_comp, params, with_metadata_param_name, | |
303 | true, &details_comp->cfg.with_meta); | |
304 | if (ret) { | |
305 | goto error; | |
306 | } | |
307 | ||
308 | /* Compact? */ | |
309 | ret = configure_bool_opt(details_comp, params, compact_param_name, | |
310 | false, &details_comp->cfg.compact); | |
311 | if (ret) { | |
312 | goto error; | |
313 | } | |
314 | ||
315 | /* With time? */ | |
316 | ret = configure_bool_opt(details_comp, params, with_time_param_name, | |
317 | true, &details_comp->cfg.with_time); | |
318 | if (ret) { | |
319 | goto error; | |
320 | } | |
321 | ||
55478183 PP |
322 | /* With trace name? */ |
323 | ret = configure_bool_opt(details_comp, params, | |
324 | with_trace_name_param_name, | |
325 | true, &details_comp->cfg.with_trace_name); | |
326 | if (ret) { | |
327 | goto error; | |
328 | } | |
329 | ||
330 | /* With stream class name? */ | |
331 | ret = configure_bool_opt(details_comp, params, | |
332 | with_stream_class_name_param_name, | |
333 | true, &details_comp->cfg.with_stream_class_name); | |
334 | if (ret) { | |
335 | goto error; | |
336 | } | |
337 | ||
338 | /* With stream name? */ | |
339 | ret = configure_bool_opt(details_comp, params, | |
340 | with_stream_name_param_name, | |
341 | true, &details_comp->cfg.with_stream_name); | |
342 | if (ret) { | |
343 | goto error; | |
344 | } | |
345 | ||
346 | /* With UUID? */ | |
347 | ret = configure_bool_opt(details_comp, params, | |
348 | with_uuid_param_name, true, &details_comp->cfg.with_uuid); | |
349 | if (ret) { | |
350 | goto error; | |
351 | } | |
352 | ||
353 | goto end; | |
354 | ||
355 | error: | |
356 | ret = -1; | |
357 | ||
358 | end: | |
359 | return ret; | |
360 | } | |
361 | ||
362 | static | |
363 | void log_configuration(bt_self_component_sink *comp, | |
364 | struct details_comp *details_comp) | |
365 | { | |
d400b9e6 | 366 | BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:", |
55478183 PP |
367 | bt_component_get_name(bt_self_component_as_component( |
368 | bt_self_component_sink_as_self_component(comp)))); | |
d400b9e6 PP |
369 | BT_COMP_LOGI(" Colorize output: %d", details_comp->cfg.with_color); |
370 | BT_COMP_LOGI(" Compact: %d", details_comp->cfg.compact); | |
371 | BT_COMP_LOGI(" With metadata: %d", details_comp->cfg.with_meta); | |
372 | BT_COMP_LOGI(" With time: %d", details_comp->cfg.with_time); | |
d400b9e6 PP |
373 | BT_COMP_LOGI(" With trace name: %d", details_comp->cfg.with_trace_name); |
374 | BT_COMP_LOGI(" With stream class name: %d", | |
55478183 | 375 | details_comp->cfg.with_stream_class_name); |
d400b9e6 PP |
376 | BT_COMP_LOGI(" With stream name: %d", details_comp->cfg.with_stream_name); |
377 | BT_COMP_LOGI(" With UUID: %d", details_comp->cfg.with_uuid); | |
55478183 PP |
378 | } |
379 | ||
380 | BT_HIDDEN | |
d24d5663 | 381 | bt_component_class_init_method_status details_init(bt_self_component_sink *comp, |
55478183 PP |
382 | const bt_value *params, |
383 | __attribute__((unused)) void *init_method_data) | |
384 | { | |
d24d5663 PP |
385 | bt_component_class_init_method_status status = |
386 | BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK; | |
387 | bt_self_component_add_port_status add_port_status; | |
55478183 PP |
388 | struct details_comp *details_comp = NULL; |
389 | ||
d24d5663 PP |
390 | add_port_status = bt_self_component_sink_add_input_port(comp, |
391 | in_port_name, NULL, NULL); | |
392 | switch (add_port_status) { | |
393 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK: | |
394 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK; | |
395 | break; | |
396 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR: | |
397 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR; | |
398 | break; | |
399 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR: | |
400 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR; | |
401 | break; | |
402 | default: | |
403 | abort(); | |
55478183 PP |
404 | } |
405 | ||
6c868a3a | 406 | details_comp = create_details_comp(comp); |
55478183 | 407 | if (!details_comp) { |
d24d5663 | 408 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR; |
55478183 PP |
409 | goto error; |
410 | } | |
411 | ||
412 | if (configure_details_comp(details_comp, params)) { | |
d400b9e6 | 413 | BT_COMP_LOGE_STR("Failed to configure component."); |
55478183 PP |
414 | goto error; |
415 | } | |
416 | ||
417 | log_configuration(comp, details_comp); | |
418 | bt_self_component_set_data( | |
419 | bt_self_component_sink_as_self_component(comp), details_comp); | |
420 | goto end; | |
421 | ||
422 | error: | |
d24d5663 PP |
423 | if (status == BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) { |
424 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR; | |
55478183 PP |
425 | } |
426 | ||
427 | destroy_details_comp(details_comp); | |
428 | ||
429 | end: | |
430 | return status; | |
431 | } | |
432 | ||
433 | BT_HIDDEN | |
d24d5663 PP |
434 | bt_component_class_sink_graph_is_configured_method_status |
435 | details_graph_is_configured(bt_self_component_sink *comp) | |
55478183 | 436 | { |
d24d5663 PP |
437 | bt_component_class_sink_graph_is_configured_method_status status = |
438 | BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; | |
55478183 PP |
439 | bt_self_component_port_input_message_iterator *iterator; |
440 | struct details_comp *details_comp; | |
441 | bt_self_component_port_input *in_port; | |
442 | ||
443 | details_comp = bt_self_component_get_data( | |
444 | bt_self_component_sink_as_self_component(comp)); | |
445 | BT_ASSERT(details_comp); | |
446 | in_port = bt_self_component_sink_borrow_input_port_by_name(comp, | |
447 | in_port_name); | |
448 | if (!bt_port_is_connected(bt_port_input_as_port_const( | |
449 | bt_self_component_port_input_as_port_input(in_port)))) { | |
d400b9e6 | 450 | BT_COMP_LOGE("Single input port is not connected: " |
55478183 | 451 | "port-name=\"%s\"", in_port_name); |
d24d5663 | 452 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR; |
55478183 PP |
453 | goto end; |
454 | } | |
455 | ||
ca02df0a PP |
456 | iterator = bt_self_component_port_input_message_iterator_create_from_sink_component( |
457 | comp, bt_self_component_sink_borrow_input_port_by_name(comp, | |
55478183 PP |
458 | in_port_name)); |
459 | if (!iterator) { | |
d24d5663 | 460 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR; |
55478183 PP |
461 | goto end; |
462 | } | |
463 | ||
464 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF( | |
465 | details_comp->msg_iter, iterator); | |
466 | ||
467 | end: | |
468 | return status; | |
469 | } | |
470 | ||
471 | BT_HIDDEN | |
d24d5663 PP |
472 | bt_component_class_sink_consume_method_status |
473 | details_consume(bt_self_component_sink *comp) | |
55478183 | 474 | { |
d24d5663 PP |
475 | bt_component_class_sink_consume_method_status ret = |
476 | BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; | |
55478183 PP |
477 | bt_message_array_const msgs; |
478 | uint64_t count; | |
479 | struct details_comp *details_comp; | |
d24d5663 | 480 | bt_message_iterator_next_status next_status; |
55478183 PP |
481 | uint64_t i; |
482 | ||
483 | details_comp = bt_self_component_get_data( | |
484 | bt_self_component_sink_as_self_component(comp)); | |
485 | BT_ASSERT(details_comp); | |
486 | BT_ASSERT(details_comp->msg_iter); | |
487 | ||
488 | /* Consume messages */ | |
d24d5663 | 489 | next_status = bt_self_component_port_input_message_iterator_next( |
55478183 | 490 | details_comp->msg_iter, &msgs, &count); |
d24d5663 PP |
491 | switch (next_status) { |
492 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK: | |
493 | ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; | |
55478183 PP |
494 | |
495 | for (i = 0; i < count; i++) { | |
496 | int print_ret = details_write_message(details_comp, | |
497 | msgs[i]); | |
498 | ||
499 | if (print_ret) { | |
500 | for (; i < count; i++) { | |
501 | /* Put all remaining messages */ | |
502 | bt_message_put_ref(msgs[i]); | |
503 | } | |
504 | ||
d24d5663 | 505 | ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; |
55478183 PP |
506 | goto end; |
507 | } | |
508 | ||
509 | /* Print output buffer to standard output and flush */ | |
510 | if (details_comp->str->len > 0) { | |
511 | printf("%s", details_comp->str->str); | |
512 | fflush(stdout); | |
513 | details_comp->printed_something = true; | |
514 | } | |
515 | ||
516 | /* Put this message */ | |
517 | bt_message_put_ref(msgs[i]); | |
518 | } | |
519 | ||
520 | break; | |
d24d5663 PP |
521 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN: |
522 | ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN; | |
55478183 | 523 | goto end; |
d24d5663 PP |
524 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: |
525 | ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; | |
55478183 | 526 | goto end; |
d24d5663 PP |
527 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR: |
528 | ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; | |
55478183 | 529 | goto end; |
d24d5663 PP |
530 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR: |
531 | ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR; | |
55478183 PP |
532 | goto end; |
533 | default: | |
534 | abort(); | |
535 | } | |
536 | ||
537 | end: | |
538 | return ret; | |
539 | } |