lib: pass config objects to component init methods
[babeltrace.git] / src / plugins / text / details / details.c
CommitLineData
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
44static
45const char * const in_port_name = "in";
46
47static
48const char * const color_param_name = "color";
49
50static
51const char * const with_metadata_param_name = "with-metadata";
52
cc413248
PP
53static
54const char * const with_data_param_name = "with-data";
55
55478183
PP
56static
57const char * const with_time_param_name = "with-time";
58
55478183
PP
59static
60const char * const with_trace_name_param_name = "with-trace-name";
61
62static
63const char * const with_stream_class_name_param_name = "with-stream-class-name";
64
65static
66const char * const with_stream_name_param_name = "with-stream-name";
67
68static
69const char * const with_uuid_param_name = "with-uuid";
70
71static
72const char * const compact_param_name = "compact";
73
74BT_HIDDEN
75void details_destroy_details_trace_class_meta(
76 struct details_trace_class_meta *details_tc_meta)
77{
78 if (!details_tc_meta) {
79 goto end;
80 }
81
82 if (details_tc_meta->objects) {
83 g_hash_table_destroy(details_tc_meta->objects);
84 details_tc_meta->objects = NULL;
85 }
86
87 g_free(details_tc_meta);
88
89end:
90 return;
91}
92
93BT_HIDDEN
94struct details_trace_class_meta *details_create_details_trace_class_meta(void)
95{
96 struct details_trace_class_meta *details_tc_meta =
97 g_new0(struct details_trace_class_meta, 1);
98
99 if (!details_tc_meta) {
100 goto end;
101 }
102
103 details_tc_meta->objects = g_hash_table_new(
104 g_direct_hash, g_direct_equal);
105 if (!details_tc_meta->objects) {
106 details_destroy_details_trace_class_meta(details_tc_meta);
107 details_tc_meta = NULL;
108 goto end;
109 }
110
111 details_tc_meta->tc_destruction_listener_id = UINT64_C(-1);
112
113end:
114 return details_tc_meta;
115}
116
117static
118void destroy_details_comp(struct details_comp *details_comp)
119{
120 GHashTableIter iter;
121 gpointer key, value;
122
123 if (!details_comp) {
124 goto end;
125 }
126
127 if (details_comp->meta) {
128 /*
129 * Remove trace class destruction listeners, because
130 * otherwise, when they are called, `details_comp`
131 * (their user data) won't exist anymore (we're
132 * destroying it here).
133 */
134 g_hash_table_iter_init(&iter, details_comp->meta);
135
136 while (g_hash_table_iter_next(&iter, &key, &value)) {
137 struct details_trace_class_meta *details_tc_meta =
138 value;
139
140 if (details_tc_meta->tc_destruction_listener_id !=
141 UINT64_C(-1)) {
b80991f6
PP
142 if (bt_trace_class_remove_destruction_listener(
143 (const void *) key,
144 details_tc_meta->tc_destruction_listener_id)) {
145 bt_current_thread_clear_error();
146 }
55478183
PP
147 }
148 }
149
150 g_hash_table_destroy(details_comp->meta);
151 details_comp->meta = NULL;
152 }
153
154 if (details_comp->traces) {
155 /*
156 * Remove trace destruction listeners, because
157 * otherwise, when they are called, `details_comp` won't
158 * exist anymore (we're destroying it here).
159 */
160 g_hash_table_iter_init(&iter, details_comp->traces);
161
162 while (g_hash_table_iter_next(&iter, &key, &value)) {
163 struct details_trace *details_trace = value;
164
b80991f6
PP
165 if (bt_trace_remove_destruction_listener(
166 (const void *) key,
167 details_trace->trace_destruction_listener_id)) {
168 bt_current_thread_clear_error();
169 }
55478183
PP
170 }
171
172 g_hash_table_destroy(details_comp->traces);
173 details_comp->traces = NULL;
174 }
175
176 if (details_comp->str) {
177 g_string_free(details_comp->str, TRUE);
178 details_comp->str = NULL;
179 }
180
181 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
182 details_comp->msg_iter);
183 g_free(details_comp);
184
185end:
186 return;
187}
188
189static
6c868a3a
PP
190struct details_comp *create_details_comp(
191 bt_self_component_sink *self_comp_sink)
55478183
PP
192{
193 struct details_comp *details_comp = g_new0(struct details_comp, 1);
6c868a3a
PP
194 bt_self_component *self_comp =
195 bt_self_component_sink_as_self_component(self_comp_sink);
55478183
PP
196
197 if (!details_comp) {
198 goto error;
199 }
200
6c868a3a
PP
201 details_comp->log_level = bt_component_get_logging_level(
202 bt_self_component_as_component(self_comp));
d400b9e6 203 details_comp->self_comp = self_comp;
55478183
PP
204 details_comp->meta = g_hash_table_new_full(g_direct_hash,
205 g_direct_equal, NULL,
206 (GDestroyNotify) details_destroy_details_trace_class_meta);
207 if (!details_comp->meta) {
208 goto error;
209 }
210
211 details_comp->traces = g_hash_table_new_full(g_direct_hash,
212 g_direct_equal, NULL, g_free);
213 if (!details_comp->traces) {
214 goto error;
215 }
216
217 details_comp->str = g_string_new(NULL);
218 if (!details_comp->str) {
219 goto error;
220 }
221
222 goto end;
223
224error:
225 destroy_details_comp(details_comp);
226 details_comp = NULL;
227
228end:
229 return details_comp;
230}
231
232BT_HIDDEN
233void details_finalize(bt_self_component_sink *comp)
234{
235 struct details_comp *details_comp;
236
237 BT_ASSERT(comp);
238 details_comp = bt_self_component_get_data(
239 bt_self_component_sink_as_self_component(comp));
240 BT_ASSERT(details_comp);
241 destroy_details_comp(details_comp);
242}
243
244static
245int configure_bool_opt(struct details_comp *details_comp,
246 const bt_value *params, const char *param_name,
247 bool default_value, bool *opt_value)
248{
249 int ret = 0;
250 const bt_value *value;
251
252 *opt_value = default_value;
253 value = bt_value_map_borrow_entry_value_const(params, param_name);
254 if (value) {
255 if (!bt_value_is_bool(value)) {
256 LOG_WRONG_PARAM_TYPE(param_name, value,
257 BT_VALUE_TYPE_BOOL);
258 ret = -1;
259 goto end;
260 }
261
262 *opt_value = (bool) bt_value_bool_get(value);
263 }
264
265end:
266 return ret;
267}
268
269static
270int configure_details_comp(struct details_comp *details_comp,
271 const bt_value *params)
272{
273 int ret = 0;
274 const bt_value *value;
275 const char *str;
276
277 /* Colorize output? */
278 details_comp->cfg.with_color = bt_common_colors_supported();
279 value = bt_value_map_borrow_entry_value_const(params, color_param_name);
280 if (value) {
281 if (!bt_value_is_string(value)) {
282 LOG_WRONG_PARAM_TYPE(color_param_name, value,
283 BT_VALUE_TYPE_STRING);
284 goto error;
285 }
286
287 str = bt_value_string_get(value);
288
289 if (strcmp(str, "never") == 0) {
290 details_comp->cfg.with_color = false;
291 } else if (strcmp(str, "auto") == 0) {
292 details_comp->cfg.with_color =
293 bt_common_colors_supported();
294 } else if (strcmp(str, "always") == 0) {
295 details_comp->cfg.with_color = true;
296 } else {
d400b9e6 297 BT_COMP_LOGE("Invalid `%s` parameter: unknown value "
55478183
PP
298 "(expecting `never`, `auto`, or `always`): "
299 "value=\"%s\"", color_param_name, str);
300 goto error;
301 }
302 }
303
304 /* With metadata objects? */
305 ret = configure_bool_opt(details_comp, params, with_metadata_param_name,
306 true, &details_comp->cfg.with_meta);
307 if (ret) {
308 goto error;
309 }
310
cc413248
PP
311 /* With data objects? */
312 ret = configure_bool_opt(details_comp, params, with_data_param_name,
313 true, &details_comp->cfg.with_data);
314 if (ret) {
315 goto error;
316 }
317
55478183
PP
318 /* Compact? */
319 ret = configure_bool_opt(details_comp, params, compact_param_name,
320 false, &details_comp->cfg.compact);
321 if (ret) {
322 goto error;
323 }
324
325 /* With time? */
326 ret = configure_bool_opt(details_comp, params, with_time_param_name,
327 true, &details_comp->cfg.with_time);
328 if (ret) {
329 goto error;
330 }
331
55478183
PP
332 /* With trace name? */
333 ret = configure_bool_opt(details_comp, params,
334 with_trace_name_param_name,
335 true, &details_comp->cfg.with_trace_name);
336 if (ret) {
337 goto error;
338 }
339
340 /* With stream class name? */
341 ret = configure_bool_opt(details_comp, params,
342 with_stream_class_name_param_name,
343 true, &details_comp->cfg.with_stream_class_name);
344 if (ret) {
345 goto error;
346 }
347
348 /* With stream name? */
349 ret = configure_bool_opt(details_comp, params,
350 with_stream_name_param_name,
351 true, &details_comp->cfg.with_stream_name);
352 if (ret) {
353 goto error;
354 }
355
356 /* With UUID? */
357 ret = configure_bool_opt(details_comp, params,
358 with_uuid_param_name, true, &details_comp->cfg.with_uuid);
359 if (ret) {
360 goto error;
361 }
362
363 goto end;
364
365error:
366 ret = -1;
367
368end:
369 return ret;
370}
371
372static
373void log_configuration(bt_self_component_sink *comp,
374 struct details_comp *details_comp)
375{
d400b9e6 376 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
55478183
PP
377 bt_component_get_name(bt_self_component_as_component(
378 bt_self_component_sink_as_self_component(comp))));
d400b9e6
PP
379 BT_COMP_LOGI(" Colorize output: %d", details_comp->cfg.with_color);
380 BT_COMP_LOGI(" Compact: %d", details_comp->cfg.compact);
381 BT_COMP_LOGI(" With metadata: %d", details_comp->cfg.with_meta);
382 BT_COMP_LOGI(" With time: %d", details_comp->cfg.with_time);
d400b9e6
PP
383 BT_COMP_LOGI(" With trace name: %d", details_comp->cfg.with_trace_name);
384 BT_COMP_LOGI(" With stream class name: %d",
55478183 385 details_comp->cfg.with_stream_class_name);
d400b9e6
PP
386 BT_COMP_LOGI(" With stream name: %d", details_comp->cfg.with_stream_name);
387 BT_COMP_LOGI(" With UUID: %d", details_comp->cfg.with_uuid);
55478183
PP
388}
389
390BT_HIDDEN
59225a3e
SM
391bt_component_class_init_method_status details_init(
392 bt_self_component_sink *comp,
393 bt_self_component_sink_configuration *config,
55478183
PP
394 const bt_value *params,
395 __attribute__((unused)) void *init_method_data)
396{
d24d5663
PP
397 bt_component_class_init_method_status status =
398 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
399 bt_self_component_add_port_status add_port_status;
55478183
PP
400 struct details_comp *details_comp = NULL;
401
d24d5663
PP
402 add_port_status = bt_self_component_sink_add_input_port(comp,
403 in_port_name, NULL, NULL);
404 switch (add_port_status) {
405 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
406 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
407 break;
408 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
409 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
410 break;
411 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
412 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
413 break;
414 default:
415 abort();
55478183
PP
416 }
417
6c868a3a 418 details_comp = create_details_comp(comp);
55478183 419 if (!details_comp) {
d24d5663 420 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
55478183
PP
421 goto error;
422 }
423
424 if (configure_details_comp(details_comp, params)) {
d400b9e6 425 BT_COMP_LOGE_STR("Failed to configure component.");
55478183
PP
426 goto error;
427 }
428
429 log_configuration(comp, details_comp);
430 bt_self_component_set_data(
431 bt_self_component_sink_as_self_component(comp), details_comp);
432 goto end;
433
434error:
d24d5663
PP
435 if (status == BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
436 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
55478183
PP
437 }
438
439 destroy_details_comp(details_comp);
440
441end:
442 return status;
443}
444
445BT_HIDDEN
d24d5663
PP
446bt_component_class_sink_graph_is_configured_method_status
447details_graph_is_configured(bt_self_component_sink *comp)
55478183 448{
e803df70
SM
449 bt_component_class_sink_graph_is_configured_method_status status;
450 bt_self_component_port_input_message_iterator_create_from_sink_component_status
451 msg_iter_status;
55478183
PP
452 bt_self_component_port_input_message_iterator *iterator;
453 struct details_comp *details_comp;
454 bt_self_component_port_input *in_port;
455
456 details_comp = bt_self_component_get_data(
457 bt_self_component_sink_as_self_component(comp));
458 BT_ASSERT(details_comp);
459 in_port = bt_self_component_sink_borrow_input_port_by_name(comp,
460 in_port_name);
461 if (!bt_port_is_connected(bt_port_input_as_port_const(
462 bt_self_component_port_input_as_port_input(in_port)))) {
d400b9e6 463 BT_COMP_LOGE("Single input port is not connected: "
55478183 464 "port-name=\"%s\"", in_port_name);
d24d5663 465 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
55478183
PP
466 goto end;
467 }
468
e803df70 469 msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component(
ca02df0a 470 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
e803df70
SM
471 in_port_name), &iterator);
472 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
473 status = (int) msg_iter_status;
55478183
PP
474 goto end;
475 }
476
477 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
478 details_comp->msg_iter, iterator);
479
e803df70
SM
480 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
481
55478183
PP
482end:
483 return status;
484}
485
486BT_HIDDEN
d24d5663
PP
487bt_component_class_sink_consume_method_status
488details_consume(bt_self_component_sink *comp)
55478183 489{
d24d5663
PP
490 bt_component_class_sink_consume_method_status ret =
491 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
55478183
PP
492 bt_message_array_const msgs;
493 uint64_t count;
494 struct details_comp *details_comp;
d24d5663 495 bt_message_iterator_next_status next_status;
55478183
PP
496 uint64_t i;
497
498 details_comp = bt_self_component_get_data(
499 bt_self_component_sink_as_self_component(comp));
500 BT_ASSERT(details_comp);
501 BT_ASSERT(details_comp->msg_iter);
502
503 /* Consume messages */
d24d5663 504 next_status = bt_self_component_port_input_message_iterator_next(
55478183 505 details_comp->msg_iter, &msgs, &count);
d24d5663
PP
506 switch (next_status) {
507 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
508 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
55478183
PP
509
510 for (i = 0; i < count; i++) {
511 int print_ret = details_write_message(details_comp,
512 msgs[i]);
513
514 if (print_ret) {
515 for (; i < count; i++) {
516 /* Put all remaining messages */
517 bt_message_put_ref(msgs[i]);
518 }
519
d24d5663 520 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
55478183
PP
521 goto end;
522 }
523
524 /* Print output buffer to standard output and flush */
525 if (details_comp->str->len > 0) {
526 printf("%s", details_comp->str->str);
527 fflush(stdout);
528 details_comp->printed_something = true;
529 }
530
531 /* Put this message */
532 bt_message_put_ref(msgs[i]);
533 }
534
535 break;
d24d5663
PP
536 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
537 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
55478183 538 goto end;
d24d5663
PP
539 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
540 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
55478183 541 goto end;
d24d5663
PP
542 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
543 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
55478183 544 goto end;
d24d5663
PP
545 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
546 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
55478183
PP
547 goto end;
548 default:
549 abort();
550 }
551
552end:
553 return ret;
554}
This page took 0.055824 seconds and 4 git commands to generate.