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