All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-block@nongnu.org, Kevin Wolf <kwolf@redhat.com>,
	Max Reitz <mreitz@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	Michael Roth <mdroth@linux.vnet.ibm.com>,
	Eric Blake <eblake@redhat.com>,
	"Daniel P. Berrange" <berrange@redhat.com>
Subject: [Qemu-devel] [PATCH v1 5/6] qapi: generate a qapi_stringify_TYPENAME method for all types
Date: Tue,  7 Jun 2016 11:11:14 +0100	[thread overview]
Message-ID: <1465294275-8733-6-git-send-email-berrange@redhat.com> (raw)
In-Reply-To: <1465294275-8733-1-git-send-email-berrange@redhat.com>

There are sometimes cases where one might wish to have a
pretty string representation of a QAPI type. For example,
the 'qemu-img info' tool wants to print out ImageInfoSpecific
type in a humand friendly format. Also when debugging problems
in code it is often useful to insert code to print out a QAPI
object.

To address this, add a qapi_stringify_TYPENAME() method for
all types which wraps around the TextOutputVisitor to turn
objects into pretty strings.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
 scripts/qapi-types.py | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index 437cf6c..c3eef41 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -167,6 +167,40 @@ void qapi_free_%(c_name)s(%(c_name)s *obj)
     return ret
 
 
+def gen_type_stringify_decl(name):
+    ret = mcgen('''
+
+char *qapi_stringify_%(c_name)s(%(c_name)s *obj, int extraIndent, int skipLevel);
+''',
+                c_name=c_name(name))
+    return ret
+
+
+def gen_type_stringify(name):
+    ret = mcgen('''
+
+char *qapi_stringify_%(c_name)s(%(c_name)s *obj, int extraIndent, int skipLevel)
+{
+    TextOutputVisitor *tov;
+    Visitor *v;
+    char *ret;
+
+    if (!obj) {
+        return NULL;
+    }
+
+    tov = text_output_visitor_new(extraIndent, skipLevel);
+    v = text_output_get_visitor(tov);
+    visit_type_%(c_name)s(v, NULL, &obj, NULL);
+    ret = text_output_get_string(tov);
+    text_output_visitor_cleanup(tov);
+    return ret;
+}
+''',
+                c_name=c_name(name))
+    return ret
+
+
 class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
     def __init__(self):
         self.decl = None
@@ -197,6 +231,10 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
         self.decl += gen_type_cleanup_decl(name)
         self.defn += gen_type_cleanup(name)
 
+    def _gen_type_stringify(self, name):
+        self.decl += gen_type_stringify_decl(name)
+        self.defn += gen_type_stringify(name)
+
     def visit_enum_type(self, name, info, values, prefix):
         # Special case for our lone builtin enum type
         # TODO use something cleaner than existence of info
@@ -215,10 +253,14 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
             self._btin += gen_type_cleanup_decl(name)
             if do_builtins:
                 self.defn += gen_type_cleanup(name)
+            self._btin += gen_type_stringify_decl(name)
+            if do_builtins:
+                self.defn += gen_type_stringify(name)
         else:
             self._fwdecl += gen_fwd_object_or_array(name)
             self.decl += gen_array(name, element_type)
             self._gen_type_cleanup(name)
+            self._gen_type_stringify(name)
 
     def visit_object_type(self, name, info, base, members, variants):
         # Nothing to do for the special empty builtin
@@ -233,11 +275,13 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
         if not name.startswith('q_'):
             # implicit types won't be directly allocated/freed
             self._gen_type_cleanup(name)
+            self._gen_type_stringify(name)
 
     def visit_alternate_type(self, name, info, variants):
         self._fwdecl += gen_fwd_object_or_array(name)
         self.decl += gen_object(name, None, [variants.tag_member], variants)
         self._gen_type_cleanup(name)
+        self._gen_type_stringify(name)
 
 # If you link code generated from multiple schemata, you want only one
 # instance of the code for built-in types.  Generate it only when
@@ -289,6 +333,7 @@ h_comment = '''
 fdef.write(mcgen('''
 #include "qemu/osdep.h"
 #include "qapi/dealloc-visitor.h"
+#include "qapi/text-output-visitor.h"
 #include "%(prefix)sqapi-types.h"
 #include "%(prefix)sqapi-visit.h"
 ''',
-- 
2.5.5

  parent reply	other threads:[~2016-06-07 10:11 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-07 10:11 [Qemu-devel] [PATCH v1 0/6] Report format specific info for LUKS block driver Daniel P. Berrange
2016-06-07 10:11 ` [Qemu-devel] [PATCH v1 1/6] crypto: add support for querying parameters for block encryption Daniel P. Berrange
2016-06-07 14:17   ` Eric Blake
2016-06-07 10:11 ` [Qemu-devel] [PATCH v1 2/6] block: export LUKS specific data to qemu-img info Daniel P. Berrange
2016-06-07 15:36   ` Eric Blake
2016-06-07 15:51     ` Daniel P. Berrange
2016-06-07 16:11       ` Eric Blake
2016-06-07 10:11 ` [Qemu-devel] [PATCH v1 3/6] qapi: assert that visitor impls have required callbacks Daniel P. Berrange
2016-06-07 15:40   ` Eric Blake
2016-06-07 15:46     ` Daniel P. Berrange
2016-06-07 10:11 ` [Qemu-devel] [PATCH v1 4/6] qapi: add a text output visitor for pretty printing types Daniel P. Berrange
2016-06-07 16:09   ` Eric Blake
2016-06-07 16:20     ` Daniel P. Berrange
2016-06-07 16:40       ` Eric Blake
2016-06-07 16:45         ` Daniel P. Berrange
2016-06-07 10:11 ` Daniel P. Berrange [this message]
2016-06-07 16:23   ` [Qemu-devel] [PATCH v1 5/6] qapi: generate a qapi_stringify_TYPENAME method for all types Eric Blake
2016-06-07 10:11 ` [Qemu-devel] [PATCH v1 6/6] block: convert to use qapi_stringify_ImageInfoSpecific Daniel P. Berrange
2016-06-07 16:59   ` Eric Blake
2016-06-07 12:04 ` [Qemu-devel] [PATCH v1 0/6] Report format specific info for LUKS block driver Eric Blake
2016-06-07 14:35   ` Daniel P. Berrange
2016-06-14 13:56 ` Max Reitz
2016-06-14 14:05   ` Daniel P. Berrange

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1465294275-8733-6-git-send-email-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.