lib: force user to include `<babeltrace2/babeltrace.h>`
[babeltrace.git] / src / lib / lib-logging.c
1 /*
2 * Copyright 2018 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_LOG_TAG "LIB/LIB-LOGGING"
24 #include "lib/logging.h"
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <inttypes.h>
31 #include <stdint.h>
32 #include <wchar.h>
33 #include <glib.h>
34 #include "common/common.h"
35 #include "common/uuid.h"
36 #include <babeltrace2/babeltrace.h>
37
38 #include "logging.h"
39 #include "assert-pre.h"
40 #include "assert-post.h"
41 #include "value.h"
42 #include "object-pool.h"
43 #include "graph/component-class.h"
44 #include "graph/component-class-sink-colander.h"
45 #include "graph/component-filter.h"
46 #include "graph/component.h"
47 #include "graph/component-sink.h"
48 #include "graph/component-source.h"
49 #include "graph/connection.h"
50 #include "graph/graph.h"
51 #include "graph/message/discarded-items.h"
52 #include "graph/message/event.h"
53 #include "graph/message/iterator.h"
54 #include "graph/message/message.h"
55 #include "graph/message/message-iterator-inactivity.h"
56 #include "graph/message/packet.h"
57 #include "graph/message/stream.h"
58 #include "graph/port.h"
59 #include "plugin/plugin.h"
60 #include "plugin/plugin-so.h"
61 #include "trace-ir/clock-class.h"
62 #include "trace-ir/clock-snapshot.h"
63 #include "trace-ir/event-class.h"
64 #include "trace-ir/event.h"
65 #include "trace-ir/field-class.h"
66 #include "trace-ir/field.h"
67 #include "trace-ir/field-path.h"
68 #include "trace-ir/packet.h"
69 #include "trace-ir/stream-class.h"
70 #include "trace-ir/stream.h"
71 #include "trace-ir/trace-class.h"
72 #include "trace-ir/trace.h"
73 #include "trace-ir/utils.h"
74 #include "error.h"
75 #include "assert-pre.h"
76
77 #define LIB_LOGGING_BUF_SIZE (4096 * 4)
78
79 static __thread char lib_logging_buf[LIB_LOGGING_BUF_SIZE];
80
81 #define BUF_APPEND(_fmt, ...) \
82 do { \
83 int _count; \
84 size_t _size = LIB_LOGGING_BUF_SIZE - \
85 (size_t) (*buf_ch - lib_logging_buf); \
86 _count = snprintf(*buf_ch, _size, (_fmt), __VA_ARGS__); \
87 BT_ASSERT(_count >= 0); \
88 *buf_ch += MIN(_count, _size); \
89 if (*buf_ch >= lib_logging_buf + LIB_LOGGING_BUF_SIZE - 1) { \
90 return; \
91 } \
92 } while (0)
93
94 #define BUF_APPEND_UUID(_uuid) \
95 do { \
96 BUF_APPEND(", %suuid=", prefix); \
97 format_uuid(buf_ch, (_uuid)); \
98 } while (0)
99
100 #define PRFIELD(_expr) prefix, (_expr)
101
102 #define PRFIELD_GSTRING(_expr) PRFIELD((_expr) ? (_expr)->str : NULL)
103
104 #define TMP_PREFIX_LEN 128
105 #define SET_TMP_PREFIX(_prefix2) \
106 do { \
107 int snprintf_ret = \
108 snprintf(tmp_prefix, TMP_PREFIX_LEN - 1, "%s%s", \
109 prefix, (_prefix2)); \
110 \
111 if (snprintf_ret < 0 || snprintf_ret >= TMP_PREFIX_LEN - 1) { \
112 abort(); \
113 } \
114 \
115 tmp_prefix[TMP_PREFIX_LEN - 1] = '\0'; \
116 } while (0)
117
118 static inline void format_component(char **buf_ch, bool extended,
119 const char *prefix, const struct bt_component *component);
120
121 static inline void format_port(char **buf_ch, bool extended,
122 const char *prefix, const struct bt_port *port);
123
124 static inline void format_connection(char **buf_ch, bool extended,
125 const char *prefix, const struct bt_connection *connection);
126
127 static inline void format_clock_snapshot(char **buf_ch, bool extended,
128 const char *prefix, const struct bt_clock_snapshot *clock_snapshot);
129
130 static inline void format_field_path(char **buf_ch, bool extended,
131 const char *prefix, const struct bt_field_path *field_path);
132
133 static inline void format_object(char **buf_ch, bool extended,
134 const char *prefix, const struct bt_object *obj)
135 {
136 BUF_APPEND(", %sref-count=%llu", prefix, obj->ref_count);
137 }
138
139 static inline void format_uuid(char **buf_ch, bt_uuid uuid)
140 {
141 BUF_APPEND("\"" BT_UUID_FMT "\"", BT_UUID_FMT_VALUES(uuid));
142 }
143
144 static inline void format_object_pool(char **buf_ch, bool extended,
145 const char *prefix, const struct bt_object_pool *pool)
146 {
147 BUF_APPEND(", %ssize=%zu", PRFIELD(pool->size));
148
149 if (pool->objects) {
150 BUF_APPEND(", %scap=%u", PRFIELD(pool->objects->len));
151 }
152 }
153
154 static inline void format_integer_field_class(char **buf_ch,
155 bool extended, const char *prefix,
156 const struct bt_field_class *field_class)
157 {
158 const struct bt_field_class_integer *int_fc =
159 (const void *) field_class;
160
161 BUF_APPEND(", %srange-size=%" PRIu64 ", %sbase=%s",
162 PRFIELD(int_fc->range),
163 PRFIELD(bt_common_field_class_integer_preferred_display_base_string(int_fc->base)));
164 }
165
166 static inline void format_array_field_class(char **buf_ch,
167 bool extended, const char *prefix,
168 const struct bt_field_class *field_class)
169 {
170 const struct bt_field_class_array *array_fc =
171 (const void *) field_class;
172
173 BUF_APPEND(", %selement-fc-addr=%p, %selement-fc-type=%s",
174 PRFIELD(array_fc->element_fc),
175 PRFIELD(bt_common_field_class_type_string(array_fc->element_fc->type)));
176 }
177
178 static inline void format_field_class(char **buf_ch, bool extended,
179 const char *prefix, const struct bt_field_class *field_class)
180 {
181 char tmp_prefix[TMP_PREFIX_LEN];
182
183 BUF_APPEND(", %stype=%s",
184 PRFIELD(bt_common_field_class_type_string(field_class->type)));
185
186 if (extended) {
187 BUF_APPEND(", %sis-frozen=%d", PRFIELD(field_class->frozen));
188 BUF_APPEND(", %sis-part-of-trace-class=%d",
189 PRFIELD(field_class->part_of_trace_class));
190 } else {
191 return;
192 }
193
194 switch (field_class->type) {
195 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
196 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
197 {
198 format_integer_field_class(buf_ch, extended, prefix, field_class);
199 break;
200 }
201 case BT_FIELD_CLASS_TYPE_REAL:
202 {
203 const struct bt_field_class_real *real_fc = (void *) field_class;
204
205 BUF_APPEND(", %sis-single-precision=%d",
206 PRFIELD(real_fc->is_single_precision));
207 break;
208 }
209 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
210 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
211 {
212 const struct bt_field_class_enumeration *enum_fc =
213 (const void *) field_class;
214
215 format_integer_field_class(buf_ch, extended, prefix, field_class);
216 BUF_APPEND(", %smapping-count=%u",
217 PRFIELD(enum_fc->mappings->len));
218 break;
219 }
220 case BT_FIELD_CLASS_TYPE_STRUCTURE:
221 {
222 const struct bt_field_class_structure *struct_fc =
223 (const void *) field_class;
224
225 if (struct_fc->common.named_fcs) {
226 BUF_APPEND(", %smember-count=%u",
227 PRFIELD(struct_fc->common.named_fcs->len));
228 }
229
230 break;
231 }
232 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
233 {
234 const struct bt_field_class_static_array *array_fc =
235 (const void *) field_class;
236
237 format_array_field_class(buf_ch, extended, prefix, field_class);
238 BUF_APPEND(", %slength=%" PRIu64, PRFIELD(array_fc->length));
239 break;
240 }
241 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
242 {
243 const struct bt_field_class_dynamic_array *array_fc =
244 (const void *) field_class;
245
246 format_array_field_class(buf_ch, extended, prefix, field_class);
247
248 if (array_fc->length_fc) {
249 SET_TMP_PREFIX("length-fc-");
250 format_field_class(buf_ch, extended, tmp_prefix,
251 array_fc->length_fc);
252 }
253
254 if (array_fc->length_field_path) {
255 SET_TMP_PREFIX("length-field-path-");
256 format_field_path(buf_ch, extended, tmp_prefix,
257 array_fc->length_field_path);
258 }
259
260 break;
261 }
262 case BT_FIELD_CLASS_TYPE_VARIANT:
263 {
264 const struct bt_field_class_variant *var_fc =
265 (const void *) field_class;
266
267 if (var_fc->common.named_fcs) {
268 BUF_APPEND(", %soption-count=%u",
269 PRFIELD(var_fc->common.named_fcs->len));
270 }
271
272 if (var_fc->selector_fc) {
273 SET_TMP_PREFIX("selector-fc-");
274 format_field_class(buf_ch, extended, tmp_prefix,
275 var_fc->selector_fc);
276 }
277
278 if (var_fc->selector_field_path) {
279 SET_TMP_PREFIX("selector-field-path-");
280 format_field_path(buf_ch, extended, tmp_prefix,
281 var_fc->selector_field_path);
282 }
283
284 break;
285 }
286 default:
287 break;
288 }
289 }
290
291 static inline void format_field_integer_extended(char **buf_ch,
292 const char *prefix, const struct bt_field *field)
293 {
294 const struct bt_field_integer *integer = (void *) field;
295 const struct bt_field_class_integer *field_class =
296 (void *) field->class;
297 const char *fmt = NULL;
298
299 BT_ASSERT(field_class);
300
301 if (field_class->base == BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL) {
302 fmt = ", %svalue=%" PRIo64;
303 } else if (field_class->base == BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL) {
304 fmt = ", %svalue=%" PRIx64;
305 }
306
307 if (field_class->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
308 field_class->common.type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION) {
309 if (!fmt) {
310 fmt = ", %svalue=%" PRId64;
311 }
312
313 BUF_APPEND(fmt, PRFIELD(integer->value.i));
314 } else {
315 if (!fmt) {
316 fmt = ", %svalue=%" PRIu64;
317 }
318
319 BUF_APPEND(fmt, PRFIELD(integer->value.u));
320 }
321 }
322
323 static inline void format_field(char **buf_ch, bool extended,
324 const char *prefix, const struct bt_field *field)
325 {
326 BUF_APPEND(", %sis-set=%d", PRFIELD(field->is_set));
327
328 if (extended) {
329 BUF_APPEND(", %sis-frozen=%d", PRFIELD(field->frozen));
330 }
331
332 BUF_APPEND(", %sclass-addr=%p", PRFIELD(field->class));
333
334 if (!field->class) {
335 return;
336 }
337
338 BUF_APPEND(", %sclass-type=%s",
339 PRFIELD(bt_common_field_class_type_string(field->class->type)));
340
341 if (!extended || !field->is_set) {
342 return;
343 }
344
345 switch (field->class->type) {
346 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
347 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
348 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
349 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
350 {
351 format_field_integer_extended(buf_ch, prefix, field);
352 break;
353 }
354 case BT_FIELD_CLASS_TYPE_REAL:
355 {
356 const struct bt_field_real *real_field = (const void *) field;
357
358 BUF_APPEND(", %svalue=%f", PRFIELD(real_field->value));
359 break;
360 }
361 case BT_FIELD_CLASS_TYPE_STRING:
362 {
363 const struct bt_field_string *str = (const void *) field;
364
365 if (str->buf) {
366 BT_ASSERT(str->buf->data);
367 BUF_APPEND(", %spartial-value=\"%.32s\"",
368 PRFIELD(str->buf->data));
369 }
370
371 break;
372 }
373 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
374 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
375 {
376 const struct bt_field_array *array_field = (const void *) field;
377
378 BUF_APPEND(", %slength=%" PRIu64, PRFIELD(array_field->length));
379
380 if (array_field->fields) {
381 BUF_APPEND(", %sallocated-length=%u",
382 PRFIELD(array_field->fields->len));
383 }
384
385 break;
386 }
387 case BT_FIELD_CLASS_TYPE_VARIANT:
388 {
389 const struct bt_field_variant *var_field = (const void *) field;
390
391 BUF_APPEND(", %sselected-field-index=%" PRIu64,
392 PRFIELD(var_field->selected_index));
393 break;
394 }
395 default:
396 break;
397 }
398 }
399
400 static inline void format_field_path(char **buf_ch, bool extended,
401 const char *prefix, const struct bt_field_path *field_path)
402 {
403 uint64_t i;
404
405 if (field_path->items) {
406 BT_ASSERT(field_path->items);
407 BUF_APPEND(", %sitem-count=%u",
408 PRFIELD(field_path->items->len));
409 }
410
411 if (!extended || !field_path->items) {
412 return;
413 }
414
415 BUF_APPEND(", %spath=[%s",
416 PRFIELD(bt_common_scope_string(field_path->root)));
417
418 for (i = 0; i < bt_field_path_get_item_count(field_path); i++) {
419 const struct bt_field_path_item *fp_item =
420 bt_field_path_borrow_item_by_index_const(field_path, i);
421
422 switch (bt_field_path_item_get_type(fp_item)) {
423 case BT_FIELD_PATH_ITEM_TYPE_INDEX:
424 BUF_APPEND(", %" PRIu64,
425 bt_field_path_item_index_get_index(fp_item));
426 break;
427 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
428 BUF_APPEND("%s", ", <CUR>");
429 break;
430 default:
431 abort();
432 }
433 }
434
435 BUF_APPEND("%s", "]");
436 }
437
438 static inline void format_trace_class(char **buf_ch, bool extended,
439 const char *prefix, const struct bt_trace_class *trace_class)
440 {
441 if (!extended) {
442 return;
443 }
444
445 BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace_class->frozen));
446
447 if (trace_class->stream_classes) {
448 BUF_APPEND(", %sstream-class-count=%u",
449 PRFIELD(trace_class->stream_classes->len));
450 }
451
452 BUF_APPEND(", %sassigns-auto-sc-id=%d",
453 PRFIELD(trace_class->assigns_automatic_stream_class_id));
454 }
455
456 static inline void format_trace(char **buf_ch, bool extended,
457 const char *prefix, const struct bt_trace *trace)
458 {
459 char tmp_prefix[TMP_PREFIX_LEN];
460
461 if (trace->name.value) {
462 BUF_APPEND(", %sname=\"%s\"", PRFIELD(trace->name.value));
463 }
464
465 if (!extended) {
466 return;
467 }
468
469 if (trace->uuid.value) {
470 BUF_APPEND_UUID(trace->uuid.value);
471 }
472
473 BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace->frozen));
474
475 if (trace->streams) {
476 BUF_APPEND(", %sstream-count=%u",
477 PRFIELD(trace->streams->len));
478 }
479
480 if (!trace->class) {
481 return;
482 }
483
484 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace->class));
485 SET_TMP_PREFIX("trace-class-");
486 format_trace_class(buf_ch, false, tmp_prefix, trace->class);
487 }
488
489 static inline void format_stream_class(char **buf_ch, bool extended,
490 const char *prefix,
491 const struct bt_stream_class *stream_class)
492 {
493 const struct bt_trace_class *trace_class;
494 char tmp_prefix[TMP_PREFIX_LEN];
495
496 BUF_APPEND(", %sid=%" PRIu64, PRFIELD(stream_class->id));
497
498 if (stream_class->name.value) {
499 BUF_APPEND(", %sname=\"%s\"",
500 PRFIELD(stream_class->name.value));
501 }
502
503 if (!extended) {
504 return;
505 }
506
507 BUF_APPEND(", %sis-frozen=%d", PRFIELD(stream_class->frozen));
508
509 if (stream_class->event_classes) {
510 BUF_APPEND(", %sevent-class-count=%u",
511 PRFIELD(stream_class->event_classes->len));
512 }
513
514 BUF_APPEND(", %spacket-context-fc-addr=%p, "
515 "%sevent-common-context-fc-addr=%p",
516 PRFIELD(stream_class->packet_context_fc),
517 PRFIELD(stream_class->event_common_context_fc));
518 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
519 if (!trace_class) {
520 return;
521 }
522
523 BUF_APPEND(", %sassigns-auto-ec-id=%d, %sassigns-auto-stream-id=%d, "
524 "%ssupports-packets=%d, "
525 "%spackets-have-begin-default-cs=%d, "
526 "%spackets-have-end-default-cs=%d, "
527 "%ssupports-discarded-events=%d, "
528 "%sdiscarded-events-have-default-cs=%d, "
529 "%ssupports-discarded-packets=%d, "
530 "%sdiscarded-packets-have-default-cs=%d",
531 PRFIELD(stream_class->assigns_automatic_event_class_id),
532 PRFIELD(stream_class->assigns_automatic_stream_id),
533 PRFIELD(stream_class->supports_packets),
534 PRFIELD(stream_class->packets_have_beginning_default_clock_snapshot),
535 PRFIELD(stream_class->packets_have_end_default_clock_snapshot),
536 PRFIELD(stream_class->supports_discarded_events),
537 PRFIELD(stream_class->discarded_events_have_default_clock_snapshots),
538 PRFIELD(stream_class->supports_discarded_packets),
539 PRFIELD(stream_class->discarded_packets_have_default_clock_snapshots));
540 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class));
541 SET_TMP_PREFIX("trace-class-");
542 format_trace_class(buf_ch, false, tmp_prefix, trace_class);
543 SET_TMP_PREFIX("pcf-pool-");
544 format_object_pool(buf_ch, extended, tmp_prefix,
545 &stream_class->packet_context_field_pool);
546 }
547
548 static inline void format_event_class(char **buf_ch, bool extended,
549 const char *prefix, const struct bt_event_class *event_class)
550 {
551 const struct bt_stream_class *stream_class;
552 const struct bt_trace_class *trace_class;
553 char tmp_prefix[TMP_PREFIX_LEN];
554
555 BUF_APPEND(", %sid=%" PRIu64, PRFIELD(event_class->id));
556
557 if (event_class->name.value) {
558 BUF_APPEND(", %sname=\"%s\"",
559 PRFIELD(event_class->name.value));
560 }
561
562 if (!extended) {
563 return;
564 }
565
566 BUF_APPEND(", %sis-frozen=%d", PRFIELD(event_class->frozen));
567
568 if (event_class->log_level.base.avail) {
569 BUF_APPEND(", %slog-level=%s",
570 PRFIELD(bt_common_event_class_log_level_string(
571 (int) event_class->log_level.value)));
572 }
573
574 if (event_class->emf_uri.value) {
575 BUF_APPEND(", %semf-uri=\"%s\"",
576 PRFIELD(event_class->emf_uri.value));
577 }
578
579 BUF_APPEND(", %sspecific-context-fc-addr=%p, %spayload-fc-addr=%p",
580 PRFIELD(event_class->specific_context_fc),
581 PRFIELD(event_class->payload_fc));
582
583 stream_class = bt_event_class_borrow_stream_class_const(event_class);
584 if (!stream_class) {
585 return;
586 }
587
588 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class));
589 SET_TMP_PREFIX("stream-class-");
590 format_stream_class(buf_ch, false, tmp_prefix, stream_class);
591 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
592 if (!trace_class) {
593 return;
594 }
595
596 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class));
597 SET_TMP_PREFIX("trace-class-");
598 format_trace_class(buf_ch, false, tmp_prefix, trace_class);
599 SET_TMP_PREFIX("event-pool-");
600 format_object_pool(buf_ch, extended, tmp_prefix,
601 &event_class->event_pool);
602 }
603
604 static inline void format_stream(char **buf_ch, bool extended,
605 const char *prefix, const struct bt_stream *stream)
606 {
607 const struct bt_stream_class *stream_class;
608 const struct bt_trace_class *trace_class = NULL;
609 const struct bt_trace *trace = NULL;
610 char tmp_prefix[TMP_PREFIX_LEN];
611
612 BUF_APPEND(", %sid=%" PRIu64, PRFIELD(stream->id));
613
614 if (stream->name.value) {
615 BUF_APPEND(", %sname=\"%s\"", PRFIELD(stream->name.value));
616 }
617
618 if (!extended) {
619 return;
620 }
621
622 stream_class = bt_stream_borrow_class_const(stream);
623 if (stream_class) {
624 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class));
625 SET_TMP_PREFIX("stream-class-");
626 format_stream_class(buf_ch, false, tmp_prefix, stream_class);
627 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
628 }
629
630 if (trace_class) {
631 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class));
632 SET_TMP_PREFIX("trace-class-");
633 format_trace_class(buf_ch, false, tmp_prefix, trace_class);
634 }
635
636 trace = bt_stream_borrow_trace_inline(stream);
637 if (trace) {
638 BUF_APPEND(", %strace-addr=%p", PRFIELD(trace));
639 SET_TMP_PREFIX("trace-");
640 format_trace(buf_ch, false, tmp_prefix, trace);
641 }
642
643 SET_TMP_PREFIX("packet-pool-");
644 format_object_pool(buf_ch, extended, tmp_prefix, &stream->packet_pool);
645 }
646
647 static inline void format_packet(char **buf_ch, bool extended,
648 const char *prefix, const struct bt_packet *packet)
649 {
650 const struct bt_stream *stream;
651 const struct bt_trace_class *trace_class;
652 char tmp_prefix[TMP_PREFIX_LEN];
653
654 if (!extended) {
655 return;
656 }
657
658 BUF_APPEND(", %sis-frozen=%d, %scontext-field-addr=%p",
659 PRFIELD(packet->frozen),
660 PRFIELD(packet->context_field ? packet->context_field->field : NULL));
661 stream = bt_packet_borrow_stream_const(packet);
662 if (!stream) {
663 return;
664 }
665
666 BUF_APPEND(", %sstream-addr=%p", PRFIELD(stream));
667 SET_TMP_PREFIX("stream-");
668 format_stream(buf_ch, false, tmp_prefix, stream);
669 trace_class = (const struct bt_trace_class *) bt_object_borrow_parent(&stream->base);
670 if (!trace_class) {
671 return;
672 }
673
674 BUF_APPEND(", %strace-class-addr=%p", PRFIELD(trace_class));
675 SET_TMP_PREFIX("trace-class-");
676 format_trace_class(buf_ch, false, tmp_prefix, trace_class);
677 }
678
679 static inline void format_event(char **buf_ch, bool extended,
680 const char *prefix, const struct bt_event *event)
681 {
682 const struct bt_trace_class *trace_class;
683 const struct bt_stream_class *stream_class;
684 char tmp_prefix[TMP_PREFIX_LEN];
685
686 if (!extended) {
687 return;
688 }
689
690 BUF_APPEND(", %sis-frozen=%d, "
691 "%scommon-context-field-addr=%p, "
692 "%sspecific-context-field-addr=%p, "
693 "%spayload-field-addr=%p, ",
694 PRFIELD(event->frozen),
695 PRFIELD(event->common_context_field),
696 PRFIELD(event->specific_context_field),
697 PRFIELD(event->payload_field));
698 BUF_APPEND(", %sevent-class-addr=%p", PRFIELD(event->class));
699
700 if (!event->class) {
701 return;
702 }
703
704 SET_TMP_PREFIX("event-class-");
705 format_event_class(buf_ch, false, tmp_prefix, event->class);
706 stream_class = bt_event_class_borrow_stream_class(event->class);
707 if (stream_class) {
708 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class));
709 SET_TMP_PREFIX("stream-class-");
710 format_stream_class(buf_ch, false, tmp_prefix,
711 stream_class);
712
713 trace_class = bt_stream_class_borrow_trace_class_inline(
714 stream_class);
715 if (trace_class) {
716 BUF_APPEND(", %strace-class-addr=%p",
717 PRFIELD(trace_class));
718 SET_TMP_PREFIX("trace-class-");
719 format_trace_class(buf_ch, false, tmp_prefix,
720 trace_class);
721 }
722 }
723
724 if (event->stream) {
725 BUF_APPEND(", %sstream-addr=%p", PRFIELD(event->stream));
726 SET_TMP_PREFIX("stream-");
727 format_stream(buf_ch, false, tmp_prefix, event->stream);
728 }
729
730 if (event->packet) {
731 BUF_APPEND(", %spacket-addr=%p", PRFIELD(event->packet));
732 SET_TMP_PREFIX("packet-");
733 format_packet(buf_ch, false, tmp_prefix, event->packet);
734 }
735 }
736
737 static inline void format_clock_class(char **buf_ch, bool extended,
738 const char *prefix, const struct bt_clock_class *clock_class)
739 {
740 char tmp_prefix[TMP_PREFIX_LEN];
741
742 if (clock_class->name.value) {
743 BUF_APPEND(", %sname=\"%s\"", PRFIELD(clock_class->name.value));
744 }
745
746 BUF_APPEND(", %sfreq=%" PRIu64, PRFIELD(clock_class->frequency));
747
748 if (!extended) {
749 return;
750 }
751
752 if (clock_class->description.value) {
753 BUF_APPEND(", %spartial-descr=\"%.32s\"",
754 PRFIELD(clock_class->description.value));
755 }
756
757 if (clock_class->uuid.value) {
758 BUF_APPEND_UUID(clock_class->uuid.value);
759 }
760
761 BUF_APPEND(", %sis-frozen=%d, %sprecision=%" PRIu64 ", "
762 "%soffset-s=%" PRId64 ", "
763 "%soffset-cycles=%" PRIu64 ", %sorigin-is-unix-epoch=%d, "
764 "%sbase-offset-ns=%" PRId64,
765 PRFIELD(clock_class->frozen), PRFIELD(clock_class->precision),
766 PRFIELD(clock_class->offset_seconds),
767 PRFIELD(clock_class->offset_cycles),
768 PRFIELD(clock_class->origin_is_unix_epoch),
769 PRFIELD(clock_class->base_offset.value_ns));
770
771 SET_TMP_PREFIX("cs-pool-");
772 format_object_pool(buf_ch, extended, tmp_prefix,
773 &clock_class->cs_pool);
774 }
775
776 static inline void format_clock_snapshot(char **buf_ch, bool extended,
777 const char *prefix, const struct bt_clock_snapshot *clock_snapshot)
778 {
779 char tmp_prefix[TMP_PREFIX_LEN];
780 BUF_APPEND(", %svalue=%" PRIu64 ", %sns-from-origin=%" PRId64,
781 PRFIELD(clock_snapshot->value_cycles),
782 PRFIELD(clock_snapshot->ns_from_origin));
783
784 if (!extended) {
785 return;
786 }
787
788 BUF_APPEND(", %sis-set=%d", PRFIELD(clock_snapshot->is_set));
789
790 if (clock_snapshot->clock_class) {
791 BUF_APPEND(", %sclock-class-addr=%p",
792 PRFIELD(clock_snapshot->clock_class));
793 SET_TMP_PREFIX("clock-class-");
794 format_clock_class(buf_ch, false, tmp_prefix,
795 clock_snapshot->clock_class);
796 }
797 }
798
799 static inline void format_value(char **buf_ch, bool extended,
800 const char *prefix, const struct bt_value *value)
801 {
802 BUF_APPEND(", %stype=%s",
803 PRFIELD(bt_common_value_type_string(bt_value_get_type(value))));
804
805 if (!extended) {
806 return;
807 }
808
809 switch (bt_value_get_type(value)) {
810 case BT_VALUE_TYPE_BOOL:
811 {
812 bt_bool val = bt_value_bool_get(value);
813
814 BUF_APPEND(", %svalue=%d", PRFIELD(val));
815 break;
816 }
817 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
818 {
819 BUF_APPEND(", %svalue=%" PRIu64,
820 PRFIELD(bt_value_unsigned_integer_get(value)));
821 break;
822 }
823 case BT_VALUE_TYPE_SIGNED_INTEGER:
824 {
825 BUF_APPEND(", %svalue=%" PRId64,
826 PRFIELD(bt_value_signed_integer_get(value)));
827 break;
828 }
829 case BT_VALUE_TYPE_REAL:
830 {
831 double val = bt_value_real_get(value);
832
833 BUF_APPEND(", %svalue=%f", PRFIELD(val));
834 break;
835 }
836 case BT_VALUE_TYPE_STRING:
837 {
838 const char *val = bt_value_string_get(value);
839
840 BUF_APPEND(", %spartial-value=\"%.32s\"", PRFIELD(val));
841 break;
842 }
843 case BT_VALUE_TYPE_ARRAY:
844 {
845 int64_t count = bt_value_array_get_size(value);
846
847 BT_ASSERT(count >= 0);
848 BUF_APPEND(", %selement-count=%" PRId64, PRFIELD(count));
849 break;
850 }
851 case BT_VALUE_TYPE_MAP:
852 {
853 int64_t count = bt_value_map_get_size(value);
854
855 BT_ASSERT(count >= 0);
856 BUF_APPEND(", %selement-count=%" PRId64, PRFIELD(count));
857 break;
858 }
859 default:
860 break;
861 }
862 }
863
864 static inline void format_message(char **buf_ch, bool extended,
865 const char *prefix, const struct bt_message *msg)
866 {
867 char tmp_prefix[TMP_PREFIX_LEN];
868
869 BUF_APPEND(", %stype=%s",
870 PRFIELD(bt_message_type_string(msg->type)));
871
872 if (!extended) {
873 return;
874 }
875
876 BUF_APPEND(", %sis-frozen=%d, %sgraph-addr=%p",
877 PRFIELD(msg->frozen), PRFIELD(msg->graph));
878
879 switch (msg->type) {
880 case BT_MESSAGE_TYPE_EVENT:
881 {
882 const struct bt_message_event *msg_event =
883 (const void *) msg;
884
885 if (msg_event->event) {
886 SET_TMP_PREFIX("event-");
887 format_event(buf_ch, true, tmp_prefix,
888 msg_event->event);
889 }
890
891 if (msg_event->default_cs) {
892 SET_TMP_PREFIX("default-cs-");
893 format_clock_snapshot(buf_ch, true, tmp_prefix,
894 msg_event->default_cs);
895 }
896
897 break;
898 }
899 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
900 case BT_MESSAGE_TYPE_STREAM_END:
901 {
902 const struct bt_message_stream *msg_stream = (const void *) msg;
903
904 if (msg_stream->stream) {
905 SET_TMP_PREFIX("stream-");
906 format_stream(buf_ch, true, tmp_prefix,
907 msg_stream->stream);
908 }
909
910 BUF_APPEND(", %sdefault-cs-state=%s",
911 PRFIELD(bt_message_stream_clock_snapshot_state_string(
912 msg_stream->default_cs_state)));
913
914 if (msg_stream->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
915 SET_TMP_PREFIX("default-cs-");
916 format_clock_snapshot(buf_ch, true, tmp_prefix,
917 msg_stream->default_cs);
918 }
919
920 break;
921 }
922 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
923 case BT_MESSAGE_TYPE_PACKET_END:
924 {
925 const struct bt_message_packet *msg_packet = (const void *) msg;
926
927 if (msg_packet->packet) {
928 SET_TMP_PREFIX("packet-");
929 format_packet(buf_ch, true, tmp_prefix,
930 msg_packet->packet);
931 }
932
933 if (msg_packet->default_cs) {
934 SET_TMP_PREFIX("default-cs-");
935 format_clock_snapshot(buf_ch, true, tmp_prefix,
936 msg_packet->default_cs);
937 }
938
939 break;
940 }
941 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
942 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
943 {
944 const struct bt_message_discarded_items *msg_disc_items =
945 (const void *) msg;
946
947 if (msg_disc_items->stream) {
948 SET_TMP_PREFIX("stream-");
949 format_stream(buf_ch, true, tmp_prefix,
950 msg_disc_items->stream);
951 }
952
953 if (msg_disc_items->default_begin_cs) {
954 SET_TMP_PREFIX("begin-default-cs-");
955 format_clock_snapshot(buf_ch, true, tmp_prefix,
956 msg_disc_items->default_begin_cs);
957 }
958
959 if (msg_disc_items->default_end_cs) {
960 SET_TMP_PREFIX("end-default-cs-");
961 format_clock_snapshot(buf_ch, true, tmp_prefix,
962 msg_disc_items->default_end_cs);
963 }
964
965 if (msg_disc_items->count.base.avail) {
966 BUF_APPEND(", %scount=%" PRIu64,
967 PRFIELD(msg_disc_items->count.value));
968 }
969
970 break;
971 }
972 default:
973 break;
974 }
975 }
976
977 static inline void format_plugin_so_shared_lib_handle(char **buf_ch,
978 const char *prefix,
979 const struct bt_plugin_so_shared_lib_handle *handle)
980 {
981 BUF_APPEND(", %saddr=%p", PRFIELD(handle));
982
983 if (handle->path) {
984 BUF_APPEND(", %spath=\"%s\"", PRFIELD_GSTRING(handle->path));
985 }
986 }
987
988 static inline void format_component_class(char **buf_ch, bool extended,
989 const char *prefix,
990 const struct bt_component_class *comp_class)
991 {
992 char tmp_prefix[TMP_PREFIX_LEN];
993
994 BUF_APPEND(", %stype=%s, %sname=\"%s\"",
995 PRFIELD(bt_component_class_type_string(comp_class->type)),
996 PRFIELD_GSTRING(comp_class->name));
997
998 if (comp_class->description) {
999 BUF_APPEND(", %spartial-descr=\"%.32s\"",
1000 PRFIELD_GSTRING(comp_class->description));
1001 }
1002
1003 if (!extended) {
1004 return;
1005 }
1006
1007 BUF_APPEND(", %sis-frozen=%d", PRFIELD(comp_class->frozen));
1008
1009 if (comp_class->so_handle) {
1010 SET_TMP_PREFIX("so-handle-");
1011 format_plugin_so_shared_lib_handle(buf_ch, tmp_prefix,
1012 comp_class->so_handle);
1013 }
1014 }
1015
1016 static inline void format_component(char **buf_ch, bool extended,
1017 const char *prefix, const struct bt_component *component)
1018 {
1019 char tmp_prefix[TMP_PREFIX_LEN];
1020
1021 BUF_APPEND(", %sname=\"%s\", %slog-level=%s",
1022 PRFIELD_GSTRING(component->name),
1023 PRFIELD(bt_common_logging_level_string(component->log_level)));
1024
1025 if (component->class) {
1026 SET_TMP_PREFIX("class-");
1027 format_component_class(buf_ch, extended, tmp_prefix,
1028 component->class);
1029 }
1030
1031 if (!extended) {
1032 return;
1033 }
1034
1035 if (component->input_ports) {
1036 BUF_APPEND(", %sinput-port-count=%u",
1037 PRFIELD(component->input_ports->len));
1038 }
1039
1040 if (component->output_ports) {
1041 BUF_APPEND(", %soutput-port-count=%u",
1042 PRFIELD(component->output_ports->len));
1043 }
1044 }
1045
1046 static inline void format_port(char **buf_ch, bool extended,
1047 const char *prefix, const struct bt_port *port)
1048 {
1049 char tmp_prefix[TMP_PREFIX_LEN];
1050
1051 BUF_APPEND(", %stype=%s, %sname=\"%s\"",
1052 PRFIELD(bt_port_type_string(port->type)),
1053 PRFIELD_GSTRING(port->name));
1054
1055 if (!extended) {
1056 return;
1057 }
1058
1059 if (port->connection) {
1060 SET_TMP_PREFIX("conn-");
1061 format_connection(buf_ch, false, tmp_prefix, port->connection);
1062 }
1063 }
1064
1065 static inline void format_connection(char **buf_ch, bool extended,
1066 const char *prefix, const struct bt_connection *connection)
1067 {
1068 char tmp_prefix[TMP_PREFIX_LEN];
1069
1070 if (!extended) {
1071 return;
1072 }
1073
1074 if (connection->upstream_port) {
1075 SET_TMP_PREFIX("upstream-port-");
1076 format_port(buf_ch, false, tmp_prefix,
1077 connection->upstream_port);
1078 }
1079
1080 if (connection->downstream_port) {
1081 SET_TMP_PREFIX("downstream-port-");
1082 format_port(buf_ch, false, tmp_prefix,
1083 connection->downstream_port);
1084 }
1085 }
1086
1087 static inline void format_graph(char **buf_ch, bool extended,
1088 const char *prefix, const struct bt_graph *graph)
1089 {
1090 char tmp_prefix[TMP_PREFIX_LEN];
1091
1092 BUF_APPEND(", %sis-canceled=%d, %scan-consume=%d, "
1093 "%sconfig-state=%s",
1094 PRFIELD(graph->canceled),
1095 PRFIELD(graph->can_consume),
1096 PRFIELD(bt_graph_configuration_state_string(graph->config_state)));
1097
1098 if (!extended) {
1099 return;
1100 }
1101
1102 if (graph->components) {
1103 BUF_APPEND(", %scomp-count=%u",
1104 PRFIELD(graph->components->len));
1105 }
1106
1107 if (graph->connections) {
1108 BUF_APPEND(", %sconn-count=%u",
1109 PRFIELD(graph->connections->len));
1110 }
1111
1112 SET_TMP_PREFIX("en-pool-");
1113 format_object_pool(buf_ch, extended, tmp_prefix,
1114 &graph->event_msg_pool);
1115 SET_TMP_PREFIX("pbn-pool-");
1116 format_object_pool(buf_ch, extended, tmp_prefix,
1117 &graph->packet_begin_msg_pool);
1118 SET_TMP_PREFIX("pen-pool-");
1119 format_object_pool(buf_ch, extended, tmp_prefix,
1120 &graph->packet_end_msg_pool);
1121 }
1122
1123 static inline void format_message_iterator(char **buf_ch,
1124 bool extended, const char *prefix,
1125 const struct bt_message_iterator *iterator)
1126 {
1127 const char *type;
1128 char tmp_prefix[TMP_PREFIX_LEN];
1129
1130 if (iterator->type == BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT) {
1131 type = "BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT";
1132 } else if (iterator->type == BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT) {
1133 type = "BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT";
1134 } else {
1135 type = "(unknown)";
1136 }
1137
1138 BUF_APPEND(", %stype=%s", PRFIELD(type));
1139
1140 switch (iterator->type) {
1141 case BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT:
1142 {
1143 const struct bt_self_component_port_input_message_iterator *
1144 port_in_iter = (const void *) iterator;
1145
1146 if (port_in_iter->upstream_component) {
1147 SET_TMP_PREFIX("upstream-comp-");
1148 format_component(buf_ch, false, tmp_prefix,
1149 port_in_iter->upstream_component);
1150 }
1151
1152 if (port_in_iter->upstream_port) {
1153 SET_TMP_PREFIX("upstream-port-");
1154 format_port(buf_ch, false, tmp_prefix,
1155 port_in_iter->upstream_port);
1156 }
1157
1158 if (port_in_iter->connection) {
1159 SET_TMP_PREFIX("upstream-conn-");
1160 format_connection(buf_ch, false, tmp_prefix,
1161 port_in_iter->connection);
1162 }
1163 break;
1164 }
1165 case BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT:
1166 {
1167 const struct bt_port_output_message_iterator *port_out_iter =
1168 (const void *) iterator;
1169
1170 if (port_out_iter->graph) {
1171 SET_TMP_PREFIX("graph-");
1172 format_graph(buf_ch, false, tmp_prefix,
1173 port_out_iter->graph);
1174 }
1175
1176 if (port_out_iter->colander) {
1177 SET_TMP_PREFIX("colander-comp-");
1178 format_component(buf_ch, false, tmp_prefix,
1179 (void *) port_out_iter->colander);
1180 }
1181
1182 break;
1183 }
1184 default:
1185 break;
1186 }
1187 }
1188
1189 static inline void format_plugin(char **buf_ch, bool extended,
1190 const char *prefix, const struct bt_plugin *plugin)
1191 {
1192 char tmp_prefix[TMP_PREFIX_LEN];
1193
1194 BUF_APPEND(", %stype=%s", PRFIELD(bt_plugin_type_string(plugin->type)));
1195
1196 if (plugin->info.path_set) {
1197 BUF_APPEND(", %spath=\"%s\"",
1198 PRFIELD_GSTRING(plugin->info.path));
1199 }
1200
1201 if (plugin->info.name_set) {
1202 BUF_APPEND(", %sname=\"%s\"",
1203 PRFIELD_GSTRING(plugin->info.name));
1204 }
1205
1206 if (!extended) {
1207 return;
1208 }
1209
1210 if (plugin->info.author_set) {
1211 BUF_APPEND(", %sauthor=\"%s\"",
1212 PRFIELD_GSTRING(plugin->info.author));
1213 }
1214
1215 if (plugin->info.license_set) {
1216 BUF_APPEND(", %slicense=\"%s\"",
1217 PRFIELD_GSTRING(plugin->info.license));
1218 }
1219
1220 if (plugin->info.version_set) {
1221 BUF_APPEND(", %sversion=%u.%u.%u%s",
1222 PRFIELD(plugin->info.version.major),
1223 plugin->info.version.minor,
1224 plugin->info.version.patch,
1225 plugin->info.version.extra ?
1226 plugin->info.version.extra->str : "");
1227 }
1228
1229 BUF_APPEND(", %ssrc-comp-class-count=%u, %sflt-comp-class-count=%u, "
1230 "%ssink-comp-class-count=%u",
1231 PRFIELD(plugin->src_comp_classes->len),
1232 PRFIELD(plugin->flt_comp_classes->len),
1233 PRFIELD(plugin->sink_comp_classes->len));
1234
1235 if (plugin->spec_data) {
1236 const struct bt_plugin_so_spec_data *spec_data =
1237 (const void *) plugin->spec_data;
1238
1239 if (spec_data->shared_lib_handle) {
1240 SET_TMP_PREFIX("so-handle-");
1241 format_plugin_so_shared_lib_handle(buf_ch, tmp_prefix,
1242 spec_data->shared_lib_handle);
1243 }
1244 }
1245 }
1246
1247 static inline void format_error_cause(char **buf_ch, bool extended,
1248 const char *prefix, const struct bt_error_cause *cause)
1249 {
1250 const struct bt_error_cause_component_class_id *comp_class_id = NULL;
1251
1252 BUF_APPEND(", %sactor-type=%s, %smodule-name=\"%s\"",
1253 PRFIELD(bt_error_cause_actor_type_string(cause->actor_type)),
1254 PRFIELD_GSTRING(cause->module_name));
1255
1256 if (!extended) {
1257 return;
1258 }
1259
1260 BUF_APPEND(", %spartial-msg=\"%.32s\"",
1261 PRFIELD_GSTRING(cause->message));
1262
1263 switch (cause->actor_type) {
1264 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT:
1265 {
1266 const struct bt_error_cause_component_actor *spec_cause =
1267 (const void *) cause;
1268
1269 BUF_APPEND(", %scomp-name=\"%s\"",
1270 PRFIELD_GSTRING(spec_cause->comp_name));
1271 comp_class_id = &spec_cause->comp_class_id;
1272 break;
1273 }
1274 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS:
1275 {
1276 const struct bt_error_cause_component_class_actor *spec_cause =
1277 (const void *) cause;
1278
1279 comp_class_id = &spec_cause->comp_class_id;
1280 break;
1281 }
1282 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR:
1283 {
1284 const struct bt_error_cause_message_iterator_actor *spec_cause =
1285 (const void *) cause;
1286
1287 BUF_APPEND(", %scomp-name=\"%s\", %scomp-out-port-name=\"%s\"",
1288 PRFIELD_GSTRING(spec_cause->comp_name),
1289 PRFIELD_GSTRING(spec_cause->output_port_name));
1290 comp_class_id = &spec_cause->comp_class_id;
1291 break;
1292 }
1293 default:
1294 break;
1295 }
1296
1297 if (comp_class_id) {
1298 BUF_APPEND(", %scomp-cls-type=%s, %scomp-cls-name=\"%s\", "
1299 "%splugin-name=\"%s\"",
1300 PRFIELD(bt_component_class_type_string(
1301 comp_class_id->type)),
1302 PRFIELD_GSTRING(comp_class_id->name),
1303 PRFIELD_GSTRING(comp_class_id->plugin_name));
1304 }
1305 }
1306
1307 static inline void handle_conversion_specifier_bt(void *priv_data,
1308 char **buf_ch, size_t avail_size,
1309 const char **out_fmt_ch, va_list *args)
1310 {
1311 const char *fmt_ch = *out_fmt_ch;
1312 bool extended = false;
1313 char prefix[64];
1314 char *prefix_ch = prefix;
1315 const void *obj;
1316
1317 /* skip "%!" */
1318 fmt_ch += 2;
1319
1320 if (*fmt_ch == 'u') {
1321 /* UUID */
1322 obj = va_arg(*args, void *);
1323 format_uuid(buf_ch, obj);
1324 goto update_fmt;
1325 }
1326
1327 if (*fmt_ch == '[') {
1328 /* local prefix */
1329 fmt_ch++;
1330
1331 while (true) {
1332 if (*fmt_ch == ']') {
1333 *prefix_ch = '\0';
1334 fmt_ch++;
1335 break;
1336 }
1337
1338 *prefix_ch = *fmt_ch;
1339 prefix_ch++;
1340 fmt_ch++;
1341 }
1342 }
1343
1344 *prefix_ch = '\0';
1345
1346 if (*fmt_ch == '+') {
1347 extended = true;
1348 fmt_ch++;
1349 }
1350
1351 obj = va_arg(*args, void *);
1352 BUF_APPEND("%saddr=%p", prefix, obj);
1353
1354 if (!obj) {
1355 goto update_fmt;
1356 }
1357
1358 switch (*fmt_ch) {
1359 case 'F':
1360 format_field_class(buf_ch, extended, prefix, obj);
1361 break;
1362 case 'f':
1363 format_field(buf_ch, extended, prefix, obj);
1364 break;
1365 case 'P':
1366 format_field_path(buf_ch, extended, prefix, obj);
1367 break;
1368 case 'E':
1369 format_event_class(buf_ch, extended, prefix, obj);
1370 break;
1371 case 'e':
1372 format_event(buf_ch, extended, prefix, obj);
1373 break;
1374 case 'S':
1375 format_stream_class(buf_ch, extended, prefix, obj);
1376 break;
1377 case 's':
1378 format_stream(buf_ch, extended, prefix, obj);
1379 break;
1380 case 'a':
1381 format_packet(buf_ch, extended, prefix, obj);
1382 break;
1383 case 't':
1384 format_trace(buf_ch, extended, prefix, obj);
1385 break;
1386 case 'T':
1387 format_trace_class(buf_ch, extended, prefix, obj);
1388 break;
1389 case 'K':
1390 format_clock_class(buf_ch, extended, prefix, obj);
1391 break;
1392 case 'k':
1393 format_clock_snapshot(buf_ch, extended, prefix, obj);
1394 break;
1395 case 'v':
1396 format_value(buf_ch, extended, prefix, obj);
1397 break;
1398 case 'n':
1399 format_message(buf_ch, extended, prefix, obj);
1400 break;
1401 case 'i':
1402 format_message_iterator(buf_ch, extended, prefix, obj);
1403 break;
1404 case 'C':
1405 format_component_class(buf_ch, extended, prefix, obj);
1406 break;
1407 case 'c':
1408 format_component(buf_ch, extended, prefix, obj);
1409 break;
1410 case 'p':
1411 format_port(buf_ch, extended, prefix, obj);
1412 break;
1413 case 'x':
1414 format_connection(buf_ch, extended, prefix, obj);
1415 break;
1416 case 'l':
1417 format_plugin(buf_ch, extended, prefix, obj);
1418 break;
1419 case 'g':
1420 format_graph(buf_ch, extended, prefix, obj);
1421 break;
1422 case 'o':
1423 format_object_pool(buf_ch, extended, prefix, obj);
1424 break;
1425 case 'O':
1426 format_object(buf_ch, extended, prefix, obj);
1427 break;
1428 case 'r':
1429 format_error_cause(buf_ch, extended, prefix, obj);
1430 break;
1431 default:
1432 abort();
1433 }
1434
1435 update_fmt:
1436 fmt_ch++;
1437 *out_fmt_ch = fmt_ch;
1438 }
1439
1440 void bt_lib_log(const char *func, const char *file, unsigned line,
1441 int lvl, const char *tag, const char *fmt, ...)
1442 {
1443 va_list args;
1444
1445 BT_ASSERT(fmt);
1446 va_start(args, fmt);
1447 bt_common_custom_vsnprintf(lib_logging_buf, LIB_LOGGING_BUF_SIZE, '!',
1448 handle_conversion_specifier_bt, NULL, fmt, &args);
1449 va_end(args);
1450 _bt_log_write_d(func, file, line, lvl, tag, "%s", lib_logging_buf);
1451 }
1452
1453 void bt_lib_maybe_log_and_append_cause(const char *func, const char *file,
1454 unsigned line, int lvl, const char *tag,
1455 const char *fmt, ...)
1456 {
1457 va_list args;
1458 bt_current_thread_error_append_cause_status status;
1459
1460 BT_ASSERT(fmt);
1461 va_start(args, fmt);
1462 bt_common_custom_vsnprintf(lib_logging_buf, LIB_LOGGING_BUF_SIZE, '!',
1463 handle_conversion_specifier_bt, NULL, fmt, &args);
1464 va_end(args);
1465
1466 /* Log conditionally, but always append the error cause */
1467 if (BT_LOG_ON(lvl)) {
1468 _bt_log_write_d(func, file, line, lvl, tag, "%s",
1469 lib_logging_buf);
1470 }
1471
1472 status = bt_current_thread_error_append_cause_from_unknown(
1473 "Babeltrace library", file, line, "%s", lib_logging_buf);
1474 if (status) {
1475 /*
1476 * Worst case: this error cause is not appended to the
1477 * current thread's error.
1478 *
1479 * We can accept this as it's an almost impossible
1480 * scenario and returning an error here would mean you
1481 * need to check the return value of each
1482 * BT_LIB_LOG*_APPEND_CAUSE() macro and that would be
1483 * cumbersome.
1484 */
1485 BT_LOGE("Cannot append error cause to current thread's "
1486 "error object: status=%s",
1487 bt_common_func_status_string(status));
1488 }
1489 }
This page took 0.106821 seconds and 4 git commands to generate.