lib: force user to include `<babeltrace2/babeltrace.h>`
[babeltrace.git] / src / lib / lib-logging.c
CommitLineData
476ef981
PP
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
3cd4c495
PP
23#define BT_LOG_TAG "LIB/LIB-LOGGING"
24#include "lib/logging.h"
3dca2276 25
476ef981
PP
26#include <stdarg.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <inttypes.h>
31#include <stdint.h>
476ef981
PP
32#include <wchar.h>
33#include <glib.h>
578e048b 34#include "common/common.h"
6162e6b7 35#include "common/uuid.h"
4fa90f32 36#include <babeltrace2/babeltrace.h>
578e048b 37
51375aa9
PP
38#include "logging.h"
39#include "assert-pre.h"
40#include "assert-post.h"
41#include "value.h"
42#include "object-pool.h"
578e048b
MJ
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"
578e048b
MJ
57#include "graph/message/stream.h"
58#include "graph/port.h"
578e048b
MJ
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"
553c4bab
PP
74#include "error.h"
75#include "assert-pre.h"
476ef981
PP
76
77#define LIB_LOGGING_BUF_SIZE (4096 * 4)
78
c9fe7f23 79static __thread char lib_logging_buf[LIB_LOGGING_BUF_SIZE];
476ef981
PP
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__); \
f6ccaed9 87 BT_ASSERT(_count >= 0); \
476ef981
PP
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) \
44c440bc
PP
95 do { \
96 BUF_APPEND(", %suuid=", prefix); \
97 format_uuid(buf_ch, (_uuid)); \
98 } while (0)
476ef981
PP
99
100#define PRFIELD(_expr) prefix, (_expr)
101
d94d92ac
PP
102#define PRFIELD_GSTRING(_expr) PRFIELD((_expr) ? (_expr)->str : NULL)
103
b293c529 104#define TMP_PREFIX_LEN 128
476ef981
PP
105#define SET_TMP_PREFIX(_prefix2) \
106 do { \
b293c529
PP
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 \
eea66fe0 115 tmp_prefix[TMP_PREFIX_LEN - 1] = '\0'; \
476ef981
PP
116 } while (0)
117
118static inline void format_component(char **buf_ch, bool extended,
862ca4ed 119 const char *prefix, const struct bt_component *component);
476ef981
PP
120
121static inline void format_port(char **buf_ch, bool extended,
862ca4ed 122 const char *prefix, const struct bt_port *port);
476ef981
PP
123
124static inline void format_connection(char **buf_ch, bool extended,
862ca4ed 125 const char *prefix, const struct bt_connection *connection);
476ef981 126
605e1019
PP
127static inline void format_clock_snapshot(char **buf_ch, bool extended,
128 const char *prefix, const struct bt_clock_snapshot *clock_snapshot);
e22b45d0 129
44c440bc 130static inline void format_field_path(char **buf_ch, bool extended,
862ca4ed 131 const char *prefix, const struct bt_field_path *field_path);
44c440bc 132
3fea54f6 133static inline void format_object(char **buf_ch, bool extended,
862ca4ed 134 const char *prefix, const struct bt_object *obj)
476ef981 135{
3fea54f6 136 BUF_APPEND(", %sref-count=%llu", prefix, obj->ref_count);
476ef981
PP
137}
138
44c440bc 139static inline void format_uuid(char **buf_ch, bt_uuid uuid)
e22b45d0 140{
6162e6b7 141 BUF_APPEND("\"" BT_UUID_FMT "\"", BT_UUID_FMT_VALUES(uuid));
e22b45d0
PP
142}
143
312c056a 144static inline void format_object_pool(char **buf_ch, bool extended,
862ca4ed 145 const char *prefix, const struct bt_object_pool *pool)
312c056a
PP
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
5cd6d0e5 154static inline void format_integer_field_class(char **buf_ch,
44c440bc 155 bool extended, const char *prefix,
862ca4ed 156 const struct bt_field_class *field_class)
44c440bc 157{
862ca4ed
PP
158 const struct bt_field_class_integer *int_fc =
159 (const void *) field_class;
44c440bc
PP
160
161 BUF_APPEND(", %srange-size=%" PRIu64 ", %sbase=%s",
5cd6d0e5
PP
162 PRFIELD(int_fc->range),
163 PRFIELD(bt_common_field_class_integer_preferred_display_base_string(int_fc->base)));
44c440bc
PP
164}
165
5cd6d0e5 166static inline void format_array_field_class(char **buf_ch,
44c440bc 167 bool extended, const char *prefix,
862ca4ed 168 const struct bt_field_class *field_class)
44c440bc 169{
862ca4ed
PP
170 const struct bt_field_class_array *array_fc =
171 (const void *) field_class;
44c440bc 172
864cad70 173 BUF_APPEND(", %selement-fc-addr=%p, %selement-fc-type=%s",
5cd6d0e5 174 PRFIELD(array_fc->element_fc),
864cad70 175 PRFIELD(bt_common_field_class_type_string(array_fc->element_fc->type)));
44c440bc
PP
176}
177
5cd6d0e5 178static inline void format_field_class(char **buf_ch, bool extended,
862ca4ed 179 const char *prefix, const struct bt_field_class *field_class)
476ef981 180{
879e14e8 181 char tmp_prefix[TMP_PREFIX_LEN];
44c440bc 182
864cad70
PP
183 BUF_APPEND(", %stype=%s",
184 PRFIELD(bt_common_field_class_type_string(field_class->type)));
476ef981
PP
185
186 if (extended) {
5cd6d0e5 187 BUF_APPEND(", %sis-frozen=%d", PRFIELD(field_class->frozen));
862ca4ed
PP
188 BUF_APPEND(", %sis-part-of-trace-class=%d",
189 PRFIELD(field_class->part_of_trace_class));
476ef981
PP
190 } else {
191 return;
192 }
193
864cad70
PP
194 switch (field_class->type) {
195 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
196 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
476ef981 197 {
5cd6d0e5 198 format_integer_field_class(buf_ch, extended, prefix, field_class);
476ef981
PP
199 break;
200 }
864cad70 201 case BT_FIELD_CLASS_TYPE_REAL:
476ef981 202 {
862ca4ed 203 const struct bt_field_class_real *real_fc = (void *) field_class;
476ef981 204
44c440bc 205 BUF_APPEND(", %sis-single-precision=%d",
5cd6d0e5 206 PRFIELD(real_fc->is_single_precision));
476ef981
PP
207 break;
208 }
864cad70
PP
209 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
210 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
476ef981 211 {
862ca4ed
PP
212 const struct bt_field_class_enumeration *enum_fc =
213 (const void *) field_class;
476ef981 214
5cd6d0e5 215 format_integer_field_class(buf_ch, extended, prefix, field_class);
476ef981 216 BUF_APPEND(", %smapping-count=%u",
5cd6d0e5 217 PRFIELD(enum_fc->mappings->len));
476ef981
PP
218 break;
219 }
864cad70 220 case BT_FIELD_CLASS_TYPE_STRUCTURE:
476ef981 221 {
862ca4ed
PP
222 const struct bt_field_class_structure *struct_fc =
223 (const void *) field_class;
44c440bc 224
5cd6d0e5 225 if (struct_fc->common.named_fcs) {
44c440bc 226 BUF_APPEND(", %smember-count=%u",
5cd6d0e5 227 PRFIELD(struct_fc->common.named_fcs->len));
44c440bc 228 }
476ef981 229
476ef981
PP
230 break;
231 }
864cad70 232 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
476ef981 233 {
862ca4ed
PP
234 const struct bt_field_class_static_array *array_fc =
235 (const void *) field_class;
476ef981 236
5cd6d0e5
PP
237 format_array_field_class(buf_ch, extended, prefix, field_class);
238 BUF_APPEND(", %slength=%" PRIu64, PRFIELD(array_fc->length));
476ef981
PP
239 break;
240 }
864cad70 241 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
476ef981 242 {
862ca4ed
PP
243 const struct bt_field_class_dynamic_array *array_fc =
244 (const void *) field_class;
44c440bc 245
5cd6d0e5 246 format_array_field_class(buf_ch, extended, prefix, field_class);
44c440bc 247
5cd6d0e5
PP
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);
44c440bc
PP
252 }
253
5cd6d0e5 254 if (array_fc->length_field_path) {
44c440bc
PP
255 SET_TMP_PREFIX("length-field-path-");
256 format_field_path(buf_ch, extended, tmp_prefix,
5cd6d0e5 257 array_fc->length_field_path);
44c440bc 258 }
476ef981 259
476ef981
PP
260 break;
261 }
864cad70 262 case BT_FIELD_CLASS_TYPE_VARIANT:
476ef981 263 {
862ca4ed
PP
264 const struct bt_field_class_variant *var_fc =
265 (const void *) field_class;
44c440bc 266
5cd6d0e5 267 if (var_fc->common.named_fcs) {
44c440bc 268 BUF_APPEND(", %soption-count=%u",
5cd6d0e5 269 PRFIELD(var_fc->common.named_fcs->len));
44c440bc
PP
270 }
271
5cd6d0e5
PP
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);
44c440bc
PP
276 }
277
5cd6d0e5 278 if (var_fc->selector_field_path) {
44c440bc
PP
279 SET_TMP_PREFIX("selector-field-path-");
280 format_field_path(buf_ch, extended, tmp_prefix,
5cd6d0e5 281 var_fc->selector_field_path);
44c440bc 282 }
476ef981 283
476ef981
PP
284 break;
285 }
286 default:
287 break;
288 }
289}
290
cb6f1f7d 291static inline void format_field_integer_extended(char **buf_ch,
862ca4ed 292 const char *prefix, const struct bt_field *field)
476ef981 293{
862ca4ed
PP
294 const struct bt_field_integer *integer = (void *) field;
295 const struct bt_field_class_integer *field_class =
296 (void *) field->class;
476ef981
PP
297 const char *fmt = NULL;
298
5cd6d0e5 299 BT_ASSERT(field_class);
312c056a 300
5cd6d0e5 301 if (field_class->base == BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL) {
476ef981 302 fmt = ", %svalue=%" PRIo64;
5cd6d0e5 303 } else if (field_class->base == BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL) {
476ef981
PP
304 fmt = ", %svalue=%" PRIx64;
305 }
306
864cad70
PP
307 if (field_class->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
308 field_class->common.type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION) {
476ef981
PP
309 if (!fmt) {
310 fmt = ", %svalue=%" PRId64;
311 }
312
44c440bc 313 BUF_APPEND(fmt, PRFIELD(integer->value.i));
476ef981
PP
314 } else {
315 if (!fmt) {
316 fmt = ", %svalue=%" PRIu64;
317 }
318
44c440bc 319 BUF_APPEND(fmt, PRFIELD(integer->value.u));
476ef981
PP
320 }
321}
322
cb6f1f7d 323static inline void format_field(char **buf_ch, bool extended,
862ca4ed 324 const char *prefix, const struct bt_field *field)
476ef981 325{
44c440bc 326 BUF_APPEND(", %sis-set=%d", PRFIELD(field->is_set));
476ef981
PP
327
328 if (extended) {
329 BUF_APPEND(", %sis-frozen=%d", PRFIELD(field->frozen));
330 }
331
864cad70 332 BUF_APPEND(", %sclass-addr=%p", PRFIELD(field->class));
312c056a 333
5cd6d0e5 334 if (!field->class) {
312c056a
PP
335 return;
336 }
337
864cad70
PP
338 BUF_APPEND(", %sclass-type=%s",
339 PRFIELD(bt_common_field_class_type_string(field->class->type)));
476ef981 340
44c440bc 341 if (!extended || !field->is_set) {
476ef981
PP
342 return;
343 }
344
864cad70
PP
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:
476ef981 350 {
cb6f1f7d 351 format_field_integer_extended(buf_ch, prefix, field);
476ef981
PP
352 break;
353 }
864cad70 354 case BT_FIELD_CLASS_TYPE_REAL:
476ef981 355 {
862ca4ed 356 const struct bt_field_real *real_field = (const void *) field;
476ef981 357
44c440bc 358 BUF_APPEND(", %svalue=%f", PRFIELD(real_field->value));
476ef981
PP
359 break;
360 }
864cad70 361 case BT_FIELD_CLASS_TYPE_STRING:
476ef981 362 {
862ca4ed 363 const struct bt_field_string *str = (const void *) field;
476ef981 364
4d4b475d
PP
365 if (str->buf) {
366 BT_ASSERT(str->buf->data);
312c056a 367 BUF_APPEND(", %spartial-value=\"%.32s\"",
4d4b475d 368 PRFIELD(str->buf->data));
312c056a 369 }
44c440bc 370
476ef981
PP
371 break;
372 }
864cad70
PP
373 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
374 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
476ef981 375 {
862ca4ed 376 const struct bt_field_array *array_field = (const void *) field;
476ef981 377
44c440bc 378 BUF_APPEND(", %slength=%" PRIu64, PRFIELD(array_field->length));
312c056a 379
44c440bc 380 if (array_field->fields) {
312c056a 381 BUF_APPEND(", %sallocated-length=%u",
44c440bc 382 PRFIELD(array_field->fields->len));
312c056a 383 }
44c440bc 384
476ef981
PP
385 break;
386 }
864cad70 387 case BT_FIELD_CLASS_TYPE_VARIANT:
476ef981 388 {
862ca4ed 389 const struct bt_field_variant *var_field = (const void *) field;
312c056a 390
44c440bc
PP
391 BUF_APPEND(", %sselected-field-index=%" PRIu64,
392 PRFIELD(var_field->selected_index));
312c056a
PP
393 break;
394 }
395 default:
396 break;
397 }
3dca2276
PP
398}
399
476ef981 400static inline void format_field_path(char **buf_ch, bool extended,
862ca4ed 401 const char *prefix, const struct bt_field_path *field_path)
476ef981
PP
402{
403 uint64_t i;
404
66ddcddf
PP
405 if (field_path->items) {
406 BT_ASSERT(field_path->items);
407 BUF_APPEND(", %sitem-count=%u",
408 PRFIELD(field_path->items->len));
312c056a 409 }
476ef981 410
66ddcddf 411 if (!extended || !field_path->items) {
476ef981
PP
412 return;
413 }
414
cb6f1f7d
PP
415 BUF_APPEND(", %spath=[%s",
416 PRFIELD(bt_common_scope_string(field_path->root)));
476ef981 417
66ddcddf
PP
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 }
476ef981
PP
433 }
434
435 BUF_APPEND("%s", "]");
436}
437
862ca4ed
PP
438static inline void format_trace_class(char **buf_ch, bool extended,
439 const char *prefix, const struct bt_trace_class *trace_class)
476ef981 440{
476ef981
PP
441 if (!extended) {
442 return;
443 }
444
862ca4ed 445 BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace_class->frozen));
476ef981 446
862ca4ed 447 if (trace_class->stream_classes) {
312c056a 448 BUF_APPEND(", %sstream-class-count=%u",
862ca4ed 449 PRFIELD(trace_class->stream_classes->len));
312c056a
PP
450 }
451
83ebb7f1 452 BUF_APPEND(", %sassigns-auto-sc-id=%d",
862ca4ed 453 PRFIELD(trace_class->assigns_automatic_stream_class_id));
862ca4ed
PP
454}
455
456static inline void format_trace(char **buf_ch, bool extended,
457 const char *prefix, const struct bt_trace *trace)
458{
879e14e8 459 char tmp_prefix[TMP_PREFIX_LEN];
862ca4ed
PP
460
461 if (trace->name.value) {
462 BUF_APPEND(", %sname=\"%s\"", PRFIELD(trace->name.value));
463 }
464
465 if (!extended) {
466 return;
467 }
468
335a2da5
PP
469 if (trace->uuid.value) {
470 BUF_APPEND_UUID(trace->uuid.value);
471 }
472
862ca4ed
PP
473 BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace->frozen));
474
312c056a
PP
475 if (trace->streams) {
476 BUF_APPEND(", %sstream-count=%u",
477 PRFIELD(trace->streams->len));
478 }
479
862ca4ed
PP
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);
3dca2276
PP
487}
488
cb6f1f7d 489static inline void format_stream_class(char **buf_ch, bool extended,
862ca4ed
PP
490 const char *prefix,
491 const struct bt_stream_class *stream_class)
3dca2276 492{
862ca4ed 493 const struct bt_trace_class *trace_class;
879e14e8 494 char tmp_prefix[TMP_PREFIX_LEN];
476ef981 495
44c440bc 496 BUF_APPEND(", %sid=%" PRIu64, PRFIELD(stream_class->id));
476ef981 497
44c440bc
PP
498 if (stream_class->name.value) {
499 BUF_APPEND(", %sname=\"%s\"",
500 PRFIELD(stream_class->name.value));
476ef981
PP
501 }
502
503 if (!extended) {
504 return;
505 }
506
507 BUF_APPEND(", %sis-frozen=%d", PRFIELD(stream_class->frozen));
312c056a
PP
508
509 if (stream_class->event_classes) {
510 BUF_APPEND(", %sevent-class-count=%u",
511 PRFIELD(stream_class->event_classes->len));
512 }
513
5cd6d0e5 514 BUF_APPEND(", %spacket-context-fc-addr=%p, "
83ebb7f1 515 "%sevent-common-context-fc-addr=%p",
5cd6d0e5 516 PRFIELD(stream_class->packet_context_fc),
5cd6d0e5 517 PRFIELD(stream_class->event_common_context_fc));
862ca4ed
PP
518 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
519 if (!trace_class) {
476ef981
PP
520 return;
521 }
522
7fe92073 523 BUF_APPEND(", %sassigns-auto-ec-id=%d, %sassigns-auto-stream-id=%d, "
26fc5aed
PP
524 "%ssupports-packets=%d, "
525 "%spackets-have-begin-default-cs=%d, "
526 "%spackets-have-end-default-cs=%d, "
2e90378a
PP
527 "%ssupports-discarded-events=%d, "
528 "%sdiscarded-events-have-default-cs=%d, "
529 "%ssupports-discarded-packets=%d, "
530 "%sdiscarded-packets-have-default-cs=%d",
44c440bc 531 PRFIELD(stream_class->assigns_automatic_event_class_id),
7fe92073 532 PRFIELD(stream_class->assigns_automatic_stream_id),
26fc5aed 533 PRFIELD(stream_class->supports_packets),
9b24b6aa
PP
534 PRFIELD(stream_class->packets_have_beginning_default_clock_snapshot),
535 PRFIELD(stream_class->packets_have_end_default_clock_snapshot),
2e90378a
PP
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));
862ca4ed
PP
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);
5c563278 543 SET_TMP_PREFIX("pcf-pool-");
9a6502b6 544 format_object_pool(buf_ch, extended, tmp_prefix,
5c563278 545 &stream_class->packet_context_field_pool);
3dca2276
PP
546}
547
cb6f1f7d 548static inline void format_event_class(char **buf_ch, bool extended,
862ca4ed 549 const char *prefix, const struct bt_event_class *event_class)
3dca2276 550{
862ca4ed
PP
551 const struct bt_stream_class *stream_class;
552 const struct bt_trace_class *trace_class;
879e14e8 553 char tmp_prefix[TMP_PREFIX_LEN];
476ef981 554
44c440bc 555 BUF_APPEND(", %sid=%" PRIu64, PRFIELD(event_class->id));
476ef981 556
44c440bc 557 if (event_class->name.value) {
cb6f1f7d 558 BUF_APPEND(", %sname=\"%s\"",
44c440bc 559 PRFIELD(event_class->name.value));
476ef981
PP
560 }
561
562 if (!extended) {
563 return;
564 }
565
44c440bc
PP
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 }
476ef981 573
44c440bc 574 if (event_class->emf_uri.value) {
476ef981 575 BUF_APPEND(", %semf-uri=\"%s\"",
44c440bc 576 PRFIELD(event_class->emf_uri.value));
476ef981
PP
577 }
578
5cd6d0e5
PP
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));
476ef981 582
862ca4ed 583 stream_class = bt_event_class_borrow_stream_class_const(event_class);
476ef981
PP
584 if (!stream_class) {
585 return;
586 }
587
588 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class));
589 SET_TMP_PREFIX("stream-class-");
cb6f1f7d 590 format_stream_class(buf_ch, false, tmp_prefix, stream_class);
862ca4ed
PP
591 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
592 if (!trace_class) {
476ef981
PP
593 return;
594 }
595
862ca4ed
PP
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);
312c056a 599 SET_TMP_PREFIX("event-pool-");
9a6502b6
PP
600 format_object_pool(buf_ch, extended, tmp_prefix,
601 &event_class->event_pool);
3dca2276
PP
602}
603
cb6f1f7d 604static inline void format_stream(char **buf_ch, bool extended,
862ca4ed 605 const char *prefix, const struct bt_stream *stream)
3dca2276 606{
862ca4ed
PP
607 const struct bt_stream_class *stream_class;
608 const struct bt_trace_class *trace_class = NULL;
609 const struct bt_trace *trace = NULL;
879e14e8 610 char tmp_prefix[TMP_PREFIX_LEN];
476ef981 611
44c440bc 612 BUF_APPEND(", %sid=%" PRIu64, PRFIELD(stream->id));
476ef981 613
44c440bc
PP
614 if (stream->name.value) {
615 BUF_APPEND(", %sname=\"%s\"", PRFIELD(stream->name.value));
476ef981
PP
616 }
617
476ef981
PP
618 if (!extended) {
619 return;
620 }
621
862ca4ed
PP
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);
476ef981
PP
628 }
629
862ca4ed
PP
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);
476ef981
PP
641 }
642
312c056a 643 SET_TMP_PREFIX("packet-pool-");
9a6502b6 644 format_object_pool(buf_ch, extended, tmp_prefix, &stream->packet_pool);
3dca2276
PP
645}
646
476ef981 647static inline void format_packet(char **buf_ch, bool extended,
862ca4ed 648 const char *prefix, const struct bt_packet *packet)
476ef981 649{
862ca4ed
PP
650 const struct bt_stream *stream;
651 const struct bt_trace_class *trace_class;
879e14e8 652 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
653
654 if (!extended) {
655 return;
656 }
657
83ebb7f1 658 BUF_APPEND(", %sis-frozen=%d, %scontext-field-addr=%p",
476ef981 659 PRFIELD(packet->frozen),
44c440bc 660 PRFIELD(packet->context_field ? packet->context_field->field : NULL));
862ca4ed 661 stream = bt_packet_borrow_stream_const(packet);
476ef981
PP
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);
862ca4ed
PP
669 trace_class = (const struct bt_trace_class *) bt_object_borrow_parent(&stream->base);
670 if (!trace_class) {
476ef981
PP
671 return;
672 }
673
862ca4ed
PP
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);
476ef981
PP
677}
678
cb6f1f7d 679static inline void format_event(char **buf_ch, bool extended,
862ca4ed 680 const char *prefix, const struct bt_event *event)
476ef981 681{
862ca4ed
PP
682 const struct bt_trace_class *trace_class;
683 const struct bt_stream_class *stream_class;
879e14e8 684 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
685
686 if (!extended) {
687 return;
688 }
689
83ebb7f1 690 BUF_APPEND(", %sis-frozen=%d, "
44c440bc 691 "%scommon-context-field-addr=%p, "
d94d92ac 692 "%sspecific-context-field-addr=%p, "
44c440bc 693 "%spayload-field-addr=%p, ",
476ef981 694 PRFIELD(event->frozen),
44c440bc
PP
695 PRFIELD(event->common_context_field),
696 PRFIELD(event->specific_context_field),
3dca2276
PP
697 PRFIELD(event->payload_field));
698 BUF_APPEND(", %sevent-class-addr=%p", PRFIELD(event->class));
312c056a
PP
699
700 if (!event->class) {
701 return;
702 }
703
476ef981 704 SET_TMP_PREFIX("event-class-");
cb6f1f7d
PP
705 format_event_class(buf_ch, false, tmp_prefix, event->class);
706 stream_class = bt_event_class_borrow_stream_class(event->class);
476ef981
PP
707 if (stream_class) {
708 BUF_APPEND(", %sstream-class-addr=%p", PRFIELD(stream_class));
709 SET_TMP_PREFIX("stream-class-");
cb6f1f7d 710 format_stream_class(buf_ch, false, tmp_prefix,
3dca2276 711 stream_class);
476ef981 712
862ca4ed
PP
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);
476ef981
PP
721 }
722 }
3dca2276 723
26fc5aed
PP
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);
476ef981
PP
728 }
729
26fc5aed
PP
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);
476ef981 734 }
476ef981
PP
735}
736
737static inline void format_clock_class(char **buf_ch, bool extended,
862ca4ed 738 const char *prefix, const struct bt_clock_class *clock_class)
476ef981 739{
879e14e8 740 char tmp_prefix[TMP_PREFIX_LEN];
312c056a 741
44c440bc
PP
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));
476ef981
PP
747
748 if (!extended) {
749 return;
750 }
751
44c440bc
PP
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);
476ef981
PP
759 }
760
761 BUF_APPEND(", %sis-frozen=%d, %sprecision=%" PRIu64 ", "
762 "%soffset-s=%" PRId64 ", "
5552377a 763 "%soffset-cycles=%" PRIu64 ", %sorigin-is-unix-epoch=%d, "
44c440bc 764 "%sbase-offset-ns=%" PRId64,
476ef981 765 PRFIELD(clock_class->frozen), PRFIELD(clock_class->precision),
44c440bc
PP
766 PRFIELD(clock_class->offset_seconds),
767 PRFIELD(clock_class->offset_cycles),
5552377a 768 PRFIELD(clock_class->origin_is_unix_epoch),
44c440bc 769 PRFIELD(clock_class->base_offset.value_ns));
312c056a 770
605e1019 771 SET_TMP_PREFIX("cs-pool-");
9a6502b6
PP
772 format_object_pool(buf_ch, extended, tmp_prefix,
773 &clock_class->cs_pool);
476ef981
PP
774}
775
605e1019
PP
776static inline void format_clock_snapshot(char **buf_ch, bool extended,
777 const char *prefix, const struct bt_clock_snapshot *clock_snapshot)
476ef981 778{
879e14e8 779 char tmp_prefix[TMP_PREFIX_LEN];
44c440bc 780 BUF_APPEND(", %svalue=%" PRIu64 ", %sns-from-origin=%" PRId64,
605e1019
PP
781 PRFIELD(clock_snapshot->value_cycles),
782 PRFIELD(clock_snapshot->ns_from_origin));
476ef981
PP
783
784 if (!extended) {
785 return;
786 }
787
605e1019 788 BUF_APPEND(", %sis-set=%d", PRFIELD(clock_snapshot->is_set));
d94d92ac 789
605e1019 790 if (clock_snapshot->clock_class) {
d94d92ac 791 BUF_APPEND(", %sclock-class-addr=%p",
605e1019 792 PRFIELD(clock_snapshot->clock_class));
d94d92ac
PP
793 SET_TMP_PREFIX("clock-class-");
794 format_clock_class(buf_ch, false, tmp_prefix,
605e1019 795 clock_snapshot->clock_class);
d94d92ac 796 }
476ef981
PP
797}
798
799static inline void format_value(char **buf_ch, bool extended,
862ca4ed 800 const char *prefix, const struct bt_value *value)
476ef981
PP
801{
802 BUF_APPEND(", %stype=%s",
da91b29a 803 PRFIELD(bt_common_value_type_string(bt_value_get_type(value))));
476ef981
PP
804
805 if (!extended) {
806 return;
807 }
808
809 switch (bt_value_get_type(value)) {
810 case BT_VALUE_TYPE_BOOL:
811 {
601b0d3c 812 bt_bool val = bt_value_bool_get(value);
476ef981 813
476ef981
PP
814 BUF_APPEND(", %svalue=%d", PRFIELD(val));
815 break;
816 }
fdd3a2da 817 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
476ef981 818 {
fdd3a2da
PP
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)));
476ef981
PP
827 break;
828 }
a373bf69 829 case BT_VALUE_TYPE_REAL:
476ef981 830 {
601b0d3c 831 double val = bt_value_real_get(value);
476ef981 832
476ef981
PP
833 BUF_APPEND(", %svalue=%f", PRFIELD(val));
834 break;
835 }
836 case BT_VALUE_TYPE_STRING:
837 {
601b0d3c 838 const char *val = bt_value_string_get(value);
476ef981 839
476ef981
PP
840 BUF_APPEND(", %spartial-value=\"%.32s\"", PRFIELD(val));
841 break;
842 }
843 case BT_VALUE_TYPE_ARRAY:
844 {
07208d85 845 int64_t count = bt_value_array_get_size(value);
476ef981 846
f6ccaed9 847 BT_ASSERT(count >= 0);
476ef981
PP
848 BUF_APPEND(", %selement-count=%" PRId64, PRFIELD(count));
849 break;
850 }
851 case BT_VALUE_TYPE_MAP:
852 {
07208d85 853 int64_t count = bt_value_map_get_size(value);
476ef981 854
f6ccaed9 855 BT_ASSERT(count >= 0);
476ef981
PP
856 BUF_APPEND(", %selement-count=%" PRId64, PRFIELD(count));
857 break;
858 }
859 default:
860 break;
861 }
862}
863
d6e69534
PP
864static inline void format_message(char **buf_ch, bool extended,
865 const char *prefix, const struct bt_message *msg)
476ef981 866{
879e14e8 867 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
868
869 BUF_APPEND(", %stype=%s",
d6e69534 870 PRFIELD(bt_message_type_string(msg->type)));
476ef981
PP
871
872 if (!extended) {
873 return;
874 }
875
5c563278 876 BUF_APPEND(", %sis-frozen=%d, %sgraph-addr=%p",
d6e69534 877 PRFIELD(msg->frozen), PRFIELD(msg->graph));
476ef981 878
d6e69534
PP
879 switch (msg->type) {
880 case BT_MESSAGE_TYPE_EVENT:
476ef981 881 {
d6e69534
PP
882 const struct bt_message_event *msg_event =
883 (const void *) msg;
476ef981 884
d6e69534 885 if (msg_event->event) {
d94d92ac 886 SET_TMP_PREFIX("event-");
5df26c89
PP
887 format_event(buf_ch, true, tmp_prefix,
888 msg_event->event);
d94d92ac
PP
889 }
890
8fc0367b
PP
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
476ef981
PP
897 break;
898 }
d6e69534 899 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
5df26c89 900 case BT_MESSAGE_TYPE_STREAM_END:
476ef981 901 {
5df26c89 902 const struct bt_message_stream *msg_stream = (const void *) msg;
476ef981 903
d6e69534 904 if (msg_stream->stream) {
d94d92ac 905 SET_TMP_PREFIX("stream-");
5df26c89
PP
906 format_stream(buf_ch, true, tmp_prefix,
907 msg_stream->stream);
d94d92ac
PP
908 }
909
5df26c89 910 BUF_APPEND(", %sdefault-cs-state=%s",
188edac1
SM
911 PRFIELD(bt_message_stream_clock_snapshot_state_string(
912 msg_stream->default_cs_state)));
476ef981 913
188edac1 914 if (msg_stream->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
5df26c89
PP
915 SET_TMP_PREFIX("default-cs-");
916 format_clock_snapshot(buf_ch, true, tmp_prefix,
188edac1 917 msg_stream->default_cs);
d94d92ac
PP
918 }
919
476ef981
PP
920 break;
921 }
5df26c89 922 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
d6e69534 923 case BT_MESSAGE_TYPE_PACKET_END:
476ef981 924 {
5df26c89 925 const struct bt_message_packet *msg_packet = (const void *) msg;
476ef981 926
d6e69534 927 if (msg_packet->packet) {
d94d92ac 928 SET_TMP_PREFIX("packet-");
5df26c89
PP
929 format_packet(buf_ch, true, tmp_prefix,
930 msg_packet->packet);
d94d92ac
PP
931 }
932
8fc0367b
PP
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
476ef981
PP
939 break;
940 }
4c833281 941 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
4237f1f2 942 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
4c833281
PP
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) {
26fc5aed 954 SET_TMP_PREFIX("begin-default-cs-");
4c833281
PP
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) {
26fc5aed 960 SET_TMP_PREFIX("end-default-cs-");
4c833281
PP
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 }
476ef981
PP
972 default:
973 break;
974 }
975}
976
977static inline void format_plugin_so_shared_lib_handle(char **buf_ch,
978 const char *prefix,
862ca4ed 979 const struct bt_plugin_so_shared_lib_handle *handle)
476ef981
PP
980{
981 BUF_APPEND(", %saddr=%p", PRFIELD(handle));
982
983 if (handle->path) {
d94d92ac 984 BUF_APPEND(", %spath=\"%s\"", PRFIELD_GSTRING(handle->path));
476ef981
PP
985 }
986}
987
988static inline void format_component_class(char **buf_ch, bool extended,
862ca4ed
PP
989 const char *prefix,
990 const struct bt_component_class *comp_class)
476ef981 991{
879e14e8 992 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
993
994 BUF_APPEND(", %stype=%s, %sname=\"%s\"",
995 PRFIELD(bt_component_class_type_string(comp_class->type)),
d94d92ac 996 PRFIELD_GSTRING(comp_class->name));
476ef981
PP
997
998 if (comp_class->description) {
999 BUF_APPEND(", %spartial-descr=\"%.32s\"",
d94d92ac 1000 PRFIELD_GSTRING(comp_class->description));
476ef981
PP
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
1016static inline void format_component(char **buf_ch, bool extended,
862ca4ed 1017 const char *prefix, const struct bt_component *component)
476ef981 1018{
879e14e8 1019 char tmp_prefix[TMP_PREFIX_LEN];
476ef981 1020
e874da19
PP
1021 BUF_APPEND(", %sname=\"%s\", %slog-level=%s",
1022 PRFIELD_GSTRING(component->name),
1023 PRFIELD(bt_common_logging_level_string(component->log_level)));
d94d92ac
PP
1024
1025 if (component->class) {
1026 SET_TMP_PREFIX("class-");
1027 format_component_class(buf_ch, extended, tmp_prefix,
1028 component->class);
1029 }
476ef981
PP
1030
1031 if (!extended) {
1032 return;
1033 }
1034
312c056a
PP
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 }
476ef981
PP
1044}
1045
1046static inline void format_port(char **buf_ch, bool extended,
862ca4ed 1047 const char *prefix, const struct bt_port *port)
476ef981 1048{
879e14e8 1049 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
1050
1051 BUF_APPEND(", %stype=%s, %sname=\"%s\"",
1052 PRFIELD(bt_port_type_string(port->type)),
d94d92ac 1053 PRFIELD_GSTRING(port->name));
476ef981
PP
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
1065static inline void format_connection(char **buf_ch, bool extended,
862ca4ed 1066 const char *prefix, const struct bt_connection *connection)
476ef981 1067{
879e14e8 1068 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
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
1087static inline void format_graph(char **buf_ch, bool extended,
862ca4ed 1088 const char *prefix, const struct bt_graph *graph)
476ef981 1089{
879e14e8 1090 char tmp_prefix[TMP_PREFIX_LEN];
5c563278 1091
5badd463
PP
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)));
476ef981
PP
1097
1098 if (!extended) {
1099 return;
1100 }
1101
312c056a
PP
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 }
5c563278
PP
1111
1112 SET_TMP_PREFIX("en-pool-");
9a6502b6 1113 format_object_pool(buf_ch, extended, tmp_prefix,
d6e69534 1114 &graph->event_msg_pool);
5c563278 1115 SET_TMP_PREFIX("pbn-pool-");
9a6502b6 1116 format_object_pool(buf_ch, extended, tmp_prefix,
d6e69534 1117 &graph->packet_begin_msg_pool);
5c563278 1118 SET_TMP_PREFIX("pen-pool-");
9a6502b6 1119 format_object_pool(buf_ch, extended, tmp_prefix,
d6e69534 1120 &graph->packet_end_msg_pool);
476ef981
PP
1121}
1122
d6e69534 1123static inline void format_message_iterator(char **buf_ch,
476ef981 1124 bool extended, const char *prefix,
d6e69534 1125 const struct bt_message_iterator *iterator)
476ef981
PP
1126{
1127 const char *type;
879e14e8 1128 char tmp_prefix[TMP_PREFIX_LEN];
476ef981 1129
d6e69534
PP
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";
476ef981
PP
1134 } else {
1135 type = "(unknown)";
1136 }
1137
1138 BUF_APPEND(", %stype=%s", PRFIELD(type));
1139
1140 switch (iterator->type) {
d6e69534 1141 case BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT:
476ef981 1142 {
d6e69534 1143 const struct bt_self_component_port_input_message_iterator *
862ca4ed 1144 port_in_iter = (const void *) iterator;
476ef981 1145
d94d92ac 1146 if (port_in_iter->upstream_component) {
476ef981
PP
1147 SET_TMP_PREFIX("upstream-comp-");
1148 format_component(buf_ch, false, tmp_prefix,
d94d92ac 1149 port_in_iter->upstream_component);
476ef981
PP
1150 }
1151
d94d92ac 1152 if (port_in_iter->upstream_port) {
476ef981
PP
1153 SET_TMP_PREFIX("upstream-port-");
1154 format_port(buf_ch, false, tmp_prefix,
d94d92ac 1155 port_in_iter->upstream_port);
476ef981
PP
1156 }
1157
d94d92ac 1158 if (port_in_iter->connection) {
476ef981
PP
1159 SET_TMP_PREFIX("upstream-conn-");
1160 format_connection(buf_ch, false, tmp_prefix,
d94d92ac 1161 port_in_iter->connection);
476ef981
PP
1162 }
1163 break;
1164 }
d6e69534 1165 case BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT:
476ef981 1166 {
d6e69534 1167 const struct bt_port_output_message_iterator *port_out_iter =
862ca4ed 1168 (const void *) iterator;
476ef981 1169
d94d92ac
PP
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
476ef981
PP
1182 break;
1183 }
1184 default:
1185 break;
1186 }
1187}
1188
1189static inline void format_plugin(char **buf_ch, bool extended,
92fed4e1 1190 const char *prefix, const struct bt_plugin *plugin)
476ef981 1191{
879e14e8 1192 char tmp_prefix[TMP_PREFIX_LEN];
476ef981
PP
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\"",
d94d92ac 1198 PRFIELD_GSTRING(plugin->info.path));
476ef981
PP
1199 }
1200
1201 if (plugin->info.name_set) {
1202 BUF_APPEND(", %sname=\"%s\"",
d94d92ac 1203 PRFIELD_GSTRING(plugin->info.name));
476ef981
PP
1204 }
1205
1206 if (!extended) {
1207 return;
1208 }
1209
1210 if (plugin->info.author_set) {
1211 BUF_APPEND(", %sauthor=\"%s\"",
d94d92ac 1212 PRFIELD_GSTRING(plugin->info.author));
476ef981
PP
1213 }
1214
1215 if (plugin->info.license_set) {
1216 BUF_APPEND(", %slicense=\"%s\"",
d94d92ac 1217 PRFIELD_GSTRING(plugin->info.license));
476ef981
PP
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
d94d92ac
PP
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));
476ef981
PP
1234
1235 if (plugin->spec_data) {
862ca4ed
PP
1236 const struct bt_plugin_so_spec_data *spec_data =
1237 (const void *) plugin->spec_data;
476ef981
PP
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
553c4bab
PP
1247static 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
476ef981
PP
1307static 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;
862ca4ed 1315 const void *obj;
476ef981
PP
1316
1317 /* skip "%!" */
1318 fmt_ch += 2;
1319
44c440bc
PP
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
476ef981
PP
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
cb6f1f7d
PP
1358 switch (*fmt_ch) {
1359 case 'F':
5cd6d0e5 1360 format_field_class(buf_ch, extended, prefix, obj);
476ef981 1361 break;
cb6f1f7d
PP
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);
476ef981 1376 break;
cb6f1f7d
PP
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;
862ca4ed
PP
1386 case 'T':
1387 format_trace_class(buf_ch, extended, prefix, obj);
1388 break;
cb6f1f7d
PP
1389 case 'K':
1390 format_clock_class(buf_ch, extended, prefix, obj);
1391 break;
1392 case 'k':
605e1019 1393 format_clock_snapshot(buf_ch, extended, prefix, obj);
cb6f1f7d
PP
1394 break;
1395 case 'v':
1396 format_value(buf_ch, extended, prefix, obj);
1397 break;
1398 case 'n':
d6e69534 1399 format_message(buf_ch, extended, prefix, obj);
cb6f1f7d
PP
1400 break;
1401 case 'i':
d6e69534 1402 format_message_iterator(buf_ch, extended, prefix, obj);
cb6f1f7d
PP
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;
44c440bc 1416 case 'l':
cb6f1f7d
PP
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;
553c4bab
PP
1428 case 'r':
1429 format_error_cause(buf_ch, extended, prefix, obj);
1430 break;
cb6f1f7d
PP
1431 default:
1432 abort();
476ef981
PP
1433 }
1434
1435update_fmt:
1436 fmt_ch++;
1437 *out_fmt_ch = fmt_ch;
1438}
1439
476ef981
PP
1440void 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
f6ccaed9 1445 BT_ASSERT(fmt);
476ef981
PP
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}
3cd4c495
PP
1452
1453void 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.108039 seconds and 4 git commands to generate.