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