Add `-internal` suffix to all internal header files
[babeltrace.git] / plugins / text / print.c
index db6bae374965178c782b4cf53955c0da294ec569..3634d0682816cf3ac97b0a09bc1d8257918a01fc 100644 (file)
 #include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/ctf-ir/fields.h>
 #include <babeltrace/ctf-ir/trace.h>
-#include <babeltrace/bitfield.h>
+#include <babeltrace/bitfield-internal.h>
 #include <babeltrace/common-internal.h>
 #include <inttypes.h>
+#include <ctype.h>
 #include "text.h"
 
 #define NSEC_PER_SEC 1000000000LL
@@ -334,6 +335,7 @@ enum bt_component_status print_event_header(struct text_component *text,
        struct bt_ctf_event_class *event_class = NULL;
        struct bt_ctf_stream_class *stream_class = NULL;
        struct bt_ctf_trace *trace_class = NULL;
+       int dom_print = 0;
 
        event_class = bt_ctf_event_get_class(event);
        if (!event_class) {
@@ -350,10 +352,6 @@ enum bt_component_status print_event_header(struct text_component *text,
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
-       if (!text->start_line) {
-               fputs(", ", text->out);
-       }
-       text->start_line = false;
        ret = print_event_timestamp(text, event, &text->start_line);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
@@ -366,11 +364,13 @@ enum bt_component_status print_event_header(struct text_component *text,
                        if (!text->start_line) {
                                fputs(", ", text->out);
                        }
-                       text->start_line = false;
                        if (print_names) {
                                print_name_equal(text, "trace");
                        }
                        fprintf(text->out, "%s", name);
+                       if (!print_names) {
+                               fprintf(text->out, " ");
+                       }
                }
        }
        if (text->options.print_trace_hostname_field) {
@@ -384,7 +384,6 @@ enum bt_component_status print_event_header(struct text_component *text,
                        if (!text->start_line) {
                                fputs(", ", text->out);
                        }
-                       text->start_line = false;
                        if (print_names) {
                                print_name_equal(text, "trace:hostname");
                        }
@@ -393,6 +392,7 @@ enum bt_component_status print_event_header(struct text_component *text,
                                fprintf(text->out, "%s", str);
                        }
                        bt_put(hostname_str);
+                       dom_print = 1;
                }
        }
        if (text->options.print_trace_domain_field) {
@@ -406,15 +406,17 @@ enum bt_component_status print_event_header(struct text_component *text,
                        if (!text->start_line) {
                                fputs(", ", text->out);
                        }
-                       text->start_line = false;
                        if (print_names) {
                                print_name_equal(text, "trace:domain");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
                        }
                        if (bt_value_string_get(domain_str, &str)
                                        == BT_VALUE_STATUS_OK) {
                                fprintf(text->out, "%s", str);
                        }
                        bt_put(domain_str);
+                       dom_print = 1;
                }
        }
        if (text->options.print_trace_procname_field) {
@@ -428,15 +430,17 @@ enum bt_component_status print_event_header(struct text_component *text,
                        if (!text->start_line) {
                                fputs(", ", text->out);
                        }
-                       text->start_line = false;
                        if (print_names) {
                                print_name_equal(text, "trace:procname");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
                        }
                        if (bt_value_string_get(procname_str, &str)
                                        == BT_VALUE_STATUS_OK) {
                                fprintf(text->out, "%s", str);
                        }
                        bt_put(procname_str);
+                       dom_print = 1;
                }
        }
        if (text->options.print_trace_vpid_field) {
@@ -450,15 +454,17 @@ enum bt_component_status print_event_header(struct text_component *text,
                        if (!text->start_line) {
                                fputs(", ", text->out);
                        }
-                       text->start_line = false;
                        if (print_names) {
                                print_name_equal(text, "trace:vpid");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
                        }
                        if (bt_value_integer_get(vpid_value, &value)
                                        == BT_VALUE_STATUS_OK) {
                                fprintf(text->out, "(%" PRId64 ")", value);
                        }
                        bt_put(vpid_value);
+                       dom_print = 1;
                }
        }
        if (text->options.print_loglevel_field) {
@@ -474,9 +480,10 @@ enum bt_component_status print_event_header(struct text_component *text,
                        if (!text->start_line) {
                                fputs(", ", text->out);
                        }
-                       text->start_line = false;
                        if (print_names) {
                                print_name_equal(text, "loglevel");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
                        }
                        if (loglevel_str) {
                                const char *str;
@@ -498,6 +505,7 @@ enum bt_component_status print_event_header(struct text_component *text,
                        }
                        bt_put(loglevel_str);
                        bt_put(loglevel_value);
+                       dom_print = 1;
                }
        }
        if (text->options.print_emf_field) {
@@ -509,9 +517,10 @@ enum bt_component_status print_event_header(struct text_component *text,
                        if (!text->start_line) {
                                fputs(", ", text->out);
                        }
-                       text->start_line = false;
                        if (print_names) {
                                print_name_equal(text, "model.emf.uri");
+                       } else if (dom_print) {
+                               fputs(":", text->out);
                        }
                        if (uri_str) {
                                const char *str;
@@ -522,12 +531,16 @@ enum bt_component_status print_event_header(struct text_component *text,
                                }
                        }
                        bt_put(uri_str);
+                       dom_print = 1;
                }
        }
+       if (dom_print && !print_names) {
+               fputs(" ", text->out);
+       }
        if (!text->start_line) {
                fputs(", ", text->out);
        }
-       text->start_line = false;
+       text->start_line = true;
        if (print_names) {
                print_name_equal(text, "name");
        }
@@ -538,6 +551,11 @@ enum bt_component_status print_event_header(struct text_component *text,
        if (text->use_colors) {
                fputs(COLOR_RST, text->out);
        }
+       if (!print_names) {
+               fputs(": ", text->out);
+       } else {
+               fputs(", ", text->out);
+       }
 end:
        bt_put(trace_class);
        bt_put(stream_class);
@@ -681,6 +699,72 @@ end:
        return ret;
 }
 
+static
+void print_escape_string(struct text_component *text, const char *str)
+{
+       int i;
+
+       fputc('"', text->out);
+       for (i = 0; i < strlen(str); i++) {
+               /* Escape sequences not recognized by iscntrl(). */
+               switch (str[i]) {
+               case '\\':
+                       fputs("\\\\", text->out);
+                       continue;
+               case '\'':
+                       fputs("\\\'", text->out);
+                       continue;
+               case '\"':
+                       fputs("\\\"", text->out);
+                       continue;
+               case '\?':
+                       fputs("\\\?", text->out);
+                       continue;
+               }
+
+               /* Standard characters. */
+               if (!iscntrl(str[i])) {
+                       fputc(str[i], text->out);
+                       continue;
+               }
+
+               switch (str[i]) {
+               case '\0':
+                       fputs("\\0", text->out);
+                       break;
+               case '\a':
+                       fputs("\\a", text->out);
+                       break;
+               case '\b':
+                       fputs("\\b", text->out);
+                       break;
+               case '\e':
+                       fputs("\\e", text->out);
+                       break;
+               case '\f':
+                       fputs("\\f", text->out);
+                       break;
+               case '\n':
+                       fputs("\\n", text->out);
+                       break;
+               case '\r':
+                       fputs("\\r", text->out);
+                       break;
+               case '\t':
+                       fputs("\\t", text->out);
+                       break;
+               case '\v':
+                       fputs("\\v", text->out);
+                       break;
+               default:
+                       /* Unhandled control-sequence, print as hex. */
+                       fprintf(text->out, "\\x%02x", str[i]);
+                       break;
+               }
+       }
+       fputc('"', text->out);
+}
+
 static
 enum bt_component_status print_enum(struct text_component *text,
                struct bt_ctf_field *field)
@@ -752,8 +836,7 @@ enum bt_component_status print_enum(struct text_component *text,
                if (text->use_colors) {
                        fputs(COLOR_ENUM_MAPPING_NAME, text->out);
                }
-               // TODO: escape string
-               fprintf(text->out, "\"%s\"", mapping_name);
+               print_escape_string(text, mapping_name);
                if (text->use_colors) {
                        fputs(COLOR_RST, text->out);
                }
@@ -899,6 +982,9 @@ enum bt_component_status print_array_field(struct text_component *text,
                } else {
                        fprintf(text->out, " ");
                }
+               if (print_names) {
+                       fprintf(text->out, "[%" PRIu64 "] = ", i);
+               }
        }
        field = bt_ctf_field_array_get_field(array, i);
        if (!field) {
@@ -917,7 +1003,7 @@ enum bt_component_status print_array(struct text_component *text,
 {
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_field_type *array_type = NULL, *field_type = NULL;
-       enum bt_ctf_type_id type_id;
+       enum bt_ctf_field_type_id type_id;
        int64_t len;
        uint64_t i;
        bool is_string = false;
@@ -938,7 +1024,7 @@ enum bt_component_status print_array(struct text_component *text,
                goto end;
        }
        type_id = bt_ctf_field_type_get_type_id(field_type);
-       if (type_id == BT_CTF_TYPE_ID_INTEGER) {
+       if (type_id == BT_CTF_FIELD_TYPE_ID_INTEGER) {
                enum bt_ctf_string_encoding encoding;
 
                encoding = bt_ctf_field_type_integer_get_encoding(field_type);
@@ -980,8 +1066,7 @@ enum bt_component_status print_array(struct text_component *text,
                if (text->use_colors) {
                        fputs(COLOR_STRING_VALUE, text->out);
                }
-               // TODO: escape string
-               fprintf(text->out, "\"%s\"", text->string->str);
+               print_escape_string(text, text->string->str);
                if (text->use_colors) {
                        fputs(COLOR_RST, text->out);
                }
@@ -1008,6 +1093,9 @@ enum bt_component_status print_sequence_field(struct text_component *text,
                } else {
                        fprintf(text->out, " ");
                }
+               if (print_names) {
+                       fprintf(text->out, "[%" PRIu64 "] = ", i);
+               }
        }
        field = bt_ctf_field_sequence_get_field(seq, i);
        if (!field) {
@@ -1027,7 +1115,7 @@ enum bt_component_status print_sequence(struct text_component *text,
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_field_type *seq_type = NULL, *field_type = NULL;
        struct bt_ctf_field *length_field = NULL;
-       enum bt_ctf_type_id type_id;
+       enum bt_ctf_field_type_id type_id;
        uint64_t len;
        uint64_t i;
        bool is_string = false;
@@ -1052,7 +1140,7 @@ enum bt_component_status print_sequence(struct text_component *text,
                goto end;
        }
        type_id = bt_ctf_field_type_get_type_id(field_type);
-       if (type_id == BT_CTF_TYPE_ID_INTEGER) {
+       if (type_id == BT_CTF_FIELD_TYPE_ID_INTEGER) {
                enum bt_ctf_string_encoding encoding;
 
                encoding = bt_ctf_field_type_integer_get_encoding(field_type);
@@ -1097,8 +1185,7 @@ enum bt_component_status print_sequence(struct text_component *text,
                if (text->use_colors) {
                        fputs(COLOR_STRING_VALUE, text->out);
                }
-               // TODO: escape string
-               fprintf(text->out, "\"%s\"", text->string->str);
+               print_escape_string(text, text->string->str);
                if (text->use_colors) {
                        fputs(COLOR_RST, text->out);
                }
@@ -1174,7 +1261,7 @@ enum bt_component_status print_field(struct text_component *text,
                struct bt_ctf_field *field, bool print_names,
                GQuark *filter_fields, int filter_array_len)
 {
-       enum bt_ctf_type_id type_id;
+       enum bt_ctf_field_type_id type_id;
 
        type_id = bt_ctf_field_get_type_id(field);
        switch (type_id) {
@@ -1202,9 +1289,7 @@ enum bt_component_status print_field(struct text_component *text,
                if (text->use_colors) {
                        fputs(COLOR_STRING_VALUE, text->out);
                }
-               // TODO: escape the string value
-               fprintf(text->out, "\"%s\"",
-                       bt_ctf_field_string_get_value(field));
+               print_escape_string(text, bt_ctf_field_string_get_value(field));
                if (text->use_colors) {
                        fputs(COLOR_RST, text->out);
                }
@@ -1376,9 +1461,11 @@ enum bt_component_status text_print_event(struct text_component *text,
                goto end;
        }
 
-       ret = print_event_header_raw(text, event);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto end;
+       if (text->options.verbose) {
+               ret = print_event_header_raw(text, event);
+               if (ret != BT_COMPONENT_STATUS_OK) {
+                       goto end;
+               }
        }
 
        ret = print_stream_event_context(text, event);
This page took 0.029833 seconds and 4 git commands to generate.