sink.text.details: replace switch to convert status with cast
[babeltrace.git] / src / plugins / text / details / details.c
CommitLineData
55478183 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
55478183 3 *
0235b0db 4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
55478183
PP
5 */
6
d400b9e6 7#define BT_COMP_LOG_SELF_COMP (details_comp->self_comp)
6c868a3a 8#define BT_LOG_OUTPUT_LEVEL (details_comp->log_level)
350ad6c1 9#define BT_LOG_TAG "PLUGIN/SINK.TEXT.DETAILS"
d9c39b0a 10#include "logging/comp-logging.h"
55478183 11
c4f23e30
FD
12#include <stdbool.h>
13
55478183
PP
14#include <babeltrace2/babeltrace.h>
15
16#include "common/common.h"
17#include "common/assert.h"
18#include "details.h"
19#include "write.h"
48a0e52c 20#include "plugins/common/param-validation/param-validation.h"
55478183 21
48a0e52c
SM
22#define IN_PORT_NAME "in"
23#define COLOR_PARAM_NAME "color"
24#define WITH_METADATA_PARAM_NAME "with-metadata"
25#define WITH_DATA_PARAM_NAME "with-data"
26#define WITH_TIME_PARAM_NAME "with-time"
27#define WITH_TRACE_NAME_PARAM_NAME "with-trace-name"
28#define WITH_STREAM_CLASS_NAME_PARAM_NAME "with-stream-class-name"
29#define WITH_STREAM_NAME_PARAM_NAME "with-stream-name"
30#define WITH_UUID_PARAM_NAME "with-uuid"
31#define COMPACT_PARAM_NAME "compact"
55478183
PP
32
33BT_HIDDEN
34void details_destroy_details_trace_class_meta(
35 struct details_trace_class_meta *details_tc_meta)
36{
37 if (!details_tc_meta) {
38 goto end;
39 }
40
41 if (details_tc_meta->objects) {
42 g_hash_table_destroy(details_tc_meta->objects);
43 details_tc_meta->objects = NULL;
44 }
45
46 g_free(details_tc_meta);
47
48end:
49 return;
50}
51
52BT_HIDDEN
53struct details_trace_class_meta *details_create_details_trace_class_meta(void)
54{
55 struct details_trace_class_meta *details_tc_meta =
56 g_new0(struct details_trace_class_meta, 1);
57
58 if (!details_tc_meta) {
59 goto end;
60 }
61
62 details_tc_meta->objects = g_hash_table_new(
63 g_direct_hash, g_direct_equal);
64 if (!details_tc_meta->objects) {
65 details_destroy_details_trace_class_meta(details_tc_meta);
66 details_tc_meta = NULL;
67 goto end;
68 }
69
70 details_tc_meta->tc_destruction_listener_id = UINT64_C(-1);
71
72end:
73 return details_tc_meta;
74}
75
76static
77void destroy_details_comp(struct details_comp *details_comp)
78{
79 GHashTableIter iter;
80 gpointer key, value;
81
82 if (!details_comp) {
83 goto end;
84 }
85
86 if (details_comp->meta) {
87 /*
88 * Remove trace class destruction listeners, because
89 * otherwise, when they are called, `details_comp`
90 * (their user data) won't exist anymore (we're
91 * destroying it here).
92 */
93 g_hash_table_iter_init(&iter, details_comp->meta);
94
95 while (g_hash_table_iter_next(&iter, &key, &value)) {
96 struct details_trace_class_meta *details_tc_meta =
97 value;
98
99 if (details_tc_meta->tc_destruction_listener_id !=
100 UINT64_C(-1)) {
b80991f6
PP
101 if (bt_trace_class_remove_destruction_listener(
102 (const void *) key,
103 details_tc_meta->tc_destruction_listener_id)) {
104 bt_current_thread_clear_error();
105 }
55478183
PP
106 }
107 }
108
109 g_hash_table_destroy(details_comp->meta);
110 details_comp->meta = NULL;
111 }
112
113 if (details_comp->traces) {
114 /*
115 * Remove trace destruction listeners, because
116 * otherwise, when they are called, `details_comp` won't
117 * exist anymore (we're destroying it here).
118 */
119 g_hash_table_iter_init(&iter, details_comp->traces);
120
121 while (g_hash_table_iter_next(&iter, &key, &value)) {
122 struct details_trace *details_trace = value;
123
b80991f6
PP
124 if (bt_trace_remove_destruction_listener(
125 (const void *) key,
126 details_trace->trace_destruction_listener_id)) {
127 bt_current_thread_clear_error();
128 }
55478183
PP
129 }
130
131 g_hash_table_destroy(details_comp->traces);
132 details_comp->traces = NULL;
133 }
134
135 if (details_comp->str) {
136 g_string_free(details_comp->str, TRUE);
137 details_comp->str = NULL;
138 }
139
9a2c8b8e 140 BT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
55478183
PP
141 details_comp->msg_iter);
142 g_free(details_comp);
143
144end:
145 return;
146}
147
148static
6c868a3a
PP
149struct details_comp *create_details_comp(
150 bt_self_component_sink *self_comp_sink)
55478183
PP
151{
152 struct details_comp *details_comp = g_new0(struct details_comp, 1);
6c868a3a
PP
153 bt_self_component *self_comp =
154 bt_self_component_sink_as_self_component(self_comp_sink);
55478183
PP
155
156 if (!details_comp) {
157 goto error;
158 }
159
6c868a3a
PP
160 details_comp->log_level = bt_component_get_logging_level(
161 bt_self_component_as_component(self_comp));
d400b9e6 162 details_comp->self_comp = self_comp;
55478183
PP
163 details_comp->meta = g_hash_table_new_full(g_direct_hash,
164 g_direct_equal, NULL,
165 (GDestroyNotify) details_destroy_details_trace_class_meta);
166 if (!details_comp->meta) {
167 goto error;
168 }
169
170 details_comp->traces = g_hash_table_new_full(g_direct_hash,
171 g_direct_equal, NULL, g_free);
172 if (!details_comp->traces) {
173 goto error;
174 }
175
176 details_comp->str = g_string_new(NULL);
177 if (!details_comp->str) {
178 goto error;
179 }
180
181 goto end;
182
183error:
184 destroy_details_comp(details_comp);
185 details_comp = NULL;
186
187end:
188 return details_comp;
189}
190
191BT_HIDDEN
192void details_finalize(bt_self_component_sink *comp)
193{
194 struct details_comp *details_comp;
195
196 BT_ASSERT(comp);
197 details_comp = bt_self_component_get_data(
198 bt_self_component_sink_as_self_component(comp));
199 BT_ASSERT(details_comp);
200 destroy_details_comp(details_comp);
201}
202
203static
48a0e52c 204void configure_bool_opt(struct details_comp *details_comp,
55478183
PP
205 const bt_value *params, const char *param_name,
206 bool default_value, bool *opt_value)
207{
55478183
PP
208 const bt_value *value;
209
210 *opt_value = default_value;
211 value = bt_value_map_borrow_entry_value_const(params, param_name);
212 if (value) {
55478183
PP
213 *opt_value = (bool) bt_value_bool_get(value);
214 }
55478183
PP
215}
216
48a0e52c
SM
217static const char *color_choices[] = { "never", "auto", "always", NULL };
218
219static const struct bt_param_validation_map_value_entry_descr details_params[] = {
220 { COLOR_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = {
221 .choices = color_choices,
222 } } },
223 { WITH_METADATA_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
224 { WITH_DATA_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
225 { COMPACT_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
226 { WITH_TIME_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
227 { WITH_TRACE_NAME_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
228 { WITH_STREAM_CLASS_NAME_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
229 { WITH_STREAM_NAME_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
230 { WITH_UUID_PARAM_NAME, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
231 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
232};
233
55478183 234static
48a0e52c
SM
235bt_component_class_initialize_method_status configure_details_comp(
236 struct details_comp *details_comp,
55478183
PP
237 const bt_value *params)
238{
48a0e52c 239 bt_component_class_initialize_method_status status;
55478183
PP
240 const bt_value *value;
241 const char *str;
48a0e52c
SM
242 enum bt_param_validation_status validation_status;
243 gchar *validate_error = NULL;
244
245 validation_status = bt_param_validation_validate(params,
246 details_params, &validate_error);
247 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
248 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
249 goto end;
250 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
251 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
252 BT_COMP_LOGE_APPEND_CAUSE(details_comp->self_comp,
253 "%s", validate_error);
254 goto end;
255 }
55478183
PP
256
257 /* Colorize output? */
258 details_comp->cfg.with_color = bt_common_colors_supported();
48a0e52c 259 value = bt_value_map_borrow_entry_value_const(params, COLOR_PARAM_NAME);
55478183 260 if (value) {
55478183
PP
261 str = bt_value_string_get(value);
262
263 if (strcmp(str, "never") == 0) {
264 details_comp->cfg.with_color = false;
265 } else if (strcmp(str, "auto") == 0) {
266 details_comp->cfg.with_color =
267 bt_common_colors_supported();
55478183 268 } else {
48a0e52c
SM
269 BT_ASSERT(strcmp(str, "always") == 0);
270
271 details_comp->cfg.with_color = true;
55478183
PP
272 }
273 }
274
275 /* With metadata objects? */
48a0e52c 276 configure_bool_opt(details_comp, params, WITH_METADATA_PARAM_NAME,
55478183 277 true, &details_comp->cfg.with_meta);
55478183 278
cc413248 279 /* With data objects? */
48a0e52c 280 configure_bool_opt(details_comp, params, WITH_DATA_PARAM_NAME,
cc413248 281 true, &details_comp->cfg.with_data);
cc413248 282
55478183 283 /* Compact? */
48a0e52c 284 configure_bool_opt(details_comp, params, COMPACT_PARAM_NAME,
55478183 285 false, &details_comp->cfg.compact);
55478183
PP
286
287 /* With time? */
48a0e52c 288 configure_bool_opt(details_comp, params, WITH_TIME_PARAM_NAME,
55478183 289 true, &details_comp->cfg.with_time);
55478183 290
55478183 291 /* With trace name? */
48a0e52c
SM
292 configure_bool_opt(details_comp, params,
293 WITH_TRACE_NAME_PARAM_NAME,
55478183 294 true, &details_comp->cfg.with_trace_name);
55478183
PP
295
296 /* With stream class name? */
48a0e52c
SM
297 configure_bool_opt(details_comp, params,
298 WITH_STREAM_CLASS_NAME_PARAM_NAME,
55478183 299 true, &details_comp->cfg.with_stream_class_name);
55478183
PP
300
301 /* With stream name? */
48a0e52c
SM
302 configure_bool_opt(details_comp, params,
303 WITH_STREAM_NAME_PARAM_NAME,
55478183 304 true, &details_comp->cfg.with_stream_name);
55478183
PP
305
306 /* With UUID? */
48a0e52c
SM
307 configure_bool_opt(details_comp, params,
308 WITH_UUID_PARAM_NAME, true, &details_comp->cfg.with_uuid);
55478183 309
48a0e52c 310 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
55478183
PP
311 goto end;
312
55478183 313end:
48a0e52c
SM
314 g_free(validate_error);
315
316 return status;
55478183
PP
317}
318
319static
320void log_configuration(bt_self_component_sink *comp,
321 struct details_comp *details_comp)
322{
d400b9e6 323 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
55478183
PP
324 bt_component_get_name(bt_self_component_as_component(
325 bt_self_component_sink_as_self_component(comp))));
d400b9e6
PP
326 BT_COMP_LOGI(" Colorize output: %d", details_comp->cfg.with_color);
327 BT_COMP_LOGI(" Compact: %d", details_comp->cfg.compact);
328 BT_COMP_LOGI(" With metadata: %d", details_comp->cfg.with_meta);
329 BT_COMP_LOGI(" With time: %d", details_comp->cfg.with_time);
d400b9e6
PP
330 BT_COMP_LOGI(" With trace name: %d", details_comp->cfg.with_trace_name);
331 BT_COMP_LOGI(" With stream class name: %d",
55478183 332 details_comp->cfg.with_stream_class_name);
d400b9e6
PP
333 BT_COMP_LOGI(" With stream name: %d", details_comp->cfg.with_stream_name);
334 BT_COMP_LOGI(" With UUID: %d", details_comp->cfg.with_uuid);
55478183
PP
335}
336
337BT_HIDDEN
21a9f056 338bt_component_class_initialize_method_status details_init(
59225a3e
SM
339 bt_self_component_sink *comp,
340 bt_self_component_sink_configuration *config,
55478183
PP
341 const bt_value *params,
342 __attribute__((unused)) void *init_method_data)
343{
21a9f056
FD
344 bt_component_class_initialize_method_status status =
345 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
d24d5663 346 bt_self_component_add_port_status add_port_status;
55478183
PP
347 struct details_comp *details_comp = NULL;
348
d24d5663 349 add_port_status = bt_self_component_sink_add_input_port(comp,
48a0e52c 350 IN_PORT_NAME, NULL, NULL);
c7f21c12
SM
351 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
352 status = (int) add_port_status;
353 goto error;
55478183
PP
354 }
355
6c868a3a 356 details_comp = create_details_comp(comp);
55478183 357 if (!details_comp) {
21a9f056 358 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
55478183
PP
359 goto error;
360 }
361
362 if (configure_details_comp(details_comp, params)) {
d400b9e6 363 BT_COMP_LOGE_STR("Failed to configure component.");
55478183
PP
364 goto error;
365 }
366
367 log_configuration(comp, details_comp);
368 bt_self_component_set_data(
369 bt_self_component_sink_as_self_component(comp), details_comp);
370 goto end;
371
372error:
21a9f056
FD
373 if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
374 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
55478183
PP
375 }
376
377 destroy_details_comp(details_comp);
378
379end:
380 return status;
381}
382
383BT_HIDDEN
d24d5663
PP
384bt_component_class_sink_graph_is_configured_method_status
385details_graph_is_configured(bt_self_component_sink *comp)
55478183 386{
e803df70 387 bt_component_class_sink_graph_is_configured_method_status status;
9a2c8b8e 388 bt_message_iterator_create_from_sink_component_status
e803df70 389 msg_iter_status;
9a2c8b8e 390 bt_message_iterator *iterator;
55478183
PP
391 struct details_comp *details_comp;
392 bt_self_component_port_input *in_port;
393
394 details_comp = bt_self_component_get_data(
395 bt_self_component_sink_as_self_component(comp));
396 BT_ASSERT(details_comp);
397 in_port = bt_self_component_sink_borrow_input_port_by_name(comp,
48a0e52c 398 IN_PORT_NAME);
55478183
PP
399 if (!bt_port_is_connected(bt_port_input_as_port_const(
400 bt_self_component_port_input_as_port_input(in_port)))) {
d400b9e6 401 BT_COMP_LOGE("Single input port is not connected: "
48a0e52c 402 "port-name=\"%s\"", IN_PORT_NAME);
d24d5663 403 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
55478183
PP
404 goto end;
405 }
406
9a2c8b8e 407 msg_iter_status = bt_message_iterator_create_from_sink_component(
ca02df0a 408 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
48a0e52c 409 IN_PORT_NAME), &iterator);
9a2c8b8e 410 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
e803df70 411 status = (int) msg_iter_status;
55478183
PP
412 goto end;
413 }
414
9a2c8b8e 415 BT_MESSAGE_ITERATOR_MOVE_REF(
55478183
PP
416 details_comp->msg_iter, iterator);
417
e803df70
SM
418 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
419
55478183
PP
420end:
421 return status;
422}
423
424BT_HIDDEN
d24d5663
PP
425bt_component_class_sink_consume_method_status
426details_consume(bt_self_component_sink *comp)
55478183 427{
818919b9 428 bt_component_class_sink_consume_method_status status;
55478183
PP
429 bt_message_array_const msgs;
430 uint64_t count;
431 struct details_comp *details_comp;
d24d5663 432 bt_message_iterator_next_status next_status;
55478183
PP
433 uint64_t i;
434
435 details_comp = bt_self_component_get_data(
436 bt_self_component_sink_as_self_component(comp));
98b15851
PP
437 BT_ASSERT_DBG(details_comp);
438 BT_ASSERT_DBG(details_comp->msg_iter);
55478183
PP
439
440 /* Consume messages */
9a2c8b8e 441 next_status = bt_message_iterator_next(
55478183 442 details_comp->msg_iter, &msgs, &count);
818919b9
SM
443 if (next_status != BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) {
444 status = (int) next_status;
445 goto end;
446 }
55478183 447
818919b9
SM
448 for (i = 0; i < count; i++) {
449 int print_ret = details_write_message(details_comp,
450 msgs[i]);
55478183 451
818919b9
SM
452 if (print_ret) {
453 for (; i < count; i++) {
454 /* Put all remaining messages */
455 bt_message_put_ref(msgs[i]);
55478183
PP
456 }
457
818919b9
SM
458 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
459 goto end;
55478183
PP
460 }
461
818919b9
SM
462 /* Print output buffer to standard output and flush */
463 if (details_comp->str->len > 0) {
464 printf("%s", details_comp->str->str);
465 fflush(stdout);
466 details_comp->printed_something = true;
467 }
468
469 /* Put this message */
470 bt_message_put_ref(msgs[i]);
55478183
PP
471 }
472
818919b9
SM
473 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
474
55478183 475end:
818919b9 476 return status;
55478183 477}
This page took 0.072232 seconds and 4 git commands to generate.