diff mbox

[RFC,v1,06/25] error: Add error_printf_fn()

Message ID 1728a2f1b0ab562957512cafe3931d3073ed68e8.1441667360.git.crosthwaite.peter@gmail.com
State New
Headers show

Commit Message

Peter Crosthwaite Sept. 11, 2015, 5:33 a.m. UTC
Add an API to report an error with a custom printf function. Use
this for the implementation of error_report_err().

Signed-off-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
---

 include/qapi/error.h |  7 +++++++
 util/error.c         | 22 ++++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

Comments

Eric Blake Sept. 11, 2015, 4:10 p.m. UTC | #1
On 09/10/2015 11:33 PM, Peter Crosthwaite wrote:
> Add an API to report an error with a custom printf function. Use
> this for the implementation of error_report_err().
> 
> Signed-off-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
> ---
> 
>  include/qapi/error.h |  7 +++++++
>  util/error.c         | 22 ++++++++++++++++++++--
>  2 files changed, 27 insertions(+), 2 deletions(-)

Sounds independently useful.

> 
> diff --git a/include/qapi/error.h b/include/qapi/error.h
> index b25c72f..0e5c311 100644
> --- a/include/qapi/error.h
> +++ b/include/qapi/error.h
> @@ -94,6 +94,13 @@ const char *error_get_pretty(Error *err);
>  void error_report_err(Error *);
>  
>  /**
> + * Report an and free an error object using a custom printf implementation.

s/an and/and/

> + */
> +
> +void error_printf_fn(Error *err, void (*printf_fn)(void *, const char *, ...),
> +                     void *printf_opaque);
> +
> +/**
>   * Propagate an error to an indirect pointer to an error.  This function will
>   * always transfer ownership of the error reference and handles the case where
>   * dst_err is NULL correctly.  Errors after the first are discarded.
> diff --git a/util/error.c b/util/error.c
> index e9c23ce..c4656af 100644
> --- a/util/error.c

> +void error_printf_fn(Error *err, void (*printf_fn)(void *, const char *, ...),
> +                     void *printf_opaque)
> +{
> +    if (err->next) {
> +        error_printf_fn(err->next, printf_fn, printf_opaque);
> +    }
> +    printf_fn(printf_opaque, "%s\n", error_get_pretty(err));
> +    error_free(err);
> +}

Of course, if you rebase this to come independent of multi-error, it
won't need the first 'if'. But it looks like a reasonable factorization.
diff mbox

Patch

diff --git a/include/qapi/error.h b/include/qapi/error.h
index b25c72f..0e5c311 100644
--- a/include/qapi/error.h
+++ b/include/qapi/error.h
@@ -94,6 +94,13 @@  const char *error_get_pretty(Error *err);
 void error_report_err(Error *);
 
 /**
+ * Report an and free an error object using a custom printf implementation.
+ */
+
+void error_printf_fn(Error *err, void (*printf_fn)(void *, const char *, ...),
+                     void *printf_opaque);
+
+/**
  * Propagate an error to an indirect pointer to an error.  This function will
  * always transfer ownership of the error reference and handles the case where
  * dst_err is NULL correctly.  Errors after the first are discarded.
diff --git a/util/error.c b/util/error.c
index e9c23ce..c4656af 100644
--- a/util/error.c
+++ b/util/error.c
@@ -163,10 +163,18 @@  void error_prefix(Error *err, const char *fmt, ...) {
     g_free(msg);
 }
 
+static void error_report_err_printf(void *opaque, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    error_vreport(fmt, ap);
+    va_end(ap);
+}
+
 void error_report_err(Error *err)
 {
-    error_report("%s", error_get_pretty(err));
-    error_free(err);
+    error_printf_fn(err, error_report_err_printf, NULL);
 }
 
 void error_free(Error *err)
@@ -180,6 +188,16 @@  void error_free(Error *err)
     }
 }
 
+void error_printf_fn(Error *err, void (*printf_fn)(void *, const char *, ...),
+                     void *printf_opaque)
+{
+    if (err->next) {
+        error_printf_fn(err->next, printf_fn, printf_opaque);
+    }
+    printf_fn(printf_opaque, "%s\n", error_get_pretty(err));
+    error_free(err);
+}
+
 void error_propagate(Error **dst_errp, Error *local_err)
 {
     if (local_err && dst_errp == &error_abort) {