From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50593) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WD6jJ-0001p1-67 for qemu-devel@nongnu.org; Tue, 11 Feb 2014 01:22:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WD6j5-0005CM-Ku for qemu-devel@nongnu.org; Tue, 11 Feb 2014 01:22:01 -0500 Received: from e28smtp05.in.ibm.com ([122.248.162.5]:54960) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WD6j4-0005Bk-Vv for qemu-devel@nongnu.org; Tue, 11 Feb 2014 01:21:47 -0500 Received: from /spool/local by e28smtp05.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 11 Feb 2014 11:51:45 +0530 Received: from d28relay01.in.ibm.com (d28relay01.in.ibm.com [9.184.220.58]) by d28dlp02.in.ibm.com (Postfix) with ESMTP id B66B43940058 for ; Tue, 11 Feb 2014 11:51:43 +0530 (IST) Received: from d28av02.in.ibm.com (d28av02.in.ibm.com [9.184.220.64]) by d28relay01.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id s1B6LduR48103676 for ; Tue, 11 Feb 2014 11:51:39 +0530 Received: from d28av02.in.ibm.com (localhost [127.0.0.1]) by d28av02.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id s1B6LhlU025574 for ; Tue, 11 Feb 2014 11:51:43 +0530 From: Wenchao Xia Date: Tue, 11 Feb 2014 14:23:03 -0500 Message-Id: <1392146588-12120-4-git-send-email-xiawenc@linux.vnet.ibm.com> In-Reply-To: <1392146588-12120-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1392146588-12120-1-git-send-email-xiawenc@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH V11 3/8] util: add error_append() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, peter.crosthwaite@xilinx.com, jcody@redhat.com, mreitz@redhat.com, stefanha@redhat.com, Wenchao Xia Some old code in error_set() is factored out, so this function can call it. Signed-off-by: Wenchao Xia Reviewed-by: Max Reitz --- include/qapi/error.h | 6 ++++++ util/error.c | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/include/qapi/error.h b/include/qapi/error.h index c0f0c3b..b36236e 100644 --- a/include/qapi/error.h +++ b/include/qapi/error.h @@ -30,6 +30,12 @@ typedef struct Error Error; void error_set(Error **err, ErrorClass err_class, const char *fmt, ...) GCC_FMT_ATTR(3, 4); /** + * Append message to err if errp != NULL. If errp is already set, "\n" will be inserted + * after old message. If errp is not set yet, it is same as error_setg(). + */ +void error_append(Error **errp, const char *fmt, ...) GCC_FMT_ATTR(2, 3); + +/** * Set an indirect pointer to an error given a ErrorClass value and a * printf-style human message, followed by a strerror() string if * @os_error is not zero. diff --git a/util/error.c b/util/error.c index f11f1d5..5053650 100644 --- a/util/error.c +++ b/util/error.c @@ -25,10 +25,12 @@ struct Error Error *error_abort; -void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...) +static void error_vset(Error **errp, + ErrorClass err_class, + const char *fmt, + va_list ap) { Error *err; - va_list ap; int saved_errno = errno; if (errp == NULL) { @@ -37,10 +39,7 @@ void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...) assert(*errp == NULL); err = g_malloc0(sizeof(*err)); - - va_start(ap, fmt); err->msg = g_strdup_vprintf(fmt, ap); - va_end(ap); err->err_class = err_class; if (errp == &error_abort) { @@ -53,6 +52,41 @@ void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...) errno = saved_errno; } +void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + error_vset(errp, err_class, fmt, ap); + va_end(ap); +} + +void error_append(Error **errp, const char *fmt, ...) +{ + va_list ap; + Error *err_old; + gchar *msg; + + if (error_is_set(errp)) { + err_old = *errp; + *errp = NULL; + va_start(ap, fmt); + msg = g_strdup_vprintf(fmt, ap); + va_end(ap); + + error_set(errp, err_old->err_class, + "%s\n%s", error_get_pretty(err_old), msg); + + g_free(msg); + error_free(err_old); + return; + } + + va_start(ap, fmt); + error_vset(errp, ERROR_CLASS_GENERIC_ERROR, fmt, ap); + va_end(ap); +} + void error_set_errno(Error **errp, int os_errno, ErrorClass err_class, const char *fmt, ...) { -- 1.7.1