From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:39270) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SukyD-0003N9-Tj for qemu-devel@nongnu.org; Fri, 27 Jul 2012 09:52:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Suky9-0006Kg-H9 for qemu-devel@nongnu.org; Fri, 27 Jul 2012 09:52:45 -0400 Received: from e36.co.us.ibm.com ([32.97.110.154]:55142) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Suky9-0006Kc-AZ for qemu-devel@nongnu.org; Fri, 27 Jul 2012 09:52:41 -0400 Received: from /spool/local by e36.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 27 Jul 2012 07:52:39 -0600 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by d03dlp01.boulder.ibm.com (Postfix) with ESMTP id 4A4B01FF001D for ; Fri, 27 Jul 2012 13:52:02 +0000 (WET) Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q6RDpIKD168966 for ; Fri, 27 Jul 2012 07:51:46 -0600 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q6RDpC6P022997 for ; Fri, 27 Jul 2012 07:51:14 -0600 From: Anthony Liguori In-Reply-To: <1343384448-21828-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1343384448-21828-1-git-send-email-xiawenc@linux.vnet.ibm.com> Date: Fri, 27 Jul 2012 08:50:43 -0500 Message-ID: <87txwte2j0.fsf@codemonkey.ws> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: Re: [Qemu-devel] [PATCH] let qemu-img info genereate json output List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Wenchao Xia , qemu-devel@nongnu.org Cc: pbonzini@redhat.com, stefanha@linux.vnet.ibm.com Wenchao Xia writes: > This patch would add option -j in qemu-img info command, which > would generate json output in stdout. This is a great idea. > > Signed-off-by: Wenchao Xia > --- > qemu-img.c | 306 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- > 1 files changed, 264 insertions(+), 42 deletions(-) > > diff --git a/qemu-img.c b/qemu-img.c > index 80cfb9b..a514c17 100644 > --- a/qemu-img.c > +++ b/qemu-img.c > @@ -33,6 +33,9 @@ > #include > #endif > > +#include "qint.h" > +#include "qjson.h" > + > typedef struct img_cmd_t { > const char *name; > int (*handler)(int argc, char **argv); > @@ -84,6 +87,7 @@ static void help(void) > " '-p' show progress of command (only certain commands)\n" > " '-S' indicates the consecutive number of bytes that must contain only zeros\n" > " for qemu-img to create a sparse image during conversion\n" > + " '-j' try get json output, which would be in stdout, > only valid in info command\n" I think an --format=json option would be a bit more extensible and better matches what most tools are doing these days. > "\n" > "Parameters to check subcommand:\n" > " '-r' tries to repair any inconsistencies that are found during the check.\n" > @@ -1102,21 +1106,210 @@ static void dump_snapshots(BlockDriverState *bs) > g_free(sn_tab); > } > > +/* string must be allocated on heap */ > +struct img_infos { CodingStyle > + char *filename; > + char *fmt; > + uint64_t total_sectors; > + int64_t allocated_size; > + int32 enc_flag; > + BlockDriverInfo *bdi; > + char *backing_filename; > + char *backing_filename_full; > + QEMUSnapshotInfo *sn_tab; > + int nb_sns; > +}; > + > +static void img_infos_init(struct img_infos *pinfo) > +{ > + memset(pinfo, 0, sizeof(struct img_infos)); > +} > + > +#define TRY_FREE(p) { \ > + if ((p) != NULL) { \ > + g_free((p)); \ > + (p) = NULL; \ > + } \ > +} > +static void img_infos_uninit(struct img_infos *pinfo) > +{ > + TRY_FREE(pinfo->filename); > + TRY_FREE(pinfo->fmt); > + TRY_FREE(pinfo->bdi); > + TRY_FREE(pinfo->backing_filename); > + TRY_FREE(pinfo->backing_filename_full); > + TRY_FREE(pinfo->sn_tab); > +} > + > +static void snapshot_info_to_list(QList *qlist, QEMUSnapshotInfo *sn) > +{ > + char buf[128]; > + QInt *qint; > + QString *qstr; > + QDict *qdic = qdict_new(); > + > + qstr = qstring_from_str(sn->id_str); > + qdict_put_obj(qdic, "id", QOBJECT(qstr)); > + qstr = qstring_from_str(sn->name); > + qdict_put_obj(qdic, "name", QOBJECT(qstr)); > + > + snprintf(buf, sizeof(buf), "%ld", sn->vm_state_size); > + qstr = qstring_from_str(buf); > + qdict_put_obj(qdic, "vm_state_size", QOBJECT(qstr)); > + > + qint = qint_from_int(sn->date_sec); > + qdict_put_obj(qdic, "date_sec", QOBJECT(qint)); > + qint = qint_from_int(sn->date_nsec); > + qdict_put_obj(qdic, "date_nsec", QOBJECT(qint)); > + snprintf(buf, sizeof(buf), "%ld", sn->vm_clock_nsec); > + qstr = qstring_from_str(buf); > + qdict_put_obj(qdic, "vm_clock_nsec", QOBJECT(qstr)); > + qlist_append(qlist, qdic); > +} No need to open code all of this. Just describe the output as a type in qapi-schema.json. Then you can just #include "qapi-visit.h", then call visit_type_ImageInfo passing in a QMPOutputVisitor. You can then pass the QObject to the json code to pretty print it. Regards, Anthony Liguori