From patchwork Fri Mar 11 21:00:40 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 86452 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1CAF1B6F2B for ; Sat, 12 Mar 2011 08:08:12 +1100 (EST) Received: from localhost ([127.0.0.1]:58886 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Py9Yy-0000BH-Ur for incoming@patchwork.ozlabs.org; Fri, 11 Mar 2011 16:07:57 -0500 Received: from [140.186.70.92] (port=47011 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Py9SZ-0005uk-Jz for qemu-devel@nongnu.org; Fri, 11 Mar 2011 16:01:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Py9SW-0008Mo-GD for qemu-devel@nongnu.org; Fri, 11 Mar 2011 16:01:19 -0500 Received: from e33.co.us.ibm.com ([32.97.110.151]:51923) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Py9SW-0008K2-8o for qemu-devel@nongnu.org; Fri, 11 Mar 2011 16:01:16 -0500 Received: from d03relay01.boulder.ibm.com (d03relay01.boulder.ibm.com [9.17.195.226]) by e33.co.us.ibm.com (8.14.4/8.13.1) with ESMTP id p2BKsWiX021103 for ; Fri, 11 Mar 2011 13:54:32 -0700 Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d03relay01.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p2BL12M7089700 for ; Fri, 11 Mar 2011 14:01:02 -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 p2BL119X008382 for ; Fri, 11 Mar 2011 14:01:01 -0700 Received: from localhost.localdomain (sig-9-65-217-31.mts.ibm.com [9.65.217.31]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p2BL0u8j008003; Fri, 11 Mar 2011 14:01:00 -0700 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Fri, 11 Mar 2011 15:00:40 -0600 Message-Id: <1299877249-13433-3-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1299877249-13433-1-git-send-email-aliguori@us.ibm.com> References: <1299877249-13433-1-git-send-email-aliguori@us.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 32.97.110.151 Cc: Paolo Bonzini , Anthony Liguori , Markus Armbruster , Michael D Roth , Luiz Capitulino Subject: [Qemu-devel] [PATCH 02/11] qerror: expose a function to format an error X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 } -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