/[ascend]/trunk/base/generic/utilities/error.c
ViewVC logotype

Contents of /trunk/base/generic/utilities/error.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 379 - (show annotations) (download) (as text)
Wed Mar 15 12:48:03 2006 UTC (13 years, 10 months ago) by johnpye
File MIME type: text/x-csrc
File size: 6483 byte(s)
Ensuring that plugins contain no undefined symbols (for the sake of Windows).
Rearranging and commending error.c slightly.
Removing some debug output in solverreporter.
1 #include <string.h>
2
3 #include "error.h"
4
5 /**
6 Global variable which stores the pointer to the callback
7 function being used.
8 */
9 static error_reporter_callback_t g_error_reporter_callback;
10
11 /**
12 Global variable which holds cached error info for
13 later output
14 */
15 static error_reporter_meta_t g_error_reporter_cache;
16
17 /**
18 Default error reporter. To use this error reporter, set
19 the callback pointer to NULL.
20 */
21 int error_reporter_default_callback(ERROR_REPORTER_CALLBACK_ARGS){
22 char *sevmsg="";
23 char *endtxt="\n";
24 int res=0;
25 switch(sev){
26 case ASC_PROG_FATAL: sevmsg = ERR_RED "PROGRAM FATAL ERROR: " ERR_NORM; break;
27 case ASC_PROG_ERROR: sevmsg = ERR_RED "PROGRAM ERROR: " ERR_NORM; break;
28 case ASC_PROG_WARNING: sevmsg = "PROGRAM WARNING: "; break;
29 case ASC_PROG_NOTE: sevmsg = ERR_GRN; endtxt=ERR_NORM; break; /* default, keep unembellished for now */
30 case ASC_USER_ERROR: sevmsg = ERR_RED "ERROR: " ERR_NORM; break;
31 case ASC_USER_WARNING: sevmsg = ERR_BRN "WARNING: " ERR_NORM; break;
32 case ASC_USER_NOTE: sevmsg = "NOTE: "; break;
33 case ASC_USER_SUCCESS: sevmsg = ERR_GRN "SUCCESS: " ERR_NORM; break;
34 }
35
36 res = ASC_FPRINTF(ASCERR,sevmsg);
37 if(filename!=NULL){
38 res += ASC_FPRINTF(ASCERR,"%s:",filename);
39 }
40 if(line!=0){
41 res += ASC_FPRINTF(ASCERR,"%d:",line);
42 }
43 if(funcname!=NULL){
44 res += ASC_FPRINTF(ASCERR,"%s:",funcname);
45 }
46 if ((filename!=NULL) || (line!=0) || (funcname!=NULL)){
47 res += ASC_FPRINTF(ASCERR," ");
48 }
49
50 res += ASC_VFPRINTF(ASCERR,fmt,args);
51 res += ASC_FPRINTF(ASCERR,endtxt);
52
53 return res;
54 }
55
56 /*--------------------------
57 REPORT the error
58 */
59 int
60 va_error_reporter(
61 const error_severity_t sev
62 , const char *errfile
63 , const int errline
64 , const char *errfunc
65 , const char *fmt
66 , const va_list args
67 ){
68 extern error_reporter_callback_t g_error_reporter_callback;
69 int res;
70
71 if(g_error_reporter_callback==NULL){
72 /* fprintf(stderr,"CALLING VFPRINTF\n"); */
73 res = error_reporter_default_callback(sev,errfile,errline,errfunc,fmt,args);
74 }else{
75 /* fprintf(stderr,"CALLING G_ERROR_REPORTER_CALLBACK\n"); */
76 res = g_error_reporter_callback(sev,errfile,errline,errfunc,fmt,args);
77 }
78
79 return res;
80 }
81
82 /*----------------------------
83 DROP-IN replacements for stdio.h / ascPrint.h
84 */
85
86 /**
87 This function performs caching of the error text if the flag is set
88 */
89 int
90 fprintf_error_reporter(FILE *file, const char *fmt, ...){
91 va_list args;
92 extern error_reporter_meta_t g_error_reporter_cache;
93 char *msg;
94 int len;
95 int res;
96
97 /* fprintf(stderr,"ENTERED FPRINTF_ERROR_REPORTER\n"); */
98
99 va_start(args,fmt);
100 if(file==stderr){
101 if(g_error_reporter_cache.iscaching){
102 msg = g_error_reporter_cache.msg;
103 len = strlen(msg);
104 res = vsnprintf(msg+len,ERROR_REPORTER_MAX_MSG-len,fmt,args);
105 if(len+res+1>=ERROR_REPORTER_MAX_MSG){
106 snprintf(msg+ERROR_REPORTER_MAX_MSG-16,15,"... (truncated)");
107 ASC_FPRINTF(stderr,"TRUNCATED MESSAGE, FULL MESSAGE FOLLOWS:\n----------START----------\n");
108 ASC_VFPRINTF(stderr,fmt,args);
109 ASC_FPRINTF(stderr,"\n-----------END----------\n");
110 }
111 }else{
112 /* Not caching: output all in one go as a ASC_PROG_NOTE */
113 res = va_error_reporter(ASC_PROG_NOTE,NULL,0,NULL,fmt,args);
114 }
115 }else{
116 res = ASC_VFPRINTF(file,fmt,args);
117 }
118
119 va_end(args);
120
121 return res;
122 }
123
124 int
125 fputc_error_reporter(int c, FILE *file){
126 if(file!=stderr){
127 return ASC_FPUTC(c,file);
128 }else if(fprintf_error_reporter(file,"%c",c) == 1){
129 return c;
130 }else{
131 return EOF;
132 }
133 }
134
135 int
136 fflush_error_reporter(FILE *file){
137 if(file!=stderr){
138 return ASC_FFLUSH(file);
139 }else{
140 return error_reporter_end_flush();
141 }
142 }
143
144 /*----------------------------
145 CACHING of multiple-FPRINTF error messages
146 */
147
148 int
149 error_reporter_start(const error_severity_t sev, const char *filename, const int line, const char *func){
150
151 extern error_reporter_meta_t g_error_reporter_cache;
152 if(g_error_reporter_cache.iscaching){
153 error_reporter_end_flush();
154 }
155 g_error_reporter_cache.iscaching = 1;
156 *(g_error_reporter_cache.msg) = '\0';
157 g_error_reporter_cache.sev = sev;
158 g_error_reporter_cache.filename = filename;
159 g_error_reporter_cache.line = line;
160 g_error_reporter_cache.func = func;
161
162 return 1;
163 }
164
165 int
166 error_reporter_end_flush(){
167 extern error_reporter_meta_t g_error_reporter_cache;
168
169 error_reporter(
170 g_error_reporter_cache.sev
171 ,g_error_reporter_cache.filename
172 ,g_error_reporter_cache.line
173 ,g_error_reporter_cache.func
174 ,g_error_reporter_cache.msg
175 );
176 g_error_reporter_cache.iscaching = 0;
177
178 return 0; /* output must be compatible with fflush */
179 }
180
181 /*--------------------------
182 REPORT the error
183 */
184 int
185 error_reporter(
186 const error_severity_t sev
187 , const char *errfile
188 , const int errline
189 , const char *errfunc
190 , const char *fmt
191 , ...
192 ){
193 int res;
194 va_list args;
195
196 va_start(args,fmt);
197 res = va_error_reporter(sev,errfile,errline,errfunc,fmt,args);
198 va_end(args);
199
200 return res;
201 }
202
203 /*-------------------------
204 SET the callback function
205 */
206 void
207 error_reporter_set_callback(
208 const error_reporter_callback_t new_callback
209 ){
210 extern error_reporter_callback_t g_error_reporter_callback;
211 g_error_reporter_callback = new_callback;
212 }
213
214 /*-------------------------
215 OPTIONAL code for systems not supporting variadic macros.
216 You know, your system probably does support variadic macros, it's just
217 a question of checking what your particular syntax is...
218 */
219
220 #if !(defined(__GNUC__) && !defined(__STRICT_ANSI__)) && !defined(HAVE_C99)
221 /* Following are only required on compilers without variadic macros: */
222
223 int error_reporter_note_no_line(const char *fmt,...){
224 int res;
225 va_list args;
226
227 va_start(args,fmt);
228 res = va_error_reporter(ASC_PROG_NOTE,"unknown-file",0,fmt,args);
229 va_end(args);
230
231 return res;
232 }
233
234 /**
235 Error reporter 'here' function for compilers not supporting
236 variadic macros.
237 */
238 int error_reporter_here(const error_severity_t sev, const char *fmt,...){
239 int res;
240 va_list args;
241
242 va_start(args,fmt);
243 res = va_error_reporter(sev,"unknown-file",0,fmt,args);
244 va_end(args);
245
246 return res;
247 }
248
249
250 /**
251 Error reporter 'no line' function for compilers not supporting
252 variadic macros.
253 */
254 int error_reporter_noline(const error_severity_t sev, const char *fmt,...){
255 int res;
256 va_list args;
257
258 va_start(args,fmt);
259 res = va_error_reporter(sev,NULL,0,fmt,args);
260 va_end(args);
261
262 return res;
263 }
264
265 int console_debug(const char *fmt,...){
266 int res;
267 va_list args;
268
269 va_start(args,fmt);
270 res = Asc_VFPrintf(ASCERR,fmt,args);
271 va_end(args);
272
273 return res;
274 }
275 #endif

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22