sink.text.details: remove LOG_WRONG_PARAM_TYPE
[babeltrace.git] / src / plugins / text / details / details.c
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
23 #define BT_COMP_LOG_SELF_COMP (details_comp->self_comp)
24 #define BT_LOG_OUTPUT_LEVEL (details_comp->log_level)
25 #define BT_LOG_TAG "PLUGIN/SINK.TEXT.DETAILS"
26 #include "logging/comp-logging.h"
27
28 #include <stdbool.h>
29
30 #include <babeltrace2/babeltrace.h>
31
32 #include "common/common.h"
33 #include "common/assert.h"
34 #include "details.h"
35 #include "write.h"
36 #include "plugins/common/param-validation/param-validation.h"
37
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"
48
49 BT_HIDDEN
50 void 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
64 end:
65 return;
66 }
67
68 BT_HIDDEN
69 struct 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
88 end:
89 return details_tc_meta;
90 }
91
92 static
93 void 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)) {
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 }
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
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 }
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
156 BT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
157 details_comp->msg_iter);
158 g_free(details_comp);
159
160 end:
161 return;
162 }
163
164 static
165 struct details_comp *create_details_comp(
166 bt_self_component_sink *self_comp_sink)
167 {
168 struct details_comp *details_comp = g_new0(struct details_comp, 1);
169 bt_self_component *self_comp =
170 bt_self_component_sink_as_self_component(self_comp_sink);
171
172 if (!details_comp) {
173 goto error;
174 }
175
176 details_comp->log_level = bt_component_get_logging_level(
177 bt_self_component_as_component(self_comp));
178 details_comp->self_comp = self_comp;
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
199 error:
200 destroy_details_comp(details_comp);
201 details_comp = NULL;
202
203 end:
204 return details_comp;
205 }
206
207 BT_HIDDEN
208 void 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
219 static
220 void configure_bool_opt(struct details_comp *details_comp,
221 const bt_value *params, const char *param_name,
222 bool default_value, bool *opt_value)
223 {
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) {
229 *opt_value = (bool) bt_value_bool_get(value);
230 }
231 }
232
233 static const char *color_choices[] = { "never", "auto", "always", NULL };
234
235 static 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
250 static
251 bt_component_class_initialize_method_status configure_details_comp(
252 struct details_comp *details_comp,
253 const bt_value *params)
254 {
255 bt_component_class_initialize_method_status status;
256 const bt_value *value;
257 const char *str;
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 }
272
273 /* Colorize output? */
274 details_comp->cfg.with_color = bt_common_colors_supported();
275 value = bt_value_map_borrow_entry_value_const(params, COLOR_PARAM_NAME);
276 if (value) {
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();
284 } else {
285 BT_ASSERT(strcmp(str, "always") == 0);
286
287 details_comp->cfg.with_color = true;
288 }
289 }
290
291 /* With metadata objects? */
292 configure_bool_opt(details_comp, params, WITH_METADATA_PARAM_NAME,
293 true, &details_comp->cfg.with_meta);
294
295 /* With data objects? */
296 configure_bool_opt(details_comp, params, WITH_DATA_PARAM_NAME,
297 true, &details_comp->cfg.with_data);
298
299 /* Compact? */
300 configure_bool_opt(details_comp, params, COMPACT_PARAM_NAME,
301 false, &details_comp->cfg.compact);
302
303 /* With time? */
304 configure_bool_opt(details_comp, params, WITH_TIME_PARAM_NAME,
305 true, &details_comp->cfg.with_time);
306
307 /* With trace name? */
308 configure_bool_opt(details_comp, params,
309 WITH_TRACE_NAME_PARAM_NAME,
310 true, &details_comp->cfg.with_trace_name);
311
312 /* With stream class name? */
313 configure_bool_opt(details_comp, params,
314 WITH_STREAM_CLASS_NAME_PARAM_NAME,
315 true, &details_comp->cfg.with_stream_class_name);
316
317 /* With stream name? */
318 configure_bool_opt(details_comp, params,
319 WITH_STREAM_NAME_PARAM_NAME,
320 true, &details_comp->cfg.with_stream_name);
321
322 /* With UUID? */
323 configure_bool_opt(details_comp, params,
324 WITH_UUID_PARAM_NAME, true, &details_comp->cfg.with_uuid);
325
326 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
327 goto end;
328
329 end:
330 g_free(validate_error);
331
332 return status;
333 }
334
335 static
336 void log_configuration(bt_self_component_sink *comp,
337 struct details_comp *details_comp)
338 {
339 BT_COMP_LOGI("Configuration for `sink.text.details` component `%s`:",
340 bt_component_get_name(bt_self_component_as_component(
341 bt_self_component_sink_as_self_component(comp))));
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);
346 BT_COMP_LOGI(" With trace name: %d", details_comp->cfg.with_trace_name);
347 BT_COMP_LOGI(" With stream class name: %d",
348 details_comp->cfg.with_stream_class_name);
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);
351 }
352
353 BT_HIDDEN
354 bt_component_class_initialize_method_status details_init(
355 bt_self_component_sink *comp,
356 bt_self_component_sink_configuration *config,
357 const bt_value *params,
358 __attribute__((unused)) void *init_method_data)
359 {
360 bt_component_class_initialize_method_status status =
361 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
362 bt_self_component_add_port_status add_port_status;
363 struct details_comp *details_comp = NULL;
364
365 add_port_status = bt_self_component_sink_add_input_port(comp,
366 IN_PORT_NAME, NULL, NULL);
367 switch (add_port_status) {
368 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
369 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
370 break;
371 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
372 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
373 break;
374 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
375 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
376 break;
377 default:
378 bt_common_abort();
379 }
380
381 details_comp = create_details_comp(comp);
382 if (!details_comp) {
383 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
384 goto error;
385 }
386
387 if (configure_details_comp(details_comp, params)) {
388 BT_COMP_LOGE_STR("Failed to configure component.");
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
397 error:
398 if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
399 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
400 }
401
402 destroy_details_comp(details_comp);
403
404 end:
405 return status;
406 }
407
408 BT_HIDDEN
409 bt_component_class_sink_graph_is_configured_method_status
410 details_graph_is_configured(bt_self_component_sink *comp)
411 {
412 bt_component_class_sink_graph_is_configured_method_status status;
413 bt_message_iterator_create_from_sink_component_status
414 msg_iter_status;
415 bt_message_iterator *iterator;
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,
423 IN_PORT_NAME);
424 if (!bt_port_is_connected(bt_port_input_as_port_const(
425 bt_self_component_port_input_as_port_input(in_port)))) {
426 BT_COMP_LOGE("Single input port is not connected: "
427 "port-name=\"%s\"", IN_PORT_NAME);
428 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_ERROR;
429 goto end;
430 }
431
432 msg_iter_status = bt_message_iterator_create_from_sink_component(
433 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
434 IN_PORT_NAME), &iterator);
435 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
436 status = (int) msg_iter_status;
437 goto end;
438 }
439
440 BT_MESSAGE_ITERATOR_MOVE_REF(
441 details_comp->msg_iter, iterator);
442
443 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
444
445 end:
446 return status;
447 }
448
449 BT_HIDDEN
450 bt_component_class_sink_consume_method_status
451 details_consume(bt_self_component_sink *comp)
452 {
453 bt_component_class_sink_consume_method_status ret =
454 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
455 bt_message_array_const msgs;
456 uint64_t count;
457 struct details_comp *details_comp;
458 bt_message_iterator_next_status next_status;
459 uint64_t i;
460
461 details_comp = bt_self_component_get_data(
462 bt_self_component_sink_as_self_component(comp));
463 BT_ASSERT_DBG(details_comp);
464 BT_ASSERT_DBG(details_comp->msg_iter);
465
466 /* Consume messages */
467 next_status = bt_message_iterator_next(
468 details_comp->msg_iter, &msgs, &count);
469 switch (next_status) {
470 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
471 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
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
483 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
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;
499 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
500 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
501 goto end;
502 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
503 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
504 goto end;
505 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
506 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
507 goto end;
508 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
509 ret = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
510 goto end;
511 default:
512 bt_common_abort();
513 }
514
515 end:
516 return ret;
517 }
This page took 0.043609 seconds and 4 git commands to generate.