[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[augeas-devel] augeas: master - More convenience macros for errors



Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=a801a2be5375da83106f19d4cfbd8dd985680c5c
Commit:        a801a2be5375da83106f19d4cfbd8dd985680c5c
Parent:        15ba82cb8731ddbd1d9acd0ac9e769b68ae3a682
Author:        David Lutterkort <lutter redhat com>
AuthorDate:    Tue Nov 24 13:58:19 2009 -0800
Committer:     David Lutterkort <lutter redhat com>
CommitterDate: Fri Dec 18 16:06:15 2009 -0800

More convenience macros for errors

Add BUG_ON and ERR_RET macros for reporting internal errors

  * src/errcode.h (BUG_ON, ERR_RET, HAS_ERR): new macros
  * src/errcode.c (bug_on): implementation for BUG_ON
---
 src/errcode.c |   24 ++++++++++++++++++++++++
 src/errcode.h |   14 ++++++++++++++
 2 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/src/errcode.c b/src/errcode.c
index 52f2644..bac2c77 100644
--- a/src/errcode.c
+++ b/src/errcode.c
@@ -48,6 +48,30 @@ void report_error(struct error *err, aug_errcode_t errcode,
     va_end(ap);
 }
 
+void bug_on(struct error *err, const char *srcfile, int srclineno,
+            const char *format, ...) {
+    char *msg = NULL;
+    int r;
+    va_list ap;
+
+    if (err->code != AUG_NOERROR)
+        return;
+
+    va_start(ap, format);
+    vreport_error(err, AUG_EINTERNAL, format, ap);
+    va_end(ap);
+
+    if (err->details == NULL) {
+        xasprintf(&err->details, "%s:%d:internal error", srcfile, srclineno);
+    } else {
+        r = xasprintf(&msg, "%s:%d:%s", srcfile, srclineno, err->details);
+        if (r >= 0) {
+            free(err->details);
+            err->details = msg;
+        }
+    }
+}
+
 /*
  * Local variables:
  *  indent-tabs-mode: nil
diff --git a/src/errcode.h b/src/errcode.h
index 8913403..e5d2334 100644
--- a/src/errcode.h
+++ b/src/errcode.h
@@ -41,8 +41,16 @@ void report_error(struct error *err, aug_errcode_t errcode,
                   const char *format, ...)
     ATTRIBUTE_FORMAT(printf, 3, 4);
 
+void bug_on(struct error *err, const char *srcfile, int srclineno,
+            const char *format, ...)
+    ATTRIBUTE_FORMAT(printf, 4, 5);
+
+#define HAS_ERR(obj) ((obj)->error->code != AUG_NOERROR)
+
 #define ERR_BAIL(obj) if ((obj)->error->code != AUG_NOERROR) goto error;
 
+#define ERR_RET(obj) if ((obj)->error->code != AUG_NOERROR) return;
+
 #define ERR_NOMEM(cond, obj)                             \
     if (cond) {                                          \
         report_error((obj)->error, AUG_ENOMEM, NULL);    \
@@ -60,6 +68,12 @@ void report_error(struct error *err, aug_errcode_t errcode,
         }                                               \
     } while(0)
 
+#define BUG_ON(cond, obj, fmt ...)                                  \
+    if (cond) {                                                     \
+        bug_on((obj)->error, __FILE__, __LINE__, ## fmt);           \
+        goto error;                                                 \
+    }
+
 #endif
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]