Move to kernel style SPDX license identifiers
[babeltrace.git] / src / plugins / text / pretty / print.c
CommitLineData
af9a82eb 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
af9a82eb 4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
f504043c 5 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
af9a82eb
JG
6 */
7
3fadfbc0 8#include <babeltrace2/babeltrace.h>
578e048b
MJ
9#include "compat/bitfield.h"
10#include "common/common.h"
6162e6b7 11#include "common/uuid.h"
578e048b
MJ
12#include "compat/time.h"
13#include "common/assert.h"
6a18b281 14#include <inttypes.h>
93a4161c 15#include <ctype.h>
c4f23e30 16#include <stdbool.h>
91d81473 17#include <string.h>
3228cc1d 18#include "pretty.h"
af9a82eb 19
1556a1af
JG
20#define NSEC_PER_SEC 1000000000LL
21
04609487
PP
22static char color_name[32];
23static char color_field_name[32];
24static char color_rst[32];
25static char color_string_value[32];
26static char color_number_value[32];
27static char color_enum_mapping_name[32];
28static char color_unknown[32];
29static char color_event_name[32];
30static char color_timestamp[32];
ad96d936 31
af9a82eb
JG
32struct timestamp {
33 int64_t real_timestamp; /* Relative to UNIX epoch. */
605e1019 34 uint64_t clock_snapshot; /* In cycles. */
af9a82eb
JG
35};
36
6a18b281 37static
d94d92ac 38int print_field(struct pretty_component *pretty,
2f84c7be 39 const bt_field *field, bool print_names);
6a18b281 40
ad96d936 41static
3228cc1d 42void print_name_equal(struct pretty_component *pretty, const char *name)
ad96d936 43{
3228cc1d 44 if (pretty->use_colors) {
04609487 45 bt_common_g_string_append(pretty->string, color_name);
dd61c4ed 46 bt_common_g_string_append(pretty->string, name);
04609487 47 bt_common_g_string_append(pretty->string, color_rst);
ad96d936 48 } else {
dd61c4ed 49 bt_common_g_string_append(pretty->string, name);
ad96d936 50 }
dd61c4ed 51 bt_common_g_string_append(pretty->string, " = ");
ad96d936
PP
52}
53
54static
3228cc1d 55void print_field_name_equal(struct pretty_component *pretty, const char *name)
ad96d936 56{
3228cc1d 57 if (pretty->use_colors) {
04609487 58 bt_common_g_string_append(pretty->string, color_field_name);
dd61c4ed 59 bt_common_g_string_append(pretty->string, name);
04609487 60 bt_common_g_string_append(pretty->string, color_rst);
ad96d936 61 } else {
dd61c4ed
MD
62 bt_common_g_string_append(pretty->string, name);
63 }
64 bt_common_g_string_append(pretty->string, " = ");
ad96d936
PP
65}
66
af9a82eb 67static
3228cc1d 68void print_timestamp_cycles(struct pretty_component *pretty,
9c202476 69 const bt_clock_snapshot *clock_snapshot, bool update_last)
af9a82eb 70{
1556a1af 71 uint64_t cycles;
1556a1af 72
605e1019 73 cycles = bt_clock_snapshot_get_value(clock_snapshot);
f354b1e1 74 bt_common_g_string_append_printf(pretty->string, "%020" PRIu64, cycles);
3af83b5a 75
9c202476
PP
76 if (update_last) {
77 if (pretty->last_cycles_timestamp != -1ULL) {
78 pretty->delta_cycles = cycles - pretty->last_cycles_timestamp;
79 }
2c091c04 80
9c202476
PP
81 pretty->last_cycles_timestamp = cycles;
82 }
af9a82eb
JG
83}
84
85static
3228cc1d 86void print_timestamp_wall(struct pretty_component *pretty,
9c202476 87 const bt_clock_snapshot *clock_snapshot, bool update_last)
af9a82eb 88{
1556a1af 89 int ret;
1556a1af
JG
90 int64_t ts_nsec = 0; /* add configurable offset */
91 int64_t ts_sec = 0; /* add configurable offset */
92 uint64_t ts_sec_abs, ts_nsec_abs;
93 bool is_negative;
af9a82eb 94
605e1019 95 if (!clock_snapshot) {
dd61c4ed 96 bt_common_g_string_append(pretty->string, "??:??:??.?????????");
1556a1af
JG
97 return;
98 }
99
605e1019 100 ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, &ts_nsec);
1556a1af 101 if (ret) {
5280f742 102 // TODO: log, this is unexpected
dd61c4ed 103 bt_common_g_string_append(pretty->string, "Error");
1556a1af
JG
104 return;
105 }
106
9c202476
PP
107 if (update_last) {
108 if (pretty->last_real_timestamp != -1ULL) {
109 pretty->delta_real_timestamp = ts_nsec - pretty->last_real_timestamp;
110 }
111
112 pretty->last_real_timestamp = ts_nsec;
3af83b5a 113 }
3af83b5a 114
1556a1af
JG
115 ts_sec += ts_nsec / NSEC_PER_SEC;
116 ts_nsec = ts_nsec % NSEC_PER_SEC;
5280f742 117
1556a1af
JG
118 if (ts_sec >= 0 && ts_nsec >= 0) {
119 is_negative = false;
120 ts_sec_abs = ts_sec;
121 ts_nsec_abs = ts_nsec;
122 } else if (ts_sec > 0 && ts_nsec < 0) {
123 is_negative = false;
124 ts_sec_abs = ts_sec - 1;
125 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
126 } else if (ts_sec == 0 && ts_nsec < 0) {
127 is_negative = true;
128 ts_sec_abs = ts_sec;
129 ts_nsec_abs = -ts_nsec;
130 } else if (ts_sec < 0 && ts_nsec > 0) {
131 is_negative = true;
132 ts_sec_abs = -(ts_sec + 1);
133 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
134 } else if (ts_sec < 0 && ts_nsec == 0) {
135 is_negative = true;
136 ts_sec_abs = -ts_sec;
137 ts_nsec_abs = ts_nsec;
138 } else { /* (ts_sec < 0 && ts_nsec < 0) */
139 is_negative = true;
140 ts_sec_abs = -ts_sec;
141 ts_nsec_abs = -ts_nsec;
142 }
143
3228cc1d 144 if (!pretty->options.clock_seconds) {
1556a1af
JG
145 struct tm tm;
146 time_t time_s = (time_t) ts_sec_abs;
147
18adbd19 148 if (is_negative && !pretty->negative_timestamp_warning_done) {
5280f742 149 // TODO: log instead
1556a1af 150 fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
18adbd19 151 pretty->negative_timestamp_warning_done = true;
1556a1af
JG
152 goto seconds;
153 }
154
3228cc1d 155 if (!pretty->options.clock_gmt) {
1556a1af
JG
156 struct tm *res;
157
58a2480d 158 res = bt_localtime_r(&time_s, &tm);
1556a1af 159 if (!res) {
5280f742 160 // TODO: log instead
1556a1af
JG
161 fprintf(stderr, "[warning] Unable to get localtime.\n");
162 goto seconds;
163 }
164 } else {
165 struct tm *res;
166
58a2480d 167 res = bt_gmtime_r(&time_s, &tm);
1556a1af 168 if (!res) {
5280f742 169 // TODO: log instead
1556a1af
JG
170 fprintf(stderr, "[warning] Unable to get gmtime.\n");
171 goto seconds;
172 }
173 }
3228cc1d 174 if (pretty->options.clock_date) {
1556a1af
JG
175 char timestr[26];
176 size_t res;
177
178 /* Print date and time */
179 res = strftime(timestr, sizeof(timestr),
4d2a94f1 180 "%Y-%m-%d ", &tm);
1556a1af 181 if (!res) {
5280f742 182 // TODO: log instead
1556a1af
JG
183 fprintf(stderr, "[warning] Unable to print ascii time.\n");
184 goto seconds;
185 }
5280f742 186
dd61c4ed 187 bt_common_g_string_append(pretty->string, timestr);
1556a1af 188 }
5280f742 189
1556a1af 190 /* Print time in HH:MM:SS.ns */
f354b1e1 191 bt_common_g_string_append_printf(pretty->string,
5280f742
PP
192 "%02d:%02d:%02d.%09" PRIu64, tm.tm_hour, tm.tm_min,
193 tm.tm_sec, ts_nsec_abs);
1556a1af
JG
194 goto end;
195 }
196seconds:
f354b1e1 197 bt_common_g_string_append_printf(pretty->string, "%s%" PRId64 ".%09" PRIu64,
5280f742 198 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
1556a1af
JG
199end:
200 return;
af9a82eb
JG
201}
202
203static
d94d92ac 204int print_event_timestamp(struct pretty_component *pretty,
2c091c04 205 const bt_message *event_msg, bool *start_line)
af9a82eb 206{
3228cc1d 207 bool print_names = pretty->options.print_header_field_names;
d94d92ac 208 int ret = 0;
605e1019 209 const bt_clock_snapshot *clock_snapshot = NULL;
af9a82eb 210
81c25620
PP
211 if (!bt_message_event_borrow_stream_class_default_clock_class_const(
212 event_msg)) {
2c091c04 213 /* No default clock class: skip the timestamp without an error */
f504043c
MD
214 goto end;
215 }
d9f65f09 216
0cbc2c33 217 clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(event_msg);
af9a82eb 218
ad96d936 219 if (print_names) {
3228cc1d 220 print_name_equal(pretty, "timestamp");
ad96d936 221 } else {
dd61c4ed 222 bt_common_g_string_append(pretty->string, "[");
ad96d936 223 }
3228cc1d 224 if (pretty->use_colors) {
04609487 225 bt_common_g_string_append(pretty->string, color_timestamp);
ad96d936 226 }
3228cc1d 227 if (pretty->options.print_timestamp_cycles) {
9c202476 228 print_timestamp_cycles(pretty, clock_snapshot, true);
af9a82eb 229 } else {
9c202476 230 print_timestamp_wall(pretty, clock_snapshot, true);
af9a82eb 231 }
3228cc1d 232 if (pretty->use_colors) {
04609487 233 bt_common_g_string_append(pretty->string, color_rst);
ad96d936 234 }
af9a82eb 235
c3c30b08 236 if (!print_names)
dd61c4ed 237 bt_common_g_string_append(pretty->string, "] ");
c3c30b08 238
3228cc1d 239 if (pretty->options.print_delta_field) {
ad96d936 240 if (print_names) {
dd61c4ed 241 bt_common_g_string_append(pretty->string, ", ");
3228cc1d 242 print_name_equal(pretty, "delta");
ad96d936 243 } else {
dd61c4ed 244 bt_common_g_string_append(pretty->string, "(");
ad96d936 245 }
3228cc1d
PP
246 if (pretty->options.print_timestamp_cycles) {
247 if (pretty->delta_cycles == -1ULL) {
dd61c4ed 248 bt_common_g_string_append(pretty->string,
c6a871e9 249 "+??????????\?\?"); /* Not a trigraph. */
3af83b5a 250 } else {
f354b1e1 251 bt_common_g_string_append_printf(pretty->string,
5280f742 252 "+%012" PRIu64, pretty->delta_cycles);
3af83b5a
MD
253 }
254 } else {
3228cc1d 255 if (pretty->delta_real_timestamp != -1ULL) {
3af83b5a 256 uint64_t delta_sec, delta_nsec, delta;
f504043c 257
3228cc1d 258 delta = pretty->delta_real_timestamp;
3af83b5a
MD
259 delta_sec = delta / NSEC_PER_SEC;
260 delta_nsec = delta % NSEC_PER_SEC;
f354b1e1 261 bt_common_g_string_append_printf(pretty->string,
5280f742 262 "+%" PRIu64 ".%09" PRIu64,
3af83b5a
MD
263 delta_sec, delta_nsec);
264 } else {
dd61c4ed 265 bt_common_g_string_append(pretty->string, "+?.?????????");
3af83b5a
MD
266 }
267 }
268 if (!print_names) {
dd61c4ed 269 bt_common_g_string_append(pretty->string, ") ");
3af83b5a
MD
270 }
271 }
272 *start_line = !print_names;
f504043c 273
af9a82eb 274end:
af9a82eb
JG
275 return ret;
276}
277
6a18b281 278static
d94d92ac 279int print_event_header(struct pretty_component *pretty,
2c091c04 280 const bt_message *event_msg)
af9a82eb 281{
3228cc1d 282 bool print_names = pretty->options.print_header_field_names;
d94d92ac 283 int ret = 0;
b19ff26f 284 const bt_event_class *event_class = NULL;
b19ff26f
PP
285 const bt_stream *stream = NULL;
286 const bt_trace *trace = NULL;
2c091c04 287 const bt_event *event = bt_message_event_borrow_event_const(event_msg);
64925bba 288 const char *ev_name;
60535549 289 int dom_print = 0;
4cdfc5e8 290 bt_property_availability prop_avail;
af9a82eb 291
40f4ba76 292 event_class = bt_event_borrow_class_const(event);
26fc5aed 293 stream = bt_event_borrow_stream_const(event);
862ca4ed 294 trace = bt_stream_borrow_trace_const(stream);
2c091c04 295 ret = print_event_timestamp(pretty, event_msg, &pretty->start_line);
d94d92ac 296 if (ret) {
af9a82eb
JG
297 goto end;
298 }
3228cc1d 299 if (pretty->options.print_trace_field) {
c3c30b08
MD
300 const char *name;
301
862ca4ed 302 name = bt_trace_get_name(trace);
c3c30b08 303 if (name) {
3228cc1d 304 if (!pretty->start_line) {
dd61c4ed 305 bt_common_g_string_append(pretty->string, ", ");
c3c30b08 306 }
c3c30b08 307 if (print_names) {
3228cc1d 308 print_name_equal(pretty, "trace");
c3c30b08 309 }
5280f742 310
dd61c4ed 311 bt_common_g_string_append(pretty->string, name);
5280f742 312
22b7fdc6 313 if (print_names) {
dd61c4ed 314 bt_common_g_string_append(pretty->string, ", ");
60535549 315 }
c3c30b08
MD
316 }
317 }
3228cc1d 318 if (pretty->options.print_trace_hostname_field) {
b19ff26f 319 const bt_value *hostname_str;
c3c30b08 320
335a2da5
PP
321 hostname_str = bt_trace_borrow_environment_entry_value_by_name_const(
322 trace, "hostname");
c3c30b08
MD
323 if (hostname_str) {
324 const char *str;
325
3228cc1d 326 if (!pretty->start_line) {
dd61c4ed 327 bt_common_g_string_append(pretty->string, ", ");
c3c30b08 328 }
c3c30b08 329 if (print_names) {
3228cc1d 330 print_name_equal(pretty, "trace:hostname");
c3c30b08 331 }
601b0d3c 332 str = bt_value_string_get(hostname_str);
dd61c4ed 333 bt_common_g_string_append(pretty->string, str);
60535549 334 dom_print = 1;
c3c30b08
MD
335 }
336 }
3228cc1d 337 if (pretty->options.print_trace_domain_field) {
b19ff26f 338 const bt_value *domain_str;
c3c30b08 339
335a2da5
PP
340 domain_str = bt_trace_borrow_environment_entry_value_by_name_const(
341 trace, "domain");
c3c30b08
MD
342 if (domain_str) {
343 const char *str;
344
3228cc1d 345 if (!pretty->start_line) {
dd61c4ed 346 bt_common_g_string_append(pretty->string, ", ");
c3c30b08 347 }
c3c30b08 348 if (print_names) {
3228cc1d 349 print_name_equal(pretty, "trace:domain");
60535549 350 } else if (dom_print) {
dd61c4ed 351 bt_common_g_string_append(pretty->string, ":");
c3c30b08 352 }
601b0d3c 353 str = bt_value_string_get(domain_str);
dd61c4ed 354 bt_common_g_string_append(pretty->string, str);
60535549 355 dom_print = 1;
c3c30b08
MD
356 }
357 }
3228cc1d 358 if (pretty->options.print_trace_procname_field) {
b19ff26f 359 const bt_value *procname_str;
c3c30b08 360
335a2da5
PP
361 procname_str = bt_trace_borrow_environment_entry_value_by_name_const(
362 trace, "procname");
c3c30b08
MD
363 if (procname_str) {
364 const char *str;
365
3228cc1d 366 if (!pretty->start_line) {
dd61c4ed 367 bt_common_g_string_append(pretty->string, ", ");
c3c30b08 368 }
c3c30b08 369 if (print_names) {
3228cc1d 370 print_name_equal(pretty, "trace:procname");
60535549 371 } else if (dom_print) {
dd61c4ed 372 bt_common_g_string_append(pretty->string, ":");
c3c30b08 373 }
601b0d3c 374 str = bt_value_string_get(procname_str);
dd61c4ed 375 bt_common_g_string_append(pretty->string, str);
60535549 376 dom_print = 1;
c3c30b08
MD
377 }
378 }
3228cc1d 379 if (pretty->options.print_trace_vpid_field) {
b19ff26f 380 const bt_value *vpid_value;
c3c30b08 381
335a2da5
PP
382 vpid_value = bt_trace_borrow_environment_entry_value_by_name_const(
383 trace, "vpid");
c3c30b08
MD
384 if (vpid_value) {
385 int64_t value;
386
3228cc1d 387 if (!pretty->start_line) {
dd61c4ed 388 bt_common_g_string_append(pretty->string, ", ");
c3c30b08 389 }
c3c30b08 390 if (print_names) {
3228cc1d 391 print_name_equal(pretty, "trace:vpid");
60535549 392 } else if (dom_print) {
dd61c4ed 393 bt_common_g_string_append(pretty->string, ":");
c3c30b08 394 }
9c08c816 395 value = bt_value_integer_signed_get(vpid_value);
f354b1e1 396 bt_common_g_string_append_printf(pretty->string,
601b0d3c 397 "(%" PRId64 ")", value);
60535549 398 dom_print = 1;
c3c30b08
MD
399 }
400 }
3228cc1d 401 if (pretty->options.print_loglevel_field) {
cf76ce92 402 static const char *log_level_names[] = {
50842bdc
PP
403 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY ] = "TRACE_EMERG",
404 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT ] = "TRACE_ALERT",
405 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL ] = "TRACE_CRIT",
406 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR ] = "TRACE_ERR",
407 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING ] = "TRACE_WARNING",
408 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE ] = "TRACE_NOTICE",
409 [ BT_EVENT_CLASS_LOG_LEVEL_INFO ] = "TRACE_INFO",
410 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM ] = "TRACE_DEBUG_SYSTEM",
411 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM ] = "TRACE_DEBUG_PROGRAM",
412 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS ] = "TRACE_DEBUG_PROCESS",
413 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE ] = "TRACE_DEBUG_MODULE",
414 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT ] = "TRACE_DEBUG_UNIT",
415 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION ] = "TRACE_DEBUG_FUNCTION",
416 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE ] = "TRACE_DEBUG_LINE",
417 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG ] = "TRACE_DEBUG",
cf76ce92 418 };
4cdfc5e8 419 bt_event_class_log_level log_level;
cf76ce92
PP
420 const char *log_level_str = NULL;
421
44c440bc
PP
422 prop_avail = bt_event_class_get_log_level(event_class,
423 &log_level);
424 if (prop_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
cf76ce92 425 log_level_str = log_level_names[log_level];
98b15851 426 BT_ASSERT_DBG(log_level_str);
c3c30b08 427
3228cc1d 428 if (!pretty->start_line) {
dd61c4ed 429 bt_common_g_string_append(pretty->string, ", ");
c3c30b08 430 }
c3c30b08 431 if (print_names) {
3228cc1d 432 print_name_equal(pretty, "loglevel");
60535549 433 } else if (dom_print) {
dd61c4ed 434 bt_common_g_string_append(pretty->string, ":");
c3c30b08 435 }
cf76ce92 436
dd61c4ed 437 bt_common_g_string_append(pretty->string, log_level_str);
f354b1e1 438 bt_common_g_string_append_printf(
cf76ce92 439 pretty->string, " (%d)", (int) log_level);
60535549 440 dom_print = 1;
c3c30b08
MD
441 }
442 }
3228cc1d 443 if (pretty->options.print_emf_field) {
cf76ce92 444 const char *uri_str;
c3c30b08 445
50842bdc 446 uri_str = bt_event_class_get_emf_uri(event_class);
c3c30b08 447 if (uri_str) {
3228cc1d 448 if (!pretty->start_line) {
dd61c4ed 449 bt_common_g_string_append(pretty->string, ", ");
c3c30b08 450 }
c3c30b08 451 if (print_names) {
3228cc1d 452 print_name_equal(pretty, "model.emf.uri");
60535549 453 } else if (dom_print) {
dd61c4ed 454 bt_common_g_string_append(pretty->string, ":");
c3c30b08 455 }
c3c30b08 456
dd61c4ed 457 bt_common_g_string_append(pretty->string, uri_str);
60535549 458 dom_print = 1;
c3c30b08
MD
459 }
460 }
60535549 461 if (dom_print && !print_names) {
dd61c4ed 462 bt_common_g_string_append(pretty->string, " ");
60535549 463 }
3228cc1d 464 if (!pretty->start_line) {
dd61c4ed 465 bt_common_g_string_append(pretty->string, ", ");
c3c30b08 466 }
3228cc1d 467 pretty->start_line = true;
6a18b281 468 if (print_names) {
3228cc1d 469 print_name_equal(pretty, "name");
ad96d936 470 }
64925bba 471 ev_name = bt_event_class_get_name(event_class);
3228cc1d 472 if (pretty->use_colors) {
64925bba 473 if (ev_name) {
dd61c4ed 474 bt_common_g_string_append(pretty->string,
04609487 475 color_event_name);
64925bba 476 } else {
dd61c4ed 477 bt_common_g_string_append(pretty->string,
04609487 478 color_unknown);
64925bba
PP
479 }
480 }
481 if (ev_name) {
dd61c4ed 482 bt_common_g_string_append(pretty->string, ev_name);
64925bba 483 } else {
dd61c4ed 484 bt_common_g_string_append(pretty->string, "<unknown>");
6a18b281 485 }
3228cc1d 486 if (pretty->use_colors) {
04609487 487 bt_common_g_string_append(pretty->string, color_rst);
ad96d936 488 }
60535549 489 if (!print_names) {
dd61c4ed 490 bt_common_g_string_append(pretty->string, ": ");
60535549 491 } else {
dd61c4ed 492 bt_common_g_string_append(pretty->string, ", ");
60535549 493 }
a82e90e8 494
af9a82eb 495end:
6a18b281
MD
496 return ret;
497}
498
499static
d94d92ac 500int print_integer(struct pretty_component *pretty,
b19ff26f 501 const bt_field *field)
6a18b281 502{
d94d92ac 503 int ret = 0;
4cdfc5e8 504 bt_field_class_integer_preferred_display_base base;
b19ff26f 505 const bt_field_class *int_fc;
6a18b281
MD
506 union {
507 uint64_t u;
508 int64_t s;
509 } v;
ad96d936 510 bool rst_color = false;
4cdfc5e8 511 bt_field_class_type ft_type;
6a18b281 512
40f4ba76 513 int_fc = bt_field_borrow_class_const(field);
98b15851 514 BT_ASSERT_DBG(int_fc);
864cad70 515 ft_type = bt_field_get_class_type(field);
ebdb6693
PP
516 if (bt_field_class_type_is(ft_type,
517 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
9c08c816 518 v.u = bt_field_integer_unsigned_get_value(field);
6a18b281 519 } else {
9c08c816 520 v.s = bt_field_integer_signed_get_value(field);
6a18b281
MD
521 }
522
3228cc1d 523 if (pretty->use_colors) {
04609487 524 bt_common_g_string_append(pretty->string, color_number_value);
ad96d936
PP
525 rst_color = true;
526 }
527
5cd6d0e5 528 base = bt_field_class_integer_get_preferred_display_base(int_fc);
6a18b281 529 switch (base) {
5cd6d0e5 530 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
6a18b281
MD
531 {
532 int bitnr, len;
533
5cd6d0e5 534 len = bt_field_class_integer_get_field_value_range(int_fc);
dd61c4ed 535 bt_common_g_string_append(pretty->string, "0b");
7f140ae8 536 _bt_safe_lshift(v.u, 64 - len);
6a18b281 537 for (bitnr = 0; bitnr < len; bitnr++) {
242ed509
MD
538 bt_common_g_string_append_c(pretty->string,
539 (v.u & (1ULL << 63)) ? '1' : '0');
7f140ae8 540 _bt_safe_lshift(v.u, 1);
6a18b281
MD
541 }
542 break;
543 }
5cd6d0e5 544 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
6a18b281 545 {
ebdb6693
PP
546 if (bt_field_class_type_is(ft_type,
547 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER)) {
6a18b281
MD
548 int len;
549
5cd6d0e5
PP
550 len = bt_field_class_integer_get_field_value_range(
551 int_fc);
6a18b281
MD
552 if (len < 64) {
553 size_t rounded_len;
554
98b15851 555 BT_ASSERT_DBG(len != 0);
6a18b281
MD
556 /* Round length to the nearest 3-bit */
557 rounded_len = (((len - 1) / 3) + 1) * 3;
558 v.u &= ((uint64_t) 1 << rounded_len) - 1;
559 }
560 }
561
f354b1e1 562 bt_common_g_string_append_printf(pretty->string, "0%" PRIo64, v.u);
6a18b281
MD
563 break;
564 }
5cd6d0e5 565 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
ebdb6693
PP
566 if (bt_field_class_type_is(ft_type,
567 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
f354b1e1 568 bt_common_g_string_append_printf(pretty->string, "%" PRIu64, v.u);
6a18b281 569 } else {
f354b1e1 570 bt_common_g_string_append_printf(pretty->string, "%" PRId64, v.s);
6a18b281
MD
571 }
572 break;
5cd6d0e5 573 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
6a18b281
MD
574 {
575 int len;
576
5cd6d0e5 577 len = bt_field_class_integer_get_field_value_range(int_fc);
6a18b281
MD
578 if (len < 64) {
579 /* Round length to the nearest nibble */
580 uint8_t rounded_len = ((len + 3) & ~0x3);
581
582 v.u &= ((uint64_t) 1 << rounded_len) - 1;
583 }
584
f354b1e1 585 bt_common_g_string_append_printf(pretty->string, "0x%" PRIX64, v.u);
6a18b281
MD
586 break;
587 }
588 default:
d94d92ac 589 ret = -1;
6a18b281
MD
590 goto end;
591 }
592end:
ad96d936 593 if (rst_color) {
04609487 594 bt_common_g_string_append(pretty->string, color_rst);
ad96d936 595 }
6a18b281
MD
596 return ret;
597}
598
93a4161c 599static
3228cc1d 600void print_escape_string(struct pretty_component *pretty, const char *str)
93a4161c
JD
601{
602 int i;
603
dd61c4ed 604 bt_common_g_string_append_c(pretty->string, '"');
5280f742 605
93a4161c
JD
606 for (i = 0; i < strlen(str); i++) {
607 /* Escape sequences not recognized by iscntrl(). */
608 switch (str[i]) {
609 case '\\':
dd61c4ed 610 bt_common_g_string_append(pretty->string, "\\\\");
93a4161c
JD
611 continue;
612 case '\'':
dd61c4ed 613 bt_common_g_string_append(pretty->string, "\\\'");
93a4161c
JD
614 continue;
615 case '\"':
dd61c4ed 616 bt_common_g_string_append(pretty->string, "\\\"");
93a4161c
JD
617 continue;
618 case '\?':
dd61c4ed 619 bt_common_g_string_append(pretty->string, "\\\?");
93a4161c
JD
620 continue;
621 }
622
623 /* Standard characters. */
994cd345 624 if (!iscntrl((unsigned char) str[i])) {
dd61c4ed 625 bt_common_g_string_append_c(pretty->string, str[i]);
93a4161c
JD
626 continue;
627 }
628
629 switch (str[i]) {
630 case '\0':
dd61c4ed 631 bt_common_g_string_append(pretty->string, "\\0");
93a4161c
JD
632 break;
633 case '\a':
dd61c4ed 634 bt_common_g_string_append(pretty->string, "\\a");
93a4161c
JD
635 break;
636 case '\b':
dd61c4ed 637 bt_common_g_string_append(pretty->string, "\\b");
93a4161c
JD
638 break;
639 case '\e':
dd61c4ed 640 bt_common_g_string_append(pretty->string, "\\e");
93a4161c
JD
641 break;
642 case '\f':
dd61c4ed 643 bt_common_g_string_append(pretty->string, "\\f");
93a4161c
JD
644 break;
645 case '\n':
dd61c4ed 646 bt_common_g_string_append(pretty->string, "\\n");
93a4161c
JD
647 break;
648 case '\r':
dd61c4ed 649 bt_common_g_string_append(pretty->string, "\\r");
93a4161c
JD
650 break;
651 case '\t':
dd61c4ed 652 bt_common_g_string_append(pretty->string, "\\t");
93a4161c
JD
653 break;
654 case '\v':
dd61c4ed 655 bt_common_g_string_append(pretty->string, "\\v");
93a4161c
JD
656 break;
657 default:
658 /* Unhandled control-sequence, print as hex. */
f354b1e1 659 bt_common_g_string_append_printf(pretty->string, "\\x%02x", str[i]);
93a4161c
JD
660 break;
661 }
662 }
5280f742 663
dd61c4ed 664 bt_common_g_string_append_c(pretty->string, '"');
93a4161c
JD
665}
666
6a18b281 667static
d94d92ac 668int print_enum(struct pretty_component *pretty,
b19ff26f 669 const bt_field *field)
6a18b281 670{
d94d92ac 671 int ret = 0;
5cd6d0e5 672 bt_field_class_enumeration_mapping_label_array label_array;
44c440bc
PP
673 uint64_t label_count;
674 uint64_t i;
96e8f959 675
864cad70
PP
676 switch (bt_field_get_class_type(field)) {
677 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
9c08c816 678 ret = bt_field_enumeration_unsigned_get_mapping_labels(field,
44c440bc
PP
679 &label_array, &label_count);
680 break;
864cad70 681 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
9c08c816 682 ret = bt_field_enumeration_signed_get_mapping_labels(field,
44c440bc
PP
683 &label_array, &label_count);
684 break;
685 default:
498e7994 686 bt_common_abort();
96e8f959 687 }
44c440bc
PP
688
689 if (ret) {
d94d92ac 690 ret = -1;
96e8f959
MD
691 goto end;
692 }
44c440bc 693
dd61c4ed 694 bt_common_g_string_append(pretty->string, "( ");
44c440bc 695 if (label_count == 0) {
83ef3205 696 if (pretty->use_colors) {
04609487 697 bt_common_g_string_append(pretty->string, color_unknown);
83ef3205 698 }
dd61c4ed 699 bt_common_g_string_append(pretty->string, "<unknown>");
83ef3205 700 if (pretty->use_colors) {
04609487 701 bt_common_g_string_append(pretty->string, color_rst);
83ef3205
MD
702 }
703 goto skip_loop;
96e8f959 704 }
44c440bc
PP
705 for (i = 0; i < label_count; i++) {
706 const char *mapping_name = label_array[i];
96e8f959 707
55c68a1a 708 if (i != 0) {
dd61c4ed 709 bt_common_g_string_append(pretty->string, ", ");
44c440bc 710 }
3228cc1d 711 if (pretty->use_colors) {
04609487 712 bt_common_g_string_append(pretty->string, color_enum_mapping_name);
ad96d936 713 }
3228cc1d
PP
714 print_escape_string(pretty, mapping_name);
715 if (pretty->use_colors) {
04609487 716 bt_common_g_string_append(pretty->string, color_rst);
ad96d936 717 }
96e8f959 718 }
83ef3205 719skip_loop:
dd61c4ed 720 bt_common_g_string_append(pretty->string, " : container = ");
312c056a 721 ret = print_integer(pretty, field);
d94d92ac 722 if (ret != 0) {
6a18b281
MD
723 goto end;
724 }
dd61c4ed 725 bt_common_g_string_append(pretty->string, " )");
6a18b281 726end:
6a18b281
MD
727 return ret;
728}
729
730static
d94d92ac 731int print_struct_field(struct pretty_component *pretty,
b19ff26f
PP
732 const bt_field *_struct,
733 const bt_field_class *struct_class,
2f84c7be 734 uint64_t i, bool print_names, uint64_t *nr_printed_fields)
6a18b281 735{
d94d92ac 736 int ret = 0;
6a18b281 737 const char *field_name;
b19ff26f 738 const bt_field *field = NULL;
1e6fd1d7 739 const bt_field_class_structure_member *member;
6a18b281 740
40f4ba76 741 field = bt_field_structure_borrow_member_field_by_index_const(_struct, i);
6a18b281 742 if (!field) {
d94d92ac 743 ret = -1;
6a18b281
MD
744 goto end;
745 }
44c440bc 746
1e6fd1d7
PP
747 member = bt_field_class_structure_borrow_member_by_index_const(
748 struct_class, i);
749 field_name = bt_field_class_structure_member_get_name(member);
6a18b281 750
2b4c4a7c 751 if (*nr_printed_fields > 0) {
dd61c4ed 752 bt_common_g_string_append(pretty->string, ", ");
6a18b281 753 } else {
dd61c4ed 754 bt_common_g_string_append(pretty->string, " ");
6a18b281
MD
755 }
756 if (print_names) {
6acddae3 757 print_field_name_equal(pretty, field_name);
6a18b281 758 }
2f84c7be 759 ret = print_field(pretty, field, print_names);
2b4c4a7c 760 *nr_printed_fields += 1;
a82e90e8 761
6a18b281 762end:
6a18b281
MD
763 return ret;
764}
765
766static
d94d92ac 767int print_struct(struct pretty_component *pretty,
2f84c7be 768 const bt_field *_struct, bool print_names)
6a18b281 769{
d94d92ac 770 int ret = 0;
b19ff26f 771 const bt_field_class *struct_class = NULL;
44c440bc 772 uint64_t nr_fields, i, nr_printed_fields;
6a18b281 773
40f4ba76 774 struct_class = bt_field_borrow_class_const(_struct);
5cd6d0e5 775 nr_fields = bt_field_class_structure_get_member_count(struct_class);
53dc85fa 776
dd61c4ed 777 bt_common_g_string_append(pretty->string, "{");
3228cc1d 778 pretty->depth++;
2b4c4a7c 779 nr_printed_fields = 0;
6a18b281 780 for (i = 0; i < nr_fields; i++) {
5cd6d0e5 781 ret = print_struct_field(pretty, _struct, struct_class, i,
2f84c7be 782 print_names, &nr_printed_fields);
d94d92ac 783 if (ret != 0) {
6a18b281
MD
784 goto end;
785 }
786 }
3228cc1d 787 pretty->depth--;
dd61c4ed 788 bt_common_g_string_append(pretty->string, " }");
a82e90e8 789
6a18b281 790end:
6a18b281
MD
791 return ret;
792}
793
794static
d94d92ac 795int print_array_field(struct pretty_component *pretty,
b19ff26f 796 const bt_field *array, uint64_t i, bool print_names)
6a18b281 797{
b19ff26f 798 const bt_field *field = NULL;
6a18b281 799
44c440bc 800 if (i != 0) {
dd61c4ed 801 bt_common_g_string_append(pretty->string, ", ");
44c440bc 802 } else {
dd61c4ed 803 bt_common_g_string_append(pretty->string, " ");
6a18b281 804 }
44c440bc 805 if (print_names) {
f354b1e1 806 bt_common_g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
6a18b281 807 }
a82e90e8 808
40f4ba76 809 field = bt_field_array_borrow_element_field_by_index_const(array, i);
98b15851 810 BT_ASSERT_DBG(field);
2f84c7be 811 return print_field(pretty, field, print_names);
6a18b281
MD
812}
813
814static
d94d92ac 815int print_array(struct pretty_component *pretty,
b19ff26f 816 const bt_field *array, bool print_names)
6a18b281 817{
d94d92ac 818 int ret = 0;
44c440bc 819 uint64_t len;
6a18b281 820 uint64_t i;
6a18b281 821
44c440bc 822 len = bt_field_array_get_length(array);
dd61c4ed 823 bt_common_g_string_append(pretty->string, "[");
3228cc1d 824 pretty->depth++;
6a18b281 825 for (i = 0; i < len; i++) {
44c440bc 826 ret = print_array_field(pretty, array, i, print_names);
d94d92ac 827 if (ret != 0) {
6a18b281
MD
828 goto end;
829 }
830 }
3228cc1d 831 pretty->depth--;
dd61c4ed 832 bt_common_g_string_append(pretty->string, " ]");
a82e90e8 833
6a18b281 834end:
6a18b281
MD
835 return ret;
836}
837
838static
d94d92ac 839int print_sequence_field(struct pretty_component *pretty,
b19ff26f 840 const bt_field *seq, uint64_t i, bool print_names)
6a18b281 841{
b19ff26f 842 const bt_field *field = NULL;
6a18b281 843
44c440bc 844 if (i != 0) {
dd61c4ed 845 bt_common_g_string_append(pretty->string, ", ");
44c440bc 846 } else {
dd61c4ed 847 bt_common_g_string_append(pretty->string, " ");
6a18b281 848 }
44c440bc 849 if (print_names) {
f354b1e1 850 bt_common_g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
6a18b281 851 }
a82e90e8 852
40f4ba76 853 field = bt_field_array_borrow_element_field_by_index_const(seq, i);
98b15851 854 BT_ASSERT_DBG(field);
2f84c7be 855 return print_field(pretty, field, print_names);
6a18b281
MD
856}
857
858static
d94d92ac 859int print_sequence(struct pretty_component *pretty,
b19ff26f 860 const bt_field *seq, bool print_names)
6a18b281 861{
d94d92ac 862 int ret = 0;
44c440bc 863 uint64_t len;
6a18b281 864 uint64_t i;
6a18b281 865
44c440bc 866 len = bt_field_array_get_length(seq);
dd61c4ed 867 bt_common_g_string_append(pretty->string, "[");
6a18b281 868
3228cc1d 869 pretty->depth++;
6a18b281 870 for (i = 0; i < len; i++) {
44c440bc 871 ret = print_sequence_field(pretty, seq, i, print_names);
d94d92ac 872 if (ret != 0) {
6a18b281
MD
873 goto end;
874 }
875 }
3228cc1d 876 pretty->depth--;
dd61c4ed 877 bt_common_g_string_append(pretty->string, " ]");
a82e90e8 878
6a18b281 879end:
6a18b281
MD
880 return ret;
881}
882
51e61f06
PP
883static
884int print_option(struct pretty_component *pretty,
885 const bt_field *option, bool print_names)
886{
887 int ret = 0;
888 const bt_field *field = NULL;
889
890 field = bt_field_option_borrow_field_const(option);
891 if (field) {
892 bt_common_g_string_append(pretty->string, "{ ");
893 pretty->depth++;
894 if (print_names) {
895 // TODO: find tag's name using field path
896 // print_field_name_equal(pretty, tag_choice);
897 }
898 ret = print_field(pretty, field, print_names);
899 if (ret != 0) {
900 goto end;
901 }
902 pretty->depth--;
903 bt_common_g_string_append(pretty->string, " }");
904 } else {
905 bt_common_g_string_append(pretty->string, "<none>");
906 }
907
908end:
909 return ret;
910}
911
6a18b281 912static
d94d92ac 913int print_variant(struct pretty_component *pretty,
b19ff26f 914 const bt_field *variant, bool print_names)
6a18b281 915{
d94d92ac 916 int ret = 0;
b19ff26f 917 const bt_field *field = NULL;
6a18b281 918
40f4ba76 919 field = bt_field_variant_borrow_selected_option_field_const(variant);
98b15851 920 BT_ASSERT_DBG(field);
dd61c4ed 921 bt_common_g_string_append(pretty->string, "{ ");
3228cc1d 922 pretty->depth++;
6a18b281 923 if (print_names) {
44c440bc
PP
924 // TODO: find tag's name using field path
925 // print_field_name_equal(pretty, tag_choice);
6a18b281 926 }
2f84c7be 927 ret = print_field(pretty, field, print_names);
d94d92ac 928 if (ret != 0) {
6a18b281
MD
929 goto end;
930 }
3228cc1d 931 pretty->depth--;
dd61c4ed 932 bt_common_g_string_append(pretty->string, " }");
a82e90e8 933
6a18b281 934end:
6a18b281
MD
935 return ret;
936}
937
938static
d94d92ac 939int print_field(struct pretty_component *pretty,
2f84c7be 940 const bt_field *field, bool print_names)
6a18b281 941{
4cdfc5e8 942 bt_field_class_type class_id;
6a18b281 943
864cad70 944 class_id = bt_field_get_class_type(field);
ebdb6693 945 if (class_id == BT_FIELD_CLASS_TYPE_BOOL) {
ff87f7c6
PP
946 bt_bool v;
947 const char *text;
948
949 v = bt_field_bool_get_value(field);
950 if (pretty->use_colors) {
04609487 951 bt_common_g_string_append(pretty->string, color_number_value);
ff87f7c6
PP
952 }
953 if (v) {
954 text = "true";
955 } else {
956 text = "false";
957 }
958 bt_common_g_string_append(pretty->string, text);
959 if (pretty->use_colors) {
04609487 960 bt_common_g_string_append(pretty->string, color_rst);
ff87f7c6
PP
961 }
962 return 0;
ebdb6693 963 } else if (class_id == BT_FIELD_CLASS_TYPE_BIT_ARRAY) {
6be72e6c
PP
964 uint64_t v = bt_field_bit_array_get_value_as_integer(field);
965
966 if (pretty->use_colors) {
967 bt_common_g_string_append(pretty->string,
04609487 968 color_number_value);
6be72e6c
PP
969 }
970 bt_common_g_string_append_printf(pretty->string, "0x%" PRIX64,
971 v);
972 if (pretty->use_colors) {
04609487 973 bt_common_g_string_append(pretty->string, color_rst);
6be72e6c
PP
974 }
975 return 0;
ebdb6693
PP
976 } else if (bt_field_class_type_is(class_id,
977 BT_FIELD_CLASS_TYPE_ENUMERATION)) {
978 return print_enum(pretty, field);
979 } else if (bt_field_class_type_is(class_id,
980 BT_FIELD_CLASS_TYPE_INTEGER)) {
3228cc1d 981 return print_integer(pretty, field);
ebdb6693
PP
982 } else if (bt_field_class_type_is(class_id,
983 BT_FIELD_CLASS_TYPE_REAL)) {
6a18b281
MD
984 double v;
985
fe4df857 986 if (class_id == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
ebdb6693 987 v = bt_field_real_single_precision_get_value(field);
fe4df857
FD
988 } else {
989 v = bt_field_real_double_precision_get_value(field);
990 }
991
3228cc1d 992 if (pretty->use_colors) {
04609487 993 bt_common_g_string_append(pretty->string, color_number_value);
ad96d936 994 }
f354b1e1 995 bt_common_g_string_append_printf(pretty->string, "%g", v);
3228cc1d 996 if (pretty->use_colors) {
04609487 997 bt_common_g_string_append(pretty->string, color_rst);
ad96d936 998 }
d94d92ac 999 return 0;
ebdb6693 1000 } else if (class_id == BT_FIELD_CLASS_TYPE_STRING) {
95a518bf
JG
1001 const char *str;
1002
50842bdc 1003 str = bt_field_string_get_value(field);
95a518bf 1004 if (!str) {
d94d92ac 1005 return -1;
95a518bf
JG
1006 }
1007
3228cc1d 1008 if (pretty->use_colors) {
04609487 1009 bt_common_g_string_append(pretty->string, color_string_value);
ad96d936 1010 }
95a518bf 1011 print_escape_string(pretty, str);
3228cc1d 1012 if (pretty->use_colors) {
04609487 1013 bt_common_g_string_append(pretty->string, color_rst);
ad96d936 1014 }
d94d92ac 1015 return 0;
ebdb6693 1016 } else if (class_id == BT_FIELD_CLASS_TYPE_STRUCTURE) {
2f84c7be 1017 return print_struct(pretty, field, print_names);
ebdb6693
PP
1018 } else if (bt_field_class_type_is(class_id,
1019 BT_FIELD_CLASS_TYPE_OPTION)) {
51e61f06 1020 return print_option(pretty, field, print_names);
ebdb6693
PP
1021 } else if (bt_field_class_type_is(class_id,
1022 BT_FIELD_CLASS_TYPE_VARIANT)) {
3228cc1d 1023 return print_variant(pretty, field, print_names);
ebdb6693 1024 } else if (class_id == BT_FIELD_CLASS_TYPE_STATIC_ARRAY) {
3228cc1d 1025 return print_array(pretty, field, print_names);
ebdb6693
PP
1026 } else if (bt_field_class_type_is(class_id,
1027 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)) {
3228cc1d 1028 return print_sequence(pretty, field, print_names);
ebdb6693 1029 } else {
5280f742 1030 // TODO: log instead
5cd6d0e5 1031 fprintf(pretty->err, "[error] Unknown type id: %d\n", (int) class_id);
d94d92ac 1032 return -1;
6a18b281
MD
1033 }
1034}
1035
1036static
d94d92ac 1037int print_stream_packet_context(struct pretty_component *pretty,
b19ff26f 1038 const bt_event *event)
6a18b281 1039{
d94d92ac 1040 int ret = 0;
b19ff26f
PP
1041 const bt_packet *packet = NULL;
1042 const bt_field *main_field = NULL;
6a18b281 1043
40f4ba76 1044 packet = bt_event_borrow_packet_const(event);
6a18b281 1045 if (!packet) {
6a18b281
MD
1046 goto end;
1047 }
40f4ba76 1048 main_field = bt_packet_borrow_context_field_const(packet);
6a18b281 1049 if (!main_field) {
6a18b281
MD
1050 goto end;
1051 }
3228cc1d 1052 if (!pretty->start_line) {
dd61c4ed 1053 bt_common_g_string_append(pretty->string, ", ");
6e1bc0df 1054 }
3228cc1d
PP
1055 pretty->start_line = false;
1056 if (pretty->options.print_scope_field_names) {
1057 print_name_equal(pretty, "stream.packet.context");
6a18b281 1058 }
3228cc1d 1059 ret = print_field(pretty, main_field,
2f84c7be 1060 pretty->options.print_context_field_names);
a82e90e8 1061
6a18b281 1062end:
6a18b281
MD
1063 return ret;
1064}
1065
6a18b281 1066static
d94d92ac 1067int print_stream_event_context(struct pretty_component *pretty,
b19ff26f 1068 const bt_event *event)
6a18b281 1069{
d94d92ac 1070 int ret = 0;
b19ff26f 1071 const bt_field *main_field = NULL;
6a18b281 1072
40f4ba76 1073 main_field = bt_event_borrow_common_context_field_const(event);
6a18b281 1074 if (!main_field) {
6a18b281
MD
1075 goto end;
1076 }
3228cc1d 1077 if (!pretty->start_line) {
dd61c4ed 1078 bt_common_g_string_append(pretty->string, ", ");
6e1bc0df 1079 }
3228cc1d
PP
1080 pretty->start_line = false;
1081 if (pretty->options.print_scope_field_names) {
1082 print_name_equal(pretty, "stream.event.context");
6a18b281 1083 }
3228cc1d 1084 ret = print_field(pretty, main_field,
2f84c7be 1085 pretty->options.print_context_field_names);
a82e90e8 1086
6a18b281 1087end:
6a18b281
MD
1088 return ret;
1089}
1090
1091static
d94d92ac 1092int print_event_context(struct pretty_component *pretty,
b19ff26f 1093 const bt_event *event)
6a18b281 1094{
d94d92ac 1095 int ret = 0;
b19ff26f 1096 const bt_field *main_field = NULL;
6a18b281 1097
40f4ba76 1098 main_field = bt_event_borrow_specific_context_field_const(event);
6a18b281 1099 if (!main_field) {
6a18b281
MD
1100 goto end;
1101 }
3228cc1d 1102 if (!pretty->start_line) {
dd61c4ed 1103 bt_common_g_string_append(pretty->string, ", ");
6e1bc0df 1104 }
3228cc1d
PP
1105 pretty->start_line = false;
1106 if (pretty->options.print_scope_field_names) {
1107 print_name_equal(pretty, "event.context");
6a18b281 1108 }
3228cc1d 1109 ret = print_field(pretty, main_field,
2f84c7be 1110 pretty->options.print_context_field_names);
a82e90e8 1111
6a18b281 1112end:
6a18b281
MD
1113 return ret;
1114}
1115
1116static
d94d92ac 1117int print_event_payload(struct pretty_component *pretty,
b19ff26f 1118 const bt_event *event)
6a18b281 1119{
d94d92ac 1120 int ret = 0;
b19ff26f 1121 const bt_field *main_field = NULL;
6a18b281 1122
40f4ba76 1123 main_field = bt_event_borrow_payload_field_const(event);
6a18b281 1124 if (!main_field) {
6a18b281
MD
1125 goto end;
1126 }
3228cc1d 1127 if (!pretty->start_line) {
dd61c4ed 1128 bt_common_g_string_append(pretty->string, ", ");
6e1bc0df 1129 }
3228cc1d
PP
1130 pretty->start_line = false;
1131 if (pretty->options.print_scope_field_names) {
1132 print_name_equal(pretty, "event.fields");
6a18b281 1133 }
3228cc1d 1134 ret = print_field(pretty, main_field,
2f84c7be 1135 pretty->options.print_payload_field_names);
a82e90e8 1136
6a18b281 1137end:
af9a82eb
JG
1138 return ret;
1139}
1140
08899d39 1141static
0f6bea4e 1142int flush_buf(FILE *stream, struct pretty_component *pretty)
08899d39
PP
1143{
1144 int ret = 0;
1145
1146 if (pretty->string->len == 0) {
1147 goto end;
1148 }
1149
0f6bea4e 1150 if (fwrite(pretty->string->str, pretty->string->len, 1, stream) != 1) {
08899d39
PP
1151 ret = -1;
1152 }
1153
1154end:
1155 return ret;
1156}
1157
af9a82eb 1158BT_HIDDEN
d94d92ac 1159int pretty_print_event(struct pretty_component *pretty,
d6e69534 1160 const bt_message *event_msg)
af9a82eb 1161{
d94d92ac 1162 int ret;
b19ff26f 1163 const bt_event *event =
d6e69534 1164 bt_message_event_borrow_event_const(event_msg);
af9a82eb 1165
98b15851 1166 BT_ASSERT_DBG(event);
3228cc1d 1167 pretty->start_line = true;
5280f742 1168 g_string_assign(pretty->string, "");
2c091c04 1169 ret = print_event_header(pretty, event_msg);
d94d92ac 1170 if (ret != 0) {
af9a82eb
JG
1171 goto end;
1172 }
6a18b281 1173
3228cc1d 1174 ret = print_stream_packet_context(pretty, event);
d94d92ac 1175 if (ret != 0) {
6a18b281
MD
1176 goto end;
1177 }
6a18b281 1178
3228cc1d 1179 ret = print_stream_event_context(pretty, event);
d94d92ac 1180 if (ret != 0) {
6a18b281
MD
1181 goto end;
1182 }
6a18b281 1183
3228cc1d 1184 ret = print_event_context(pretty, event);
d94d92ac 1185 if (ret != 0) {
6a18b281
MD
1186 goto end;
1187 }
6a18b281 1188
3228cc1d 1189 ret = print_event_payload(pretty, event);
d94d92ac 1190 if (ret != 0) {
6a18b281
MD
1191 goto end;
1192 }
af9a82eb 1193
dd61c4ed 1194 bt_common_g_string_append_c(pretty->string, '\n');
0f6bea4e 1195 if (flush_buf(pretty->out, pretty)) {
d94d92ac 1196 ret = -1;
5280f742
PP
1197 goto end;
1198 }
1199
af9a82eb
JG
1200end:
1201 return ret;
1202}
0f6bea4e
PP
1203
1204static
8e53bed4
PP
1205int print_discarded_elements_msg(struct pretty_component *pretty,
1206 const bt_stream *stream,
1207 const bt_clock_snapshot *begin_clock_snapshot,
1208 const bt_clock_snapshot *end_clock_snapshot,
0f6bea4e
PP
1209 uint64_t count, const char *elem_type)
1210{
d94d92ac 1211 int ret = 0;
b19ff26f
PP
1212 const bt_stream_class *stream_class = NULL;
1213 const bt_trace *trace = NULL;
0f6bea4e
PP
1214 const char *stream_name;
1215 const char *trace_name;
8e53bed4 1216 bt_uuid trace_uuid;
0f6bea4e
PP
1217 int64_t stream_class_id;
1218 int64_t stream_id;
697f479d 1219 const char *init_msg;
0f6bea4e
PP
1220
1221 /* Stream name */
0f6bea4e 1222 stream_name = bt_stream_get_name(stream);
8e53bed4
PP
1223 if (!stream_name) {
1224 stream_name = "(unknown)";
1225 }
0f6bea4e
PP
1226
1227 /* Stream class ID */
40f4ba76 1228 stream_class = bt_stream_borrow_class_const(stream);
0f6bea4e
PP
1229 BT_ASSERT(stream_class);
1230 stream_class_id = bt_stream_class_get_id(stream_class);
1231
1232 /* Stream ID */
1233 stream_id = bt_stream_get_id(stream);
1234
1235 /* Trace name */
8e53bed4 1236 trace = bt_stream_borrow_trace_const(stream);
0f6bea4e
PP
1237 BT_ASSERT(trace);
1238 trace_name = bt_trace_get_name(trace);
1239 if (!trace_name) {
1240 trace_name = "(unknown)";
1241 }
1242
1243 /* Trace UUID */
335a2da5 1244 trace_uuid = bt_trace_get_uuid(trace);
0f6bea4e
PP
1245
1246 /* Format message */
1247 g_string_assign(pretty->string, "");
697f479d
PP
1248
1249 if (count == UINT64_C(-1)) {
1250 init_msg = "Tracer may have discarded";
1251 } else {
1252 init_msg = "Tracer discarded";
1253 }
1254
f354b1e1 1255 bt_common_g_string_append_printf(pretty->string,
697f479d 1256 "%s%sWARNING%s%s: %s ",
0f6bea4e
PP
1257 bt_common_color_fg_yellow(),
1258 bt_common_color_bold(),
1259 bt_common_color_reset(),
697f479d 1260 bt_common_color_fg_yellow(), init_msg);
8e53bed4
PP
1261
1262 if (count == UINT64_C(-1)) {
f354b1e1 1263 bt_common_g_string_append_printf(pretty->string, "%ss", elem_type);
8e53bed4 1264 } else {
f354b1e1 1265 bt_common_g_string_append_printf(pretty->string,
8e53bed4
PP
1266 "%" PRIu64 " %s%s", count, elem_type,
1267 count == 1 ? "" : "s");
1268 }
0f6bea4e 1269
dd61c4ed 1270 bt_common_g_string_append_c(pretty->string, ' ');
3f2f6b21 1271
605e1019 1272 if (begin_clock_snapshot && end_clock_snapshot) {
dd61c4ed 1273 bt_common_g_string_append(pretty->string, "between [");
9c202476 1274 print_timestamp_wall(pretty, begin_clock_snapshot, false);
dd61c4ed 1275 bt_common_g_string_append(pretty->string, "] and [");
9c202476 1276 print_timestamp_wall(pretty, end_clock_snapshot, false);
dd61c4ed 1277 bt_common_g_string_append(pretty->string, "]");
0f6bea4e 1278 } else {
dd61c4ed 1279 bt_common_g_string_append(pretty->string, "(unknown time range)");
0f6bea4e
PP
1280 }
1281
f354b1e1 1282 bt_common_g_string_append_printf(pretty->string, " in trace \"%s\" ", trace_name);
0f6bea4e
PP
1283
1284 if (trace_uuid) {
f354b1e1 1285 bt_common_g_string_append_printf(pretty->string,
6162e6b7
MJ
1286 "(UUID: " BT_UUID_FMT ") ",
1287 BT_UUID_FMT_VALUES(trace_uuid));
0f6bea4e 1288 } else {
dd61c4ed 1289 bt_common_g_string_append(pretty->string, "(no UUID) ");
0f6bea4e
PP
1290 }
1291
f354b1e1 1292 bt_common_g_string_append_printf(pretty->string,
8e53bed4 1293 "within stream \"%s\" (stream class ID: %" PRIu64 ", ",
0f6bea4e
PP
1294 stream_name, stream_class_id);
1295
1296 if (stream_id >= 0) {
f354b1e1 1297 bt_common_g_string_append_printf(pretty->string,
8e53bed4 1298 "stream ID: %" PRIu64, stream_id);
0f6bea4e 1299 } else {
dd61c4ed 1300 bt_common_g_string_append(pretty->string, "no stream ID");
0f6bea4e
PP
1301 }
1302
f354b1e1 1303 bt_common_g_string_append_printf(pretty->string, ").%s\n",
0f6bea4e
PP
1304 bt_common_color_reset());
1305
1306 /*
1307 * Print to standard error stream to remain backward compatible
1308 * with Babeltrace 1.
1309 */
1310 if (flush_buf(stderr, pretty)) {
d94d92ac 1311 ret = -1;
0f6bea4e
PP
1312 }
1313
1314 return ret;
1315}
1316
1317BT_HIDDEN
8e53bed4
PP
1318int pretty_print_discarded_items(struct pretty_component *pretty,
1319 const bt_message *msg)
0f6bea4e 1320{
8e53bed4
PP
1321 const bt_clock_snapshot *begin = NULL;
1322 const bt_clock_snapshot *end = NULL;
1323 const bt_stream *stream;
1324 const bt_stream_class *stream_class;
1325 uint64_t count = UINT64_C(-1);
1326 const char *elem_type;
1327
1328 switch (bt_message_get_type(msg)) {
1329 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1330 stream = bt_message_discarded_events_borrow_stream_const(msg);
1331
1332 if (bt_message_discarded_events_get_count(msg, &count) ==
1333 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1334 count = UINT64_C(-1);
0f6bea4e 1335 }
8e53bed4
PP
1336
1337 elem_type = "event";
1338 break;
1339 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1340 stream = bt_message_discarded_packets_borrow_stream_const(msg);
1341
1342 if (bt_message_discarded_packets_get_count(msg, &count) ==
1343 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1344 count = UINT64_C(-1);
1345 }
1346
1347 elem_type = "packet";
1348 break;
1349 default:
498e7994 1350 bt_common_abort();
0f6bea4e
PP
1351 }
1352
8e53bed4
PP
1353 BT_ASSERT(stream);
1354 stream_class = bt_stream_borrow_class_const(stream);
1355
2e90378a
PP
1356 switch (bt_message_get_type(msg)) {
1357 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1358 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
1359 stream_class)) {
9b24b6aa 1360 begin = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 1361 msg);
9b24b6aa 1362 end = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
0cbc2c33 1363 msg);
2e90378a
PP
1364 }
1365
1366 break;
1367 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1368 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
1369 stream_class)) {
9b24b6aa 1370 begin = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 1371 msg);
9b24b6aa 1372 end = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
0cbc2c33 1373 msg);
0f6bea4e 1374 }
2e90378a
PP
1375
1376 break;
1377 default:
498e7994 1378 bt_common_abort();
0f6bea4e
PP
1379 }
1380
8e53bed4
PP
1381 print_discarded_elements_msg(pretty, stream, begin, end,
1382 count, elem_type);
44c440bc 1383 return 0;
0f6bea4e 1384}
04609487
PP
1385
1386BT_HIDDEN
1387void pretty_print_init(void)
1388{
1389 strcpy(color_name, bt_common_color_bold());
1390 strcpy(color_field_name, bt_common_color_fg_cyan());
1391 strcpy(color_rst, bt_common_color_reset());
1392 strcpy(color_string_value, bt_common_color_bold());
1393 strcpy(color_number_value, bt_common_color_bold());
1394 strcpy(color_enum_mapping_name, bt_common_color_bold());
1395 strcpy(color_unknown, bt_common_color_bold());
1396 strcat(color_unknown, bt_common_color_fg_bright_red());
1397 strcpy(color_event_name, bt_common_color_bold());
1398 strcat(color_event_name, bt_common_color_fg_bright_magenta());
1399 strcpy(color_timestamp, bt_common_color_bold());
1400 strcat(color_timestamp, bt_common_color_fg_bright_yellow());
1401}
This page took 0.159011 seconds and 4 git commands to generate.