diff mbox

[V9,3/8] util: add error_append()

Message ID 1388950991-30105-4-git-send-email-xiawenc@linux.vnet.ibm.com
State New
Headers show

Commit Message

Wayne Xia Jan. 5, 2014, 7:43 p.m. UTC
Some old code in error_set() is factored out, so this function can
call it.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 include/qapi/error.h |    6 ++++++
 util/error.c         |   44 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 45 insertions(+), 5 deletions(-)

Comments

Max Reitz Jan. 11, 2014, 10:56 p.m. UTC | #1
On 05.01.2014 20:43, Wenchao Xia wrote:
> Some old code in error_set() is factored out, so this function can
> call it.
>
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
>   include/qapi/error.h |    6 ++++++
>   util/error.c         |   44 +++++++++++++++++++++++++++++++++++++++-----
>   2 files changed, 45 insertions(+), 5 deletions(-)

Reviewed-by: Max Reitz <mreitz@redhat.com>
diff mbox

Patch

diff --git a/include/qapi/error.h b/include/qapi/error.h
index 7d4c696..c6d6dd8 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 3ee362a..dafe130 100644
--- a/util/error.c
+++ b/util/error.c
@@ -23,10 +23,12 @@  struct Error
     ErrorClass err_class;
 };
 
-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) {
@@ -35,10 +37,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;
 
     *errp = err;
@@ -46,6 +45,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, ...)
 {