Show field names by default, enhance --names option
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 2 Nov 2011 13:48:20 +0000 (09:48 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 2 Nov 2011 13:48:20 +0000 (09:48 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
converter/babeltrace.c
formats/ctf-text/ctf-text.c
include/babeltrace/babeltrace-internal.h

index 04d2615e788dd470693c9d7a10a13b2f33545d4f..22743c96878a1072585315e3d4eb0fb571d52a83 100644 (file)
@@ -68,7 +68,7 @@ static struct poptOption long_options[] = {
        { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
        { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
        { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
-       { "names", 'n', POPT_ARG_NONE, NULL, OPT_NAMES, NULL, NULL },
+       { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
        { NULL, 0, 0, NULL, 0, NULL, NULL },
 };
 
@@ -92,14 +92,47 @@ static void usage(FILE *fp)
        fprintf(fp, "  -h, --help                     This help message\n");
        fprintf(fp, "  -l, --list                     List available formats\n");
        fprintf(fp, "  -v, --verbose                  Verbose mode\n");
-       fprintf(fp, "                                 (or set BABELTRACE_VERBOSE env. var.)\n");
+       fprintf(fp, "                                 (or set BABELTRACE_VERBOSE environment variable)\n");
        fprintf(fp, "  -d, --debug                    Debug mode\n");
-       fprintf(fp, "                                 (or set BABELTRACE_DEBUG env. var.)\n");
-       fprintf(fp, "  -n, --names                    Print field names\n");
+       fprintf(fp, "                                 (or set BABELTRACE_DEBUG environment variable)\n");
+       fprintf(fp, "  -n, --names name1<,name2,...>  Print field names.\n");
+       fprintf(fp, "                                 Available field names:\n");
+       fprintf(fp, "                                     payload OR args OR arg\n");
+       fprintf(fp, "                                     all, scope, header, context OR ctx\n");
+       fprintf(fp, "                                        (payload active by default)\n");
        list_formats(fp);
        fprintf(fp, "\n");
 }
 
+static int get_names_args(poptContext *pc)
+{
+       char *str, *strlist, *strctx;
+
+       opt_payload_field_names = 0;
+       strlist = (char *) poptGetOptArg(*pc);
+       if (!strlist) {
+               return -EINVAL;
+       }
+       str = strtok_r(strlist, ",", &strctx);
+       do {
+               if (!strcmp(str, "all"))
+                       opt_all_field_names = 1;
+               else if (!strcmp(str, "scope"))
+                       opt_scope_field_names = 1;
+               else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
+                       opt_context_field_names = 1;
+               else if (!strcmp(str, "header"))
+                       opt_header_field_names = 1;
+               else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
+                       opt_payload_field_names = 1;
+               else {
+                       fprintf(stdout, "[error] unknown field name type %s\n", str);
+                       return -EINVAL;
+               }
+       } while ((str = strtok_r(NULL, ",", &strctx)));
+       return 0;
+}
+
 /*
  * Return 0 if caller should continue, < 0 if caller should return
  * error, > 0 if caller should exit without reporting error.
@@ -117,6 +150,9 @@ static int parse_options(int argc, char **argv)
        pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
 
+       /* set default */
+       opt_payload_field_names = 1;
+
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_HELP:
@@ -130,12 +166,15 @@ static int parse_options(int argc, char **argv)
                case OPT_VERBOSE:
                        babeltrace_verbose = 1;
                        break;
+               case OPT_NAMES:
+                       if (get_names_args(&pc)) {
+                               ret = -EINVAL;
+                               goto end;
+                       }
+                       break;
                case OPT_DEBUG:
                        babeltrace_debug = 1;
                        break;
-               case OPT_NAMES:
-                       opt_field_names = 1;
-                       break;
                default:
                        ret = -EINVAL;
                        goto end;
@@ -148,6 +187,7 @@ static int parse_options(int argc, char **argv)
                goto end;
        }
        opt_output_path = poptGetArg(pc);
+
 end:
        if (pc) {
                poptFreeContext(pc);
index b8673755255c037649255d46c5a36392cd51ec56..5879cf4f8dfbb2a1f02216332e3dc817217a88c0 100644 (file)
 #include <unistd.h>
 #include <stdlib.h>
 
-int opt_field_names;
+int opt_all_field_names,
+       opt_scope_field_names,
+       opt_header_field_names,
+       opt_context_field_names,
+       opt_payload_field_names;
+
+enum field_item {
+       ITEM_SCOPE,
+       ITEM_HEADER,
+       ITEM_CONTEXT,
+       ITEM_PAYLOAD,
+};
 
 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
                void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
@@ -96,6 +107,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)
@@ -127,6 +172,7 @@ int ctf_text_write_event(struct stream_pos *ppos,
        }
 
        if (stream->has_timestamp) {
+               set_field_names_print(pos, ITEM_HEADER);
                if (pos->print_names)
                        fprintf(pos->fp, "timestamp = ");
                else
@@ -140,6 +186,7 @@ int ctf_text_write_event(struct stream_pos *ppos,
                else
                        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));
@@ -152,10 +199,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;
@@ -166,10 +215,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;
@@ -180,10 +231,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;
@@ -194,10 +247,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;
@@ -208,10 +263,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;
@@ -249,7 +306,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:
index 0b7b1eee860d04831cfd9e77e8b334f435e9813b..78adefce346e40bd2dc82d964ebf31fc1730acd2 100644 (file)
@@ -29,6 +29,10 @@ struct trace_collection {
 int convert_trace(struct trace_descriptor *td_write,
                  struct trace_collection *trace_collection_read);
 
-extern int opt_field_names;
+extern int opt_all_field_names,
+       opt_scope_field_names,
+       opt_header_field_names,
+       opt_context_field_names,
+       opt_payload_field_names;
 
 #endif
This page took 0.040154 seconds and 4 git commands to generate.