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