Add loglevel support
[babeltrace.git] / formats / ctf-text / ctf-text.c
index 1500d735c18c437be2fc9c0744498ce9ffd222e8..1b3e4bc73438716289662696e8da6c4b33410c2e 100644 (file)
@@ -3,7 +3,9 @@
  *
  * CTF Text Format registration.
  *
- * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -19,7 +21,7 @@
 #include <babeltrace/format.h>
 #include <babeltrace/ctf-text/types.h>
 #include <babeltrace/ctf/metadata.h>
-#include <babeltrace/babeltrace.h>
+#include <babeltrace/babeltrace-internal.h>
 #include <inttypes.h>
 #include <uuid/uuid.h>
 #include <sys/mman.h>
 #include <unistd.h>
 #include <stdlib.h>
 
-struct trace_descriptor *ctf_text_open_trace(const char *path, int flags);
+int opt_all_field_names,
+       opt_scope_field_names,
+       opt_header_field_names,
+       opt_context_field_names,
+       opt_payload_field_names,
+       opt_trace_name,
+       opt_trace_domain,
+       opt_trace_procname,
+       opt_trace_vpid,
+       opt_loglevel;
+
+enum field_item {
+       ITEM_SCOPE,
+       ITEM_HEADER,
+       ITEM_CONTEXT,
+       ITEM_PAYLOAD,
+};
+
+struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
+               const char *path, int flags,
+               void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
+                       int whence), FILE *metadata_fp);
 void ctf_text_close_trace(struct trace_descriptor *descriptor);
 
 static
@@ -90,6 +113,40 @@ int print_field(struct definition *definition)
        return 1;
 }
 
+static
+void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item)
+{
+       switch (item) {
+       case ITEM_SCOPE:
+               if (opt_all_field_names || opt_scope_field_names)
+                       pos->print_names = 1;
+               else
+                       pos->print_names = 0;
+               break;
+       case ITEM_HEADER:
+               if (opt_all_field_names || opt_header_field_names)
+                       pos->print_names = 1;
+               else
+                       pos->print_names = 0;
+               break;
+       case ITEM_CONTEXT:
+               if (opt_all_field_names || opt_context_field_names)
+                       pos->print_names = 1;
+               else
+                       pos->print_names = 0;
+               break;
+       case ITEM_PAYLOAD:
+               if (opt_all_field_names || opt_payload_field_names)
+                       pos->print_names = 1;
+               else
+                       pos->print_names = 0;
+
+               break;
+       default:
+               assert(0);
+       }
+}
+
 static
 int ctf_text_write_event(struct stream_pos *ppos,
                         struct ctf_stream *stream)
