configure: enable -Wshadow-field
[babeltrace.git] / src / plugins / text / details / details.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_COMP_LOG_SELF_COMP (details_comp->self_comp)
8 #define BT_LOG_OUTPUT_LEVEL (details_comp->log_level)
9 #define BT_LOG_TAG "PLUGIN/SINK.TEXT.DETAILS"
10 #include "logging/comp-logging.h"
11
12 #include <stdbool.h>
13
14 #include <babeltrace2/babeltrace.h>
15
16 #include "common/common.h"
17 #include "common/assert.h"
18 #include "details.h"
19 #include "write.h"
20 #include "plugins/common/param-validation/param-validation.h"
21
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"
32
33 BT_HIDDEN
34 void 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
48 end:
49 return;
50 }
51
52 BT_HIDDEN
53 struct 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
72 end:
73 return details_tc_meta;
74 }
75
76 static
77 void 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)) {
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 }
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
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 }
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
140 BT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
141 details_comp->msg_iter);
142 g_free(details_comp);
143
144 end:
145 return;
146 }
147
148 static
149 struct details_comp *create_details_comp(
150 bt_self_component_sink *self_comp_sink)
151 {
152 struct details_comp *details_comp = g_new0(struct details_comp, 1);
153 bt_self_component *self_comp =
154 bt_self_component_sink_as_self_component(self_comp_sink);
155
156 if (!details_comp) {
157 goto error;
158 }
159
160 details_comp->log_level = bt_component_get_logging_level(
161 bt_self_component_as_component(self_comp));
162 details_comp->self_comp = self_comp;
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
183 error:
184 destroy_details_comp(details_comp);
185 details_comp = NULL;
186
187 end:
188 return details_comp;
189 }
190
191 BT_HIDDEN
192 void 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
203 static
204 void configure_bool_opt(struct details_comp *details_comp,
205 const bt_value *params, const char *param_name,
206 bool default_value, bool *opt_value)
207 {
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) {
213 *opt_value = (bool) bt_value_bool_get(value);
214 }
215 }
216
217 static const char *color_choices[] = { "never", "auto", "always", NULL };
218
219 static 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
234 static
235 bt_component_class_initialize_method_status configure_details_comp(
236 struct details_comp *details_comp,
237 const bt_value *params)
238 {
239 bt_component_class_initialize_method_status status;
240 const bt_value *value;
241 const char *str;
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 }
256
257 /* Colorize output? */
258 details_comp->cfg.with_color = bt_common_colors_supported();
259 value = bt_value_map_borrow_entry_value_const(params, COLOR_PARAM_NAME);
260 if (value) {
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();
268 } else {
269 BT_ASSERT(strcmp(str, "always") == 0);
270
271 details_comp->cfg.with_color = true;
272 }
273 }
274
275 /* With metadata objects? */
276 configure_bool_opt(details_comp, params, WITH_METADATA_PARAM_NAME,
277 true, &details_comp->cfg.with_meta);
278
279 /* With data objects? */
280 configure_bool_opt(details_comp, params, WITH_DATA_PARAM_NAME,
281 true, &details_comp->cfg.with_data);
282
283 /* Compact? */
284 configure_bool_opt(details_comp, params, COMPACT_PARAM_NAME,
285 false, &details_comp->cfg.compact);
286
287 /* With time? */
288 configure_bool_opt(details_comp, params, WITH_TIME_PARAM_NAME,
289 true, &details_comp->cfg.with_time);
290
291 /* With trace name? */
292 configure_bool_opt(details_comp, params,
293 WITH_TRACE_NAME_PARAM_NAME,
294 true, &details_comp->cfg.with_trace_name);
295
296 /* With stream class name? */
297 configure_bool_opt(details_comp, params,
298 WITH_STREAM_CLASS_NAME_PARAM_NAME,
299 true, &details_comp->cfg.with_stream_class_name);
300
301 /* With stream name? */
302 configure_bool_opt(details_comp, params,
303 WITH_STREAM_NAME_PARAM_NAME,
304 true, &details_comp->cfg.with_stream_name);
305
306 /* With UUID? */
307 configure_bool_opt(details_comp, params,
308 WITH_UUID_PARAM_NAME, true, &details_comp->cfg.with_uuid);
309
310 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
311 goto end;
312
313 end:
314 g_free(validate_error);
315
316 return status;
317 }
318
319 static
320 void log_configuration(bt_self_component_sink *comp,
321 struct details_comp *details_comp)
322 {
323 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
324 bt_component_get_name(bt_self_component_as_component(
325 bt_self_component_sink_as_self_component(comp))));
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);
330 BT_COMP_LOGI(" With trace name: %d", details_comp->cfg.with_trace_name);
331 BT_COMP_LOGI(" With stream class name: %d",
332 details_comp->cfg.with_stream_class_name);
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);
335 }
336
337 BT_HIDDEN
338 bt_component_class_initialize_method_status details_init(
339 bt_self_component_sink *comp,
340 bt_self_component_sink_configuration *config,
341 const bt_value *params,
342 __attribute__((unused)) void *init_method_data)
343 {
344 bt_component_class_initialize_method_status status;
345 bt_self_component_add_port_status add_port_status;
346 struct details_comp *details_comp;
347 bt_self_component *self_comp =
348 bt_self_component_sink_as_self_component(comp);
349 bt_logging_level log_level =
350 bt_component_get_logging_level(
351 bt_self_component_as_component(self_comp));
352
353 details_comp = create_details_comp(comp);
354 if (!details_comp) {
355 /*
356 * Don't use BT_COMP_LOGE_APPEND_CAUSE, as `details_comp` is not
357 * initialized yet.
358 */
359 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
360 "Failed to allocate component.");
361 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
362 self_comp, "Failed to allocate component.");
363 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
364 goto error;
365 }
366
367 add_port_status = bt_self_component_sink_add_input_port(comp,
368 IN_PORT_NAME, NULL, NULL);
369 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
370 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to add input port.");
371 status = (int) add_port_status;
372 goto error;
373 }
374
375 status = configure_details_comp(details_comp, params);
376 if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
377 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to configure component.");
378 goto error;
379 }
380
381 log_configuration(comp, details_comp);
382 bt_self_component_set_data(
383 bt_self_component_sink_as_self_component(comp), details_comp);
384 goto end;
385
386 error:
387 destroy_details_comp(details_comp);
388
389 end:
390 return status;
391 }
392
393 BT_HIDDEN
394 bt_component_class_sink_graph_is_configured_method_status
395 details_graph_is_configured(bt_self_component_sink *comp)
396 {
397 bt_component_class_sink_graph_is_configured_method_status status;
398 bt_message_iterator_create_from_sink_component_status
399 msg_iter_status;
400 bt_message_iterator *iterator;
401 bt_self_component_port_input *in_port;
402 bt_self_component *self_comp =
403 bt_self_component_sink_as_self_component(comp);
404 struct details_comp *details_comp = bt_self_component_get_data(self_comp);
405
406 BT_ASSERT(details_comp);
407
408 in_port = bt_self_component_sink_borrow_input_port_by_name(comp,
409 IN_PORT_NAME);
410 if (!bt_port_is_connected(bt_port_input_as_port_const(
411 bt_self_component_port_input_as_port_input(in_port)))) {
412 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Single input port is not connected: "
413 "port-name=\"%s\"", IN_PORT_NAME);
414 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
415 goto end;
416 }
417
418 msg_iter_status = bt_message_iterator_create_from_sink_component(
419 comp, in_port, &iterator);
420 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
421 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create message iterator: "
422 "port-name=\"%s\"", IN_PORT_NAME);
423 status = (int) msg_iter_status;
424 goto end;
425 }
426
427 BT_MESSAGE_ITERATOR_MOVE_REF(
428 details_comp->msg_iter, iterator);
429
430 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
431
432 end:
433 return status;
434 }
435
436 BT_HIDDEN
437 bt_component_class_sink_consume_method_status
438 details_consume(bt_self_component_sink *comp)
439 {
440 bt_component_class_sink_consume_method_status status;
441 bt_message_array_const msgs;
442 uint64_t count;
443 bt_message_iterator_next_status next_status;
444 uint64_t i;
445 bt_self_component *self_comp = bt_self_component_sink_as_self_component(comp);
446 struct details_comp *details_comp = bt_self_component_get_data(self_comp);
447
448 BT_ASSERT_DBG(details_comp);
449 BT_ASSERT_DBG(details_comp->msg_iter);
450
451 /* Consume messages */
452 next_status = bt_message_iterator_next(
453 details_comp->msg_iter, &msgs, &count);
454 if (next_status != BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) {
455 status = (int) next_status;
456 goto end;
457 }
458
459 for (i = 0; i < count; i++) {
460 int print_ret = details_write_message(details_comp,
461 msgs[i]);
462
463 if (print_ret) {
464 for (; i < count; i++) {
465 /* Put all remaining messages */
466 bt_message_put_ref(msgs[i]);
467 }
468
469 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to write message.");
470 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
471 goto end;
472 }
473
474 /* Print output buffer to standard output and flush */
475 if (details_comp->str->len > 0) {
476 printf("%s", details_comp->str->str);
477 fflush(stdout);
478 details_comp->printed_something = true;
479 }
480
481 /* Put this message */
482 bt_message_put_ref(msgs[i]);
483 }
484
485 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
486
487 end:
488 return status;
489 }
This page took 0.04136 seconds and 4 git commands to generate.