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