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

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