From patchwork Tue Feb 11 19:23:03 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 319133 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 0E71A2C007A for ; Tue, 11 Feb 2014 17:24:19 +1100 (EST) Received: from localhost ([::1]:59711 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WD6lU-0003ma-K1 for incoming@patchwork.ozlabs.org; Tue, 11 Feb 2014 01:24:16 -0500 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 d28dlp02.in.ibm.com (9.184.220.127) by e28smtp05.in.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 11 Feb 2014 11:51:44 +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 Received: from RH64wenchao ([9.181.129.59]) by d28av02.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id s1B6LbNL025270; Tue, 11 Feb 2014 11:51:41 +0530 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Tue, 11 Feb 2014 14:23:03 -0500 Message-Id: <1392146588-12120-4-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 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> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 14021106-8256-0000-0000-00000B67532D X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.5 Cc: kwolf@redhat.com, peter.crosthwaite@xilinx.com, jcody@redhat.com, mreitz@redhat.com, stefanha@redhat.com, Wenchao Xia Subject: [Qemu-devel] [PATCH V11 3/8] util: add error_append() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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, ...) {