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