1 /*
2  * errcode.c: internal interface for error reporting
3  *
4  * Copyright (C) 2009-2016 David Lutterkort
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  *
20  * Author: David Lutterkort <lutter@redhat.com>
21  */
22 
23 #include <config.h>
24 
25 #include "errcode.h"
26 #include "memory.h"
27 #include <stdarg.h>
28 
vreport_error(struct error * err,aug_errcode_t errcode,const char * format,va_list ap)29 static void vreport_error(struct error *err, aug_errcode_t errcode,
30                    const char *format, va_list ap) {
31     /* We only remember the first error */
32     if (err->code != AUG_NOERROR)
33         return;
34     assert(err->details == NULL);
35 
36     err->code = errcode;
37     if (format != NULL) {
38         if (vasprintf(&err->details, format, ap) < 0)
39             err->details = NULL;
40     }
41 }
42 
report_error(struct error * err,aug_errcode_t errcode,const char * format,...)43 void report_error(struct error *err, aug_errcode_t errcode,
44                   const char *format, ...) {
45     va_list ap;
46 
47     va_start(ap, format);
48     vreport_error(err, errcode, format, ap);
49     va_end(ap);
50 }
51 
bug_on(struct error * err,const char * srcfile,int srclineno,const char * format,...)52 void bug_on(struct error *err, const char *srcfile, int srclineno,
53             const char *format, ...) {
54     char *msg = NULL;
55     int r;
56     va_list ap;
57 
58     if (err->code != AUG_NOERROR)
59         return;
60 
61     va_start(ap, format);
62     vreport_error(err, AUG_EINTERNAL, format, ap);
63     va_end(ap);
64 
65     if (err->details == NULL) {
66         xasprintf(&err->details, "%s:%d:internal error", srcfile, srclineno);
67     } else {
68         r = xasprintf(&msg, "%s:%d:%s", srcfile, srclineno, err->details);
69         if (r >= 0) {
70             free(err->details);
71             err->details = msg;
72         }
73     }
74 }
75 
reset_error(struct error * err)76 void reset_error(struct error *err) {
77     err->code = AUG_NOERROR;
78     err->minor = 0;
79     FREE(err->details);
80     err->minor_details = NULL;
81 }
82 
83 /*
84  * Local variables:
85  *  indent-tabs-mode: nil
86  *  c-indent-level: 4
87  *  c-basic-offset: 4
88  *  tab-width: 4
89  * End:
90  */
91