Move to kernel style SPDX license identifiers
[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);
d24d5663
PP
351 switch (add_port_status) {
352 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
21a9f056 353 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
d24d5663
PP
354 break;
355 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
21a9f056 356 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
d24d5663
PP
357 break;
358 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
21a9f056 359 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
d24d5663
PP
360 break;
361 default:
498e7994 362 bt_common_abort();
55478183
PP
363 }
364
6c868a3a 365 details_comp = create_details_comp(comp);
55478183 366 if (!details_comp) {
21a9f056 367 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
55478183
PP
368 goto error;
369 }
370
371 if (configure_details_comp(details_comp, params)) {
d400b9e6 372 BT_COMP_LOGE_STR("Failed to configure component.");
55478183
PP
373 goto error;
374 }
375
376 log_configuration(comp, details_comp);
377 bt_self_component_set_data(
378 bt_self_component_sink_as_self_component(comp), details_comp);
379 goto end;
380
381error:
21a9f056
FD
382 if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
383 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
55478183
PP
384 }
385
386 destroy_details_comp(details_comp);
387
388end:
389 return status;
390}
391
392BT_HIDDEN
d24d5663
PP
393bt_component_class_sink_graph_is_configured_method_status
394details_graph_is_configured(bt_self_component_sink *comp)
55478183 395{
e803df70 396 bt_component_class_sink_graph_is_configured_method_status status;
9a2c8b8e 397 bt_message_iterator_create_from_sink_component_status
e803df70 398 msg_iter_status;
9a2c8b8e 399 bt_message_iterator *iterator;
55478183
PP
400 struct details_comp *details_comp;
401 bt_self_component_port_input *in_port;
402
403 details_comp = bt_self_component_get_data(
404 bt_self_component_sink_as_self_component(comp));
405 BT_ASSERT(details_comp);
406 in_port = bt_self_component_sink_borrow_input_port_by_name(comp,
48a0e52c 407 IN_PORT_NAME);
55478183
PP
408 if (!bt_port_is_connected(bt_port_input_as_port_const(
409 bt_self_component_port_input_as_port_input(in_port)))) {
d400b9e6 410 BT_COMP_LOGE("Single input port is not connected: "
48a0e52c 411 "port-name=\"%s\"", IN_PORT_NAME);
d24d5663 412 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
55478183
PP
413 goto end;
414 }
415
9a2c8b8e 416 msg_iter_status = bt_message_iterator_create_from_sink_component(
ca02df0a 417 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
48a0e52c 418 IN_PORT_NAME), &iterator);
9a2c8b8e 419 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
e803df70 420 status = (int) msg_iter_status;
55478183
PP
421 goto end;
422 }
423
9a2c8b8e 424 BT_MESSAGE_ITERATOR_MOVE_REF(
55478183
PP
425 details_comp->msg_iter, iterator);
426
e803df70
SM
427 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
428
55478183
PP
429end:
430 return status;
431}
432
433BT_HIDDEN
d24d5663
PP
434bt_component_class_sink_consume_method_status
435details_consume(bt_self_component_sink *comp)
55478183 436{
d24d5663
PP
437 bt_component_class_sink_consume_method_status ret =
438 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
55478183
PP
439 bt_message_array_const msgs;
440 uint64_t count;
441 struct details_comp *details_comp;
d24d5663 442 bt_message_iterator_next_status next_status;
55478183
PP
443 uint64_t i;
444
445 details_comp = bt_self_component_get_data(
446 bt_self_component_sink_as_self_component(comp));
98b15851
PP
447 BT_ASSERT_DBG(details_comp);
448 BT_ASSERT_DBG(details_comp->msg_iter);
55478183
PP
449
450 /* Consume messages */
9a2c8b8e 451 next_status = bt_message_iterator_next(
55478183 452 details_comp->msg_iter, &msgs, &count);
d24d5663
PP
453 switch (next_status) {
454 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
455 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
55478183
PP
456
457 for (i = 0; i < count; i++) {
458 int print_ret = details_write_message(details_comp,
459 msgs[i]);
460
461 if (print_ret) {
462 for (; i < count; i++) {
463 /* Put all remaining messages */
464 bt_message_put_ref(msgs[i]);
465 }
466
d24d5663 467 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
55478183
PP
468 goto end;
469 }
470
471 /* Print output buffer to standard output and flush */
472 if (details_comp->str->len > 0) {
473 printf("%s", details_comp->str->str);
474 fflush(stdout);
475 details_comp->printed_something = true;
476 }
477
478 /* Put this message */
479 bt_message_put_ref(msgs[i]);
480 }
481
482 break;
d24d5663
PP
483 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
484 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
55478183 485 goto end;
d24d5663
PP
486 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
487 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
55478183 488 goto end;
d24d5663
PP
489 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
490 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
55478183 491 goto end;
d24d5663
PP
492 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
493 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
55478183
PP
494 goto end;
495 default:
498e7994 496 bt_common_abort();
55478183
PP
497 }
498
499end:
500 return ret;
501}
This page took 0.067292 seconds and 4 git commands to generate.