From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=51514 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Py9Zq-0001Md-9f for qemu-devel@nongnu.org; Fri, 11 Mar 2011 16:08:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Py9Zo-0001Cz-MS for qemu-devel@nongnu.org; Fri, 11 Mar 2011 16:08:50 -0500 Received: from e34.co.us.ibm.com ([32.97.110.152]:52087) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Py9Zo-0001Cs-EM for qemu-devel@nongnu.org; Fri, 11 Mar 2011 16:08:48 -0500 Received: from d03relay03.boulder.ibm.com (d03relay03.boulder.ibm.com [9.17.195.228]) by e34.co.us.ibm.com (8.14.4/8.13.1) with ESMTP id p2BKuw3F001367 for ; Fri, 11 Mar 2011 13:56:58 -0700 Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d03relay03.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p2BL8f4o114614 for ; Fri, 11 Mar 2011 14:08:41 -0700 Received: from d03av04.boulder.ibm.com (loopback [127.0.0.1]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p2BL8eWl012631 for ; Fri, 11 Mar 2011 14:08:40 -0700 Message-ID: <4D7A8F56.2080703@us.ibm.com> Date: Fri, 11 Mar 2011 15:08:38 -0600 From: Anthony Liguori MIME-Version: 1.0 References: <1299877249-13433-1-git-send-email-aliguori@us.ibm.com> <1299877249-13433-3-git-send-email-aliguori@us.ibm.com> In-Reply-To: <1299877249-13433-3-git-send-email-aliguori@us.ibm.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] Re: [PATCH 02/11] qerror: expose a function to format an error List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Anthony Liguori Cc: Paolo Bonzini , Luiz Capitulino , qemu-devel@nongnu.org, Michael D Roth , Markus Armbruster On 03/11/2011 03:00 PM, Anthony Liguori wrote: > This will let Error share the QError human formatting. This is only used for > HMP. > > Signed-off-by: Anthony Liguori > > diff --git a/qerror.c b/qerror.c > index 4855604..13d53c9 100644 > --- a/qerror.c > +++ b/qerror.c > @@ -326,12 +326,18 @@ QError *qerror_from_info(const char *file, int linenr, const char *func, > return qerr; > } > > -static void parse_error(const QError *qerror, int c) > +static void parse_error(const QErrorStringTable *entry, int c) > { > - qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc); > +#if 0 > + qerror_abort(qerror, "expected '%c' in '%s'", c, entry->desc); > +#else > + fprintf(stderr, "expected '%c' in '%s'", c, entry->desc); > + abort(); > +#endif > } Err, I shouldn't have left these #if 0's in here. Please ignore them. Regards, Anthony Liguori > -static const char *append_field(QString *outstr, const QError *qerror, > +static const char *append_field(QDict *error, QString *outstr, > + const QErrorStringTable *entry, > const char *start) > { > QObject *obj; > @@ -339,24 +345,31 @@ static const char *append_field(QString *outstr, const QError *qerror, > QString *key_qs; > const char *end, *key; > > - if (*start != '%') > - parse_error(qerror, '%'); > + if (*start != '%') { > + parse_error(entry, '%'); > + } > start++; > - if (*start != '(') > - parse_error(qerror, '('); > + if (*start != '(') { > + parse_error(entry, '('); > + } > start++; > > end = strchr(start, ')'); > - if (!end) > - parse_error(qerror, ')'); > + if (!end) { > + parse_error(entry, ')'); > + } > > key_qs = qstring_from_substr(start, 0, end - start - 1); > key = qstring_get_str(key_qs); > > - qdict = qobject_to_qdict(qdict_get(qerror->error, "data")); > + qdict = qobject_to_qdict(qdict_get(error, "data")); > obj = qdict_get(qdict, key); > if (!obj) { > +#if 0 > qerror_abort(qerror, "key '%s' not found in QDict", key); > +#else > + abort(); > +#endif > } > > switch (qobject_type(obj)) { > @@ -367,41 +380,66 @@ static const char *append_field(QString *outstr, const QError *qerror, > qstring_append_int(outstr, qdict_get_int(qdict, key)); > break; > default: > +#if 0 > qerror_abort(qerror, "invalid type '%c'", qobject_type(obj)); > +#else > + abort(); > +#endif > } > > QDECREF(key_qs); > return ++end; > } > > -/** > - * qerror_human(): Format QError data into human-readable string. > - * > - * Formats according to member 'desc' of the specified QError object. > - */ > -QString *qerror_human(const QError *qerror) > +static QString *qerror_format_desc(QDict *error, > + const QErrorStringTable *entry) > { > - const char *p; > QString *qstring; > + const char *p; > > - assert(qerror->entry != NULL); > + assert(entry != NULL); > > qstring = qstring_new(); > > - for (p = qerror->entry->desc; *p != '\0';) { > + for (p = entry->desc; *p != '\0';) { > if (*p != '%') { > qstring_append_chr(qstring, *p++); > } else if (*(p + 1) == '%') { > qstring_append_chr(qstring, '%'); > p += 2; > } else { > - p = append_field(qstring, qerror, p); > + p = append_field(error, qstring, entry, p); > } > } > > return qstring; > } > > +QString *qerror_format(const char *fmt, QDict *error) > +{ > + const QErrorStringTable *entry = NULL; > + int i; > + > + for (i = 0; qerror_table[i].error_fmt; i++) { > + if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { > + entry =&qerror_table[i]; > + break; > + } > + } > + > + return qerror_format_desc(error, entry); > +} > + > +/** > + * qerror_human(): Format QError data into human-readable string. > + * > + * Formats according to member 'desc' of the specified QError object. > + */ > +QString *qerror_human(const QError *qerror) > +{ > + return qerror_format_desc(qerror->error, qerror->entry); > +} > + > /** > * qerror_print(): Print QError data > * > diff --git a/qerror.h b/qerror.h > index f732d45..fd63ee9 100644 > --- a/qerror.h > +++ b/qerror.h > @@ -42,6 +42,7 @@ void qerror_report_internal(const char *file, int linenr, const char *func, > #define qerror_report(fmt, ...) \ > qerror_report_internal(__FILE__, __LINE__, __func__, fmt, ## __VA_ARGS__) > QError *qobject_to_qerror(const QObject *obj); > +QString *qerror_format(const char *fmt, QDict *error); > > /* > * QError class list