diff options
Diffstat (limited to 'src/diag.c')
-rw-r--r-- | src/diag.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/diag.c b/src/diag.c new file mode 100644 index 0000000..fcb8fd2 --- /dev/null +++ b/src/diag.c | |||
@@ -0,0 +1,73 @@ | |||
1 | /* grecs - Gray's Extensible Configuration System | ||
2 | Copyright (C) 2007-2011 Sergey Poznyakoff | ||
3 | |||
4 | Grecs is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU General Public License as published by the | ||
6 | Free Software Foundation; either version 3 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | Grecs is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License along | ||
15 | with Grecs. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #ifdef HAVE_CONFIG_H | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | #include <grecs.h> | ||
21 | #include <stdlib.h> | ||
22 | #include <stdio.h> | ||
23 | #include <string.h> | ||
24 | #include <errno.h> | ||
25 | |||
26 | static void | ||
27 | default_print_diag(grecs_locus_t *locus, int err, int errcode, const char *msg) | ||
28 | { | ||
29 | if (locus) | ||
30 | fprintf(stderr, "%s:%d: ", locus->file, locus->line); | ||
31 | if (!err) | ||
32 | fprintf(stderr, "warning: "); | ||
33 | fprintf(stderr, "%s", msg); | ||
34 | if (errcode) | ||
35 | fprintf(stderr, ": %s", strerror(errno)); | ||
36 | fputc('\n', stderr); | ||
37 | } | ||
38 | |||
39 | void (*grecs_print_diag_fun)(grecs_locus_t *, int, int, const char *msg) = | ||
40 | default_print_diag; | ||
41 | |||
42 | void | ||
43 | grecs_warning(grecs_locus_t *locus, int errcode, const char *fmt, ...) | ||
44 | { | ||
45 | va_list ap; | ||
46 | char *buf = NULL; | ||
47 | size_t size = 0; | ||
48 | |||
49 | va_start(ap, fmt); | ||
50 | if (grecs_vasprintf(&buf, &size, fmt, ap)) | ||
51 | grecs_alloc_die(); | ||
52 | va_end(ap); | ||
53 | grecs_print_diag_fun(locus, 0, errcode, buf); | ||
54 | free(buf); | ||
55 | } | ||
56 | |||
57 | void | ||
58 | grecs_error(grecs_locus_t *locus, int errcode, const char *fmt, ...) | ||
59 | { | ||
60 | va_list ap; | ||
61 | char *buf = NULL; | ||
62 | size_t size = 0; | ||
63 | |||
64 | va_start(ap, fmt); | ||
65 | if (grecs_vasprintf(&buf, &size, fmt, ap)) | ||
66 | grecs_alloc_die(); | ||
67 | va_end(ap); | ||
68 | grecs_print_diag_fun(locus, 1, errcode, buf); | ||
69 | free(buf); | ||
70 | grecs_error_count++; | ||
71 | } | ||
72 | |||
73 | |||