From: Mathieu Desnoyers Date: Mon, 9 May 2011 18:14:41 +0000 (-0400) Subject: Update pretty print output X-Git-Tag: v0.1~61 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=fd3382e8659903b36971efb933e5770e802b45d5 Update pretty print output Signed-off-by: Mathieu Desnoyers --- diff --git a/formats/ctf-text/ctf-text.c b/formats/ctf-text/ctf-text.c index 882e0620..70e0e92a 100644 --- a/formats/ctf-text/ctf-text.c +++ b/formats/ctf-text/ctf-text.c @@ -60,11 +60,11 @@ int ctf_text_write_event(struct stream_pos *ppos, struct ctf_text_stream_pos *pos = container_of(ppos, struct ctf_text_stream_pos, parent); struct ctf_stream_class *stream_class = stream->stream_class; + int field_nr_saved; struct ctf_event *event_class; uint64_t id = 0; int len_index; int ret; - int field_nr = 0; /* print event header */ if (stream_class->event_header) { @@ -109,54 +109,68 @@ int ctf_text_write_event(struct stream_pos *ppos, fprintf(pos->fp, "name = "); fprintf(pos->fp, "%s", g_quark_to_string(event_class->name)); if (pos->print_names) - field_nr++; + pos->field_nr++; else - fprintf(pos->fp, ": "); + fprintf(pos->fp, ":"); /* Only show the event header in verbose mode */ if (babeltrace_verbose && stream_class->event_header) { - if (field_nr++ != 0) - fprintf(pos->fp, ", "); + if (pos->field_nr++ != 0) + fprintf(pos->fp, ","); if (pos->print_names) - fprintf(pos->fp, "stream.event.header = "); + fprintf(pos->fp, " stream.event.header ="); + field_nr_saved = pos->field_nr; + pos->field_nr = 0; ret = generic_rw(ppos, &stream_class->event_header->p); if (ret) goto error; + pos->field_nr = field_nr_saved; } /* print stream-declared event context */ if (stream_class->event_context) { - if (field_nr++ != 0) - fprintf(pos->fp, ", "); + if (pos->field_nr++ != 0) + fprintf(pos->fp, ","); if (pos->print_names) - fprintf(pos->fp, "stream.event.context = "); + fprintf(pos->fp, " stream.event.context ="); + field_nr_saved = pos->field_nr; + pos->field_nr = 0; ret = generic_rw(ppos, &stream_class->event_context->p); if (ret) goto error; + pos->field_nr = field_nr_saved; } /* print event-declared event context */ if (event_class->context) { - if (field_nr++ != 0) - fprintf(pos->fp, ", "); + if (pos->field_nr++ != 0) + fprintf(pos->fp, ","); if (pos->print_names) - fprintf(pos->fp, "event.context = "); + fprintf(pos->fp, " event.context ="); + field_nr_saved = pos->field_nr; + pos->field_nr = 0; ret = generic_rw(ppos, &event_class->context->p); if (ret) goto error; + pos->field_nr = field_nr_saved; } /* Read and print event payload */ if (event_class->fields) { - if (field_nr++ != 0) - fprintf(pos->fp, ", "); + if (pos->field_nr++ != 0) + fprintf(pos->fp, ","); if (pos->print_names) - fprintf(pos->fp, "event.fields = "); + fprintf(pos->fp, " event.fields ="); + field_nr_saved = pos->field_nr; + pos->field_nr = 0; ret = generic_rw(ppos, &event_class->fields->p); if (ret) goto error; + pos->field_nr = field_nr_saved; } + /* newline */ fprintf(pos->fp, "\n"); + pos->field_nr = 0; return 0; diff --git a/formats/ctf-text/types/array.c b/formats/ctf-text/types/array.c index aa941b5e..9524ba16 100644 --- a/formats/ctf-text/types/array.c +++ b/formats/ctf-text/types/array.c @@ -22,23 +22,26 @@ int ctf_text_array_write(struct stream_pos *ppos, struct definition *definition) { struct ctf_text_stream_pos *pos = ctf_text_pos(ppos); + int field_nr_saved; int ret; if (!pos->dummy) { - if (definition->index != 0 && definition->index != INT_MAX) + if (pos->field_nr++ != 0) fprintf(pos->fp, ","); - if (definition->index != INT_MAX) - fprintf(pos->fp, " "); + fprintf(pos->fp, " "); if (pos->print_names) fprintf(pos->fp, "%s = ", g_quark_to_string(definition->name)); fprintf(pos->fp, "["); pos->depth++; } + field_nr_saved = pos->field_nr; + pos->field_nr = 0; ret = array_rw(ppos, definition); if (!pos->dummy) { pos->depth--; fprintf(pos->fp, " ]"); } + pos->field_nr = field_nr_saved; return ret; } diff --git a/formats/ctf-text/types/enum.c b/formats/ctf-text/types/enum.c index 0cb90218..381491a4 100644 --- a/formats/ctf-text/types/enum.c +++ b/formats/ctf-text/types/enum.c @@ -33,10 +33,9 @@ int ctf_text_enum_write(struct stream_pos *ppos, struct definition *definition) if (pos->dummy) return 0; - if (definition->index != 0 && definition->index != INT_MAX) + if (pos->field_nr++ != 0) fprintf(pos->fp, ","); - if (definition->index != INT_MAX) - fprintf(pos->fp, " "); + fprintf(pos->fp, " "); if (pos->print_names) fprintf(pos->fp, "%s = ", g_quark_to_string(definition->name)); diff --git a/formats/ctf-text/types/float.c b/formats/ctf-text/types/float.c index 3569fd4e..2ec7728e 100644 --- a/formats/ctf-text/types/float.c +++ b/formats/ctf-text/types/float.c @@ -30,10 +30,9 @@ int ctf_text_float_write(struct stream_pos *ppos, struct definition *definition) if (pos->dummy) return 0; - if (definition->index != 0 && definition->index != INT_MAX) + if (pos->field_nr++ != 0) fprintf(pos->fp, ","); - if (definition->index != INT_MAX) - fprintf(pos->fp, " "); + fprintf(pos->fp, " "); if (pos->print_names) fprintf(pos->fp, "%s = ", g_quark_to_string(definition->name)); diff --git a/formats/ctf-text/types/integer.c b/formats/ctf-text/types/integer.c index 0032e642..8b2df2c0 100644 --- a/formats/ctf-text/types/integer.c +++ b/formats/ctf-text/types/integer.c @@ -32,10 +32,9 @@ int ctf_text_integer_write(struct stream_pos *ppos, struct definition *definitio if (pos->dummy) return 0; - if (definition->index != 0 && definition->index != INT_MAX) + if (pos->field_nr++ != 0) fprintf(pos->fp, ","); - if (definition->index != INT_MAX) - fprintf(pos->fp, " "); + fprintf(pos->fp, " "); if (pos->print_names) fprintf(pos->fp, "%s = ", g_quark_to_string(definition->name)); diff --git a/formats/ctf-text/types/sequence.c b/formats/ctf-text/types/sequence.c index 4e904894..7b5d3c94 100644 --- a/formats/ctf-text/types/sequence.c +++ b/formats/ctf-text/types/sequence.c @@ -22,23 +22,26 @@ int ctf_text_sequence_write(struct stream_pos *ppos, struct definition *definition) { struct ctf_text_stream_pos *pos = ctf_text_pos(ppos); + int field_nr_saved; int ret; if (!pos->dummy) { - if (definition->index != 0 && definition->index != INT_MAX) + if (pos->field_nr++ != 0) fprintf(pos->fp, ","); - if (definition->index != INT_MAX) - fprintf(pos->fp, " "); + fprintf(pos->fp, " "); if (pos->print_names) fprintf(pos->fp, "%s = ", g_quark_to_string(definition->name)); fprintf(pos->fp, "["); pos->depth++; } + field_nr_saved = pos->field_nr; + pos->field_nr = 0; ret = sequence_rw(ppos, definition); if (!pos->dummy) { pos->depth--; fprintf(pos->fp, " ]"); } + pos->field_nr = field_nr_saved; return ret; } diff --git a/formats/ctf-text/types/string.c b/formats/ctf-text/types/string.c index 3979e826..eca5f91f 100644 --- a/formats/ctf-text/types/string.c +++ b/formats/ctf-text/types/string.c @@ -32,10 +32,9 @@ int ctf_text_string_write(struct stream_pos *ppos, if (pos->dummy) return 0; - if (definition->index != 0 && definition->index != INT_MAX) + if (pos->field_nr++ != 0) fprintf(pos->fp, ","); - if (definition->index != INT_MAX) - fprintf(pos->fp, " "); + fprintf(pos->fp, " "); if (pos->print_names) fprintf(pos->fp, "%s = ", g_quark_to_string(definition->name)); diff --git a/formats/ctf-text/types/struct.c b/formats/ctf-text/types/struct.c index 9f63c7bd..7dd17e04 100644 --- a/formats/ctf-text/types/struct.c +++ b/formats/ctf-text/types/struct.c @@ -21,28 +21,36 @@ int ctf_text_struct_write(struct stream_pos *ppos, struct definition *definition) { + struct declaration *declaration = definition->declaration; + struct declaration_struct *struct_declaration = + container_of(declaration, struct declaration_struct, p); struct ctf_text_stream_pos *pos = ctf_text_pos(ppos); + uint64_t len = struct_declaration_len(struct_declaration); + int field_nr_saved; int ret; if (!pos->dummy) { if (pos->depth >= 0) { - if (definition->index != 0 && definition->index != INT_MAX) + if (pos->field_nr++ != 0) fprintf(pos->fp, ","); - if (definition->index != INT_MAX) - fprintf(pos->fp, " "); + fprintf(pos->fp, " "); if (pos->print_names && definition->name != 0) fprintf(pos->fp, "%s = ", g_quark_to_string(definition->name)); - fprintf(pos->fp, "{"); + if (pos->print_names || len > 1) + fprintf(pos->fp, "{"); } pos->depth++; } + field_nr_saved = pos->field_nr; + pos->field_nr = 0; ret = struct_rw(ppos, definition); if (!pos->dummy) { pos->depth--; - if (pos->depth >= 0) { + if (pos->depth >= 0 && (pos->print_names || len > 1)) { fprintf(pos->fp, " }"); } } + pos->field_nr = field_nr_saved; return ret; } diff --git a/formats/ctf-text/types/variant.c b/formats/ctf-text/types/variant.c index 1787fb3e..50d21903 100644 --- a/formats/ctf-text/types/variant.c +++ b/formats/ctf-text/types/variant.c @@ -22,14 +22,14 @@ int ctf_text_variant_write(struct stream_pos *ppos, struct definition *definition) { struct ctf_text_stream_pos *pos = ctf_text_pos(ppos); + int field_nr_saved; int ret; if (!pos->dummy) { if (pos->depth >= 0) { - if (definition->index != 0 && definition->index != INT_MAX) + if (pos->field_nr++ != 0) fprintf(pos->fp, ","); - if (definition->index != INT_MAX) - fprintf(pos->fp, " "); + fprintf(pos->fp, " "); if (pos->print_names) fprintf(pos->fp, "%s = ", g_quark_to_string(definition->name)); @@ -37,6 +37,8 @@ int ctf_text_variant_write(struct stream_pos *ppos, struct definition *definitio } pos->depth++; } + field_nr_saved = pos->field_nr; + pos->field_nr = 0; ret = variant_rw(ppos, definition); if (!pos->dummy) { pos->depth--; @@ -44,5 +46,6 @@ int ctf_text_variant_write(struct stream_pos *ppos, struct definition *definitio fprintf(pos->fp, " }"); } } + pos->field_nr = field_nr_saved; return ret; } diff --git a/include/babeltrace/ctf-text/types.h b/include/babeltrace/ctf-text/types.h index bb837bcd..0670b3d6 100644 --- a/include/babeltrace/ctf-text/types.h +++ b/include/babeltrace/ctf-text/types.h @@ -38,6 +38,7 @@ struct ctf_text_stream_pos { int depth; int dummy; /* disable output */ int print_names; /* print field names */ + int field_nr; }; static inline diff --git a/include/babeltrace/types.h b/include/babeltrace/types.h index b7e84da2..2c7650e6 100644 --- a/include/babeltrace/types.h +++ b/include/babeltrace/types.h @@ -446,6 +446,7 @@ struct definition * struct_definition_get_field_from_index(struct definition_struct *struct_definition, int index); int struct_rw(struct stream_pos *pos, struct definition *definition); +uint64_t struct_declaration_len(struct declaration_struct *struct_declaration); /* * The tag enumeration is validated to ensure that it contains only mappings diff --git a/types/struct.c b/types/struct.c index 6012447b..ba312111 100644 --- a/types/struct.c +++ b/types/struct.c @@ -216,3 +216,8 @@ struct_definition_get_field_from_index(struct definition_struct *_struct, return NULL; return g_ptr_array_index(_struct->fields, index); } + +uint64_t struct_declaration_len(struct declaration_struct *struct_declaration) +{ + return struct_declaration->fields->len; +}