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