root/libswish3/trunk/src/libswish3/error.c

Revision 2135, 2.4 kB (checked in by karpet, 7 months ago)

make warnings optional

Line 
1 /*
2  * This file is part of libswish3
3  * Copyright (C) 2007 Peter Karman
4  *
5  *  libswish3 is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  libswish3 is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with libswish3; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /* error handling based on Swish-e ver2 error.c */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <errno.h>
26 #include <err.h>
27 #include <string.h>
28 #include <libxml/globals.h>
29
30 #include "libswish3.h"
31
32 extern int SWISH_DEBUG;
33 extern int SWISH_WARNINGS;
34
35 static FILE *error_handle = NULL;
36
37 void
38 swish_set_error_handle(
39     FILE * where
40 )
41 {
42     error_handle = where;
43 }
44
45 void
46 swish_croak(
47     const char *file,
48     int line,
49     const char *func,
50     char *msgfmt,
51     ...
52 )
53 {
54     va_list args;
55
56     if (!error_handle)
57         error_handle = stderr;
58
59     va_start(args, msgfmt);
60     fprintf(error_handle, "Swish ERROR %s:%d %s: ", file, line, func);
61     vfprintf(error_handle, msgfmt, args);
62     fprintf(error_handle, "\n");
63     va_end(args);
64
65     if (!errno)
66         errno = 1;
67
68     exit(errno);
69 }
70
71 void
72 swish_warn(
73     const char *file,
74     int line,
75     const char *func,
76     char *msgfmt,
77     ...
78 )
79 {
80     va_list args;
81
82     if (!error_handle)
83         error_handle = stderr;
84        
85     if (!SWISH_WARNINGS)
86         return;
87
88     va_start(args, msgfmt);
89     fprintf(error_handle, "Swish WARNING %s:%d %s: ", file, line, func);
90     vfprintf(error_handle, msgfmt, args);
91     fprintf(error_handle, "\n");
92     va_end(args);
93 }
94
95 void
96 swish_debug(
97     const char *file,
98     int line,
99     const char *func,
100     char *msgfmt,
101     ...
102 )
103 {
104     va_list args;
105
106     if (!error_handle)
107         error_handle = stderr;
108
109     va_start(args, msgfmt);
110     fprintf(error_handle, "Swish DEBUG %s:%d %s: ", file, line, func);
111     vfprintf(error_handle, msgfmt, args);
112     fprintf(error_handle, "\n");
113     va_end(args);
114 }
Note: See TracBrowser for help on using the browser.