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