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