/src/augeas/src/errcode.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
29 | | static void vreport_error(struct error *err, aug_errcode_t errcode, |
30 | 312 | const char *format, va_list ap) { |
31 | | /* We only remember the first error */ |
32 | 312 | if (err->code != AUG_NOERROR) |
33 | 0 | return; |
34 | 312 | assert(err->details == NULL); |
35 | | |
36 | 312 | err->code = errcode; |
37 | 312 | if (format != NULL) { |
38 | 312 | if (vasprintf(&err->details, format, ap) < 0) |
39 | 0 | err->details = NULL; |
40 | 312 | } |
41 | 312 | } |
42 | | |
43 | | void report_error(struct error *err, aug_errcode_t errcode, |
44 | 312 | const char *format, ...) { |
45 | 312 | va_list ap; |
46 | | |
47 | 312 | va_start(ap, format); |
48 | 312 | vreport_error(err, errcode, format, ap); |
49 | 312 | va_end(ap); |
50 | 312 | } |
51 | | |
52 | | void bug_on(struct error *err, const char *srcfile, int srclineno, |
53 | 0 | const char *format, ...) { |
54 | 0 | char *msg = NULL; |
55 | 0 | int r; |
56 | 0 | va_list ap; |
57 | |
|
58 | 0 | if (err->code != AUG_NOERROR) |
59 | 0 | return; |
60 | | |
61 | 0 | va_start(ap, format); |
62 | 0 | vreport_error(err, AUG_EINTERNAL, format, ap); |
63 | 0 | va_end(ap); |
64 | |
|
65 | 0 | if (err->details == NULL) { |
66 | 0 | xasprintf(&err->details, "%s:%d:internal error", srcfile, srclineno); |
67 | 0 | } else { |
68 | 0 | r = xasprintf(&msg, "%s:%d:%s", srcfile, srclineno, err->details); |
69 | 0 | if (r >= 0) { |
70 | 0 | free(err->details); |
71 | 0 | err->details = msg; |
72 | 0 | } |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | 1.26k | void reset_error(struct error *err) { |
77 | 1.26k | err->code = AUG_NOERROR; |
78 | 1.26k | err->minor = 0; |
79 | 1.26k | FREE(err->details); |
80 | 1.26k | err->minor_details = NULL; |
81 | 1.26k | } |
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 | | */ |