@@ -99,36 +156,12 @@ int ctf_text_write_event(struct stream_pos *ppos,
        struct ctf_stream_class *stream_class = stream->stream_class;
        int field_nr_saved;
        struct ctf_event *event_class;
-       struct ctf_file_event *event;
-       uint64_t id = 0;
+       struct ctf_stream_event *event;
+       uint64_t id;
        int ret;
+       int dom_print = 0;
 
-       /* print event header */
-       if (stream->stream_event_header) {
-               struct definition_integer *integer_definition;
-               struct definition *variant;
-
-               /* lookup event id */
-               integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
-               if (integer_definition) {
-                       id = integer_definition->value._unsigned;
-               } else {
-                       struct definition_enum *enum_definition;
-
-                       enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
-                       if (enum_definition) {
-                               id = enum_definition->integer->value._unsigned;
-                       }
-               }
-
-               variant = lookup_variant(&stream->stream_event_header->p, "v");
-               if (variant) {
-                       integer_definition = lookup_integer(variant, "id", FALSE);
-                       if (integer_definition) {
-                               id = integer_definition->value._unsigned;
-                       }
-               }
-       }
+       id = stream->event_id;
 
        if (id >= stream_class->events_by_id->len) {
                fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
@@ -145,7 +178,8 @@ int ctf_text_write_event(struct stream_pos *ppos,
                return -EINVAL;
        }
 
-       if (stream->timestamp) {
+       if (stream->has_timestamp) {
+               set_field_names_print(pos, ITEM_HEADER);
                if (pos->print_names)
                        fprintf(pos->fp, "timestamp = ");
                else
@@ -159,6 +193,76 @@ int ctf_text_write_event(struct stream_pos *ppos,
                else
                        fprintf(pos->fp, " ");
        }
+       if ((opt_trace_name || opt_all_field_names) && stream_class->trace->path[0] != '\0') {
+               set_field_names_print(pos, ITEM_HEADER);
+               if (pos->print_names) {
+                       if (opt_trace_name || opt_all_field_names)
+                               fprintf(pos->fp, "trace = ");
+               }
+
+               fprintf(pos->fp, "%s", stream_class->trace->path);
+               if (pos->print_names)
+                       fprintf(pos->fp, ", ");
+               else
+                       fprintf(pos->fp, " ");
+       }
+       if ((opt_trace_domain) && stream_class->trace->domain[0] != '\0') {
+               set_field_names_print(pos, ITEM_HEADER);
+               if (pos->print_names) {
+                       fprintf(pos->fp, "trace:domain = ");
+               }
+               if (opt_trace_domain)
+                       fprintf(pos->fp, "%s", stream_class->trace->domain);
+               if (pos->print_names)
+                       fprintf(pos->fp, ", ");
+               dom_print = 1;
+       }
+       if ((opt_trace_procname) && stream_class->trace->procname[0] != '\0') {
+               set_field_names_print(pos, ITEM_HEADER);
+               if (pos->print_names) {
+                       fprintf(pos->fp, "trace:procname = ");
+               } else if (dom_print) {
+                       fprintf(pos->fp, ":");
+               }
+
+               if (opt_trace_procname)
+                       fprintf(pos->fp, "%s", stream_class->trace->procname);
+               if (pos->print_names)
+                       fprintf(pos->fp, ", ");
+               dom_print = 1;
+       }
+       if ((opt_trace_vpid) && stream_class->trace->vpid[0] != '\0') {
+               set_field_names_print(pos, ITEM_HEADER);
+               if (pos->print_names) {
+                       fprintf(pos->fp, "trace:vpid = ");
+               } else if (dom_print) {
+                       fprintf(pos->fp, ":");
+               }
+
+               if (opt_trace_vpid)
+                       fprintf(pos->fp, "%s", stream_class->trace->vpid);
+               if (pos->print_names)
+                       fprintf(pos->fp, ", ");
+               dom_print = 1;
+       }
+       if ((opt_loglevel || opt_all_field_names) && event_class->loglevel_identifier != 0) {
+               set_field_names_print(pos, ITEM_HEADER);
+               if (pos->print_names) {
+                       fprintf(pos->fp, "loglevel = ");
+               } else if (dom_print) {
+                       fprintf(pos->fp, ":");
+               }
+
+               fprintf(pos->fp, "%s (%lld)",
+                       g_quark_to_string(event_class->loglevel_identifier),
+                       (long long) event_class->loglevel_value);
+               if (pos->print_names)
+                       fprintf(pos->fp, ", ");
+               dom_print = 1;
+       }
+       if (dom_print && !pos->print_names)
+               fprintf(pos->fp, " ");
+       set_field_names_print(pos, ITEM_HEADER);
        if (pos->print_names)
                fprintf(pos->fp, "name = ");
        fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
@@ -171,10 +275,12 @@ int ctf_text_write_event(struct stream_pos *ppos,
        if (stream->stream_packet_context) {
                if (pos->field_nr++ != 0)
                        fprintf(pos->fp, ",");
+               set_field_names_print(pos, ITEM_SCOPE);
                if (pos->print_names)
                        fprintf(pos->fp, " stream.packet.context =");
                field_nr_saved = pos->field_nr;
                pos->field_nr = 0;
+               set_field_names_print(pos, ITEM_CONTEXT);
                ret = generic_rw(ppos, &stream->stream_packet_context->p);
                if (ret)
                        goto error;
@@ -185,10 +291,12 @@ int ctf_text_write_event(struct stream_pos *ppos,
        if (babeltrace_verbose && stream->stream_event_header) {
                if (pos->field_nr++ != 0)
                        fprintf(pos->fp, ",");
+               set_field_names_print(pos, ITEM_SCOPE);
                if (pos->print_names)
                        fprintf(pos->fp, " stream.event.header =");
                field_nr_saved = pos->field_nr;
                pos->field_nr = 0;
+               set_field_names_print(pos, ITEM_CONTEXT);
                ret = generic_rw(ppos, &stream->stream_event_header->p);
                if (ret)
                        goto error;
@@ -199,10 +307,12 @@ int ctf_text_write_event(struct stream_pos *ppos,
        if (stream->stream_event_context) {
                if (pos->field_nr++ != 0)
                        fprintf(pos->fp, ",");
+               set_field_names_print(pos, ITEM_SCOPE);
                if (pos->print_names)
                        fprintf(pos->fp, " stream.event.context =");
                field_nr_saved = pos->field_nr;
                pos->field_nr = 0;
+               set_field_names_print(pos, ITEM_CONTEXT);
                ret = generic_rw(ppos, &stream->stream_event_context->p);
                if (ret)
                        goto error;
@@ -213,10 +323,12 @@ int ctf_text_write_event(struct stream_pos *ppos,
        if (event->event_context) {
                if (pos->field_nr++ != 0)
                        fprintf(pos->fp, ",");
+               set_field_names_print(pos, ITEM_SCOPE);
                if (pos->print_names)
                        fprintf(pos->fp, " event.context =");
                field_nr_saved = pos->field_nr;
                pos->field_nr = 0;
+               set_field_names_print(pos, ITEM_CONTEXT);
                ret = generic_rw(ppos, &event->event_context->p);
                if (ret)
                        goto error;
@@ -227,10 +339,12 @@ int ctf_text_write_event(struct stream_pos *ppos,
        if (event->event_fields) {
                if (pos->field_nr++ != 0)
                        fprintf(pos->fp, ",");
+               set_field_names_print(pos, ITEM_SCOPE);
                if (pos->print_names)
                        fprintf(pos->fp, " event.fields =");
                field_nr_saved = pos->field_nr;
                pos->field_nr = 0;
+               set_field_names_print(pos, ITEM_PAYLOAD);
                ret = generic_rw(ppos, &event->event_fields->p);
                if (ret)
                        goto error;
@@ -248,7 +362,10 @@ error:
 }
 
 
-struct trace_descriptor *ctf_text_open_trace(const char *path, int flags)
+struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
+               const char *path, int flags,
+               void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
+                       int whence), FILE *metadata_fp)
 {
        struct ctf_text_stream_pos *pos;
        FILE *fp;
@@ -266,7 +383,7 @@ struct trace_descriptor *ctf_text_open_trace(const char *path, int flags)
                pos->fp = fp;
                pos->parent.rw_table = write_dispatch_table;
                pos->parent.event_cb = ctf_text_write_event;
-               pos->print_names = opt_field_names;
+               pos->print_names = 0;
                break;
        case O_RDONLY:
        default:
This page took 0.027072 seconds and 4 git commands to generate.