/[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 77 - (show annotations) (download) (as text)
Tue Dec 6 04:06:12 2005 UTC (14 years, 9 months ago) by johnpye
File MIME type: text/x-csrc
File size: 5447 byte(s)
Reorganised 'HAVE_PACKAGES' in Jamrules_general.
Removed -DTHIS_IS_AN_UNUSED_FUNCTION from Jamfile so that we find if they really *are* unused.
Added CCFLAGS += stdc=c99 to Jamfile
Changed ASCEND contact details to Wiki URL instead of outdated(?) email address
Conditional HAVE_C99 flag in ascConfig.
ascDynaload.h added __unix__ CPP flag
error.h using C99 rather than GNUC extension
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_NORM "\033[0m"
19 #else
20 # define ERR_RED ""
21 # define ERR_GRN ""
22 # define ERR_NORM ""
23 #endif
24
25 /**
26 Global variable which stores the pointer to the callback
27 function being used.
28 */
29 static error_reporter_callback_t g_error_reporter_callback;
30
31 /**
32 Global variable which holds cached error info for
33 later output
34 */
35 static error_reporter_meta_t g_error_reporter_cache;
36
37 /**
38 Default error reporter. To use this error reporter, set
39 the callback pointer to NULL.
40 */
41 int error_reporter_default_callback(ERROR_REPORTER_CALLBACK_ARGS){
42 char *sevmsg="";
43 char *endtxt="\n";
44 int res=0;
45 switch(sev){
46 case ASC_PROG_FATAL: sevmsg = ERR_RED "PROGRAM FATAL ERROR: " ERR_NORM; break;
47 case ASC_PROG_ERROR: sevmsg = "PROGRAM ERROR: "; break;
48 case ASC_PROG_WARNING: sevmsg = "PROGRAM WARNING: "; break;
49 case ASC_PROG_NOTE: sevmsg = ERR_GRN; endtxt=ERR_NORM; break; /* default, keep unembellished for now */
50 case ASC_USER_ERROR: sevmsg = "ERROR: "; break;
51 case ASC_USER_WARNING: sevmsg = "WARNING: "; break;
52 case ASC_USER_NOTE: sevmsg = "NOTE: "; break;
53 case ASC_USER_SUCCESS: sevmsg = "SUCCESS: "; break;
54 }
55
56 res = ASC_FPRINTF(ASCERR,sevmsg);
57 if(filename!=NULL){
58 res += ASC_FPRINTF(ASCERR,"%s:%d: ",filename,line);
59 }
60 res += ASC_VFPRINTF(ASCERR,fmt,args);
61 res += ASC_FPRINTF(ASCERR,endtxt);
62
63 return res;
64 }
65
66 /*--------------------------
67 REPORT the error
68 */
69 int
70 va_error_reporter(
71 const error_severity_t sev
72 , const char *errfile, const int errline
73 , const char *fmt
74 , va_list args
75 ){
76 extern error_reporter_callback_t g_error_reporter_callback;
77 int res;
78
79 if(g_error_reporter_callback==NULL){
80 /* fprintf(stderr,"CALLING VFPRINTF\n"); */
81 res = error_reporter_default_callback(sev,errfile,errline,fmt,args);
82 }else{
83 /* fprintf(stderr,"CALLING G_ERROR_REPORTER_CALLBACK\n"); */
84 res = g_error_reporter_callback(sev,errfile,errline,fmt,args);
85 }
86
87 return res;
88 }
89
90 /*----------------------------
91 DROP-IN replacements for stdio.h / ascPrint.h
92 */
93
94 /**
95 This function performs caching of the error text if the flag is set
96 */
97 int
98 fprintf_error_reporter(FILE *file, const char *fmt, ...){
99 va_list args;
100 extern error_reporter_meta_t g_error_reporter_cache;
101 char *msg;
102 int len;
103 int res;
104
105 /* fprintf(stderr,"ENTERED FPRINTF_ERROR_REPORTER\n"); */
106
107 va_start(args,fmt);
108 if(file==stderr){
109 if(g_error_reporter_cache.iscaching){
110 msg = g_error_reporter_cache.msg;
111 len = strlen(msg);
112 res = vsnprintf(msg+len,ERROR_REPORTER_MAX_MSG-len,fmt,args);
113 if(len+res+1>=ERROR_REPORTER_MAX_MSG){
114 snprintf(msg+ERROR_REPORTER_MAX_MSG-15,14,"... (truncate)");
115 ASC_FPRINTF(stderr,"TRUNCATED MESSAGE, FULL MESSAGE FOLLOWS:\n----------START----------\n");
116 ASC_VFPRINTF(stderr,fmt,args);
117 ASC_FPRINTF(stderr,"\n-----------END----------\n");
118 }
119 }else{
120 /* Not caching: output all in one go as a ASC_PROG_NOTE */
121 res = va_error_reporter(ASC_PROG_NOTE,NULL,0,fmt,args);
122 }
123 }else{
124 res = ASC_VFPRINTF(file,fmt,args);
125 }
126
127 va_end(args);
128
129 return res;
130 }
131
132 int
133 fputc_error_reporter(int c, FILE *file){
134 if(file!=stderr){
135 return ASC_FPUTC(c,file);
136 }else if(fprintf_error_reporter(file,"%c",c) == 1){
137 return c;
138 }else{
139 return EOF;
140 }
141 }
142
143 int
144 fflush_error_reporter(FILE *file){
145 if(file!=stderr){
146 return ASC_FFLUSH(file);
147 }else{
148 return error_reporter_end_flush();
149 }
150 }
151
152 /*----------------------------
153 CACHING of multiple-FPRINTF error messages
154 */
155
156 int
157 error_reporter_start(const error_severity_t sev, const char *filename, const int line){
158
159 extern error_reporter_meta_t g_error_reporter_cache;
160 if(g_error_reporter_cache.iscaching){
161 error_reporter_end_flush();
162 }
163 g_error_reporter_cache.iscaching = 1;
164 *(g_error_reporter_cache.msg) = '\0';
165 g_error_reporter_cache.sev = sev;
166 g_error_reporter_cache.filename = filename;
167 g_error_reporter_cache.line = line;
168
169 return 1;
170 }
171
172 int
173 error_reporter_end_flush(){
174 extern error_reporter_meta_t g_error_reporter_cache;
175 extern error_reporter_callback_t g_error_reporter_callback;
176
177 error_reporter(
178 g_error_reporter_cache.sev
179 ,g_error_reporter_cache.filename
180 ,g_error_reporter_cache.line
181 ,g_error_reporter_cache.msg
182 );
183 g_error_reporter_cache.iscaching = 0;
184
185 return 0; /* output must be compatible with fflush */
186 }
187
188 /*--------------------------
189 REPORT the error
190 */
191 int
192 error_reporter(
193 const error_severity_t sev
194 , const char *errfile, const int errline
195 , const char *fmt, ...
196 ){
197 int res;
198 va_list args;
199
200 va_start(args,fmt);
201 res = va_error_reporter(sev,errfile,errline,fmt,args);
202 va_end(args);
203
204 return res;
205 }
206
207 #ifndef HAVE_C99
208 int error_reporter_note_no_line(const char *fmt,...){
209 int res;
210 va_list args;
211
212 va_start(args,fmt);
213 res = va_error_reporter(ASC_PROG_NOTE,"unknown-file",0,fmt,args);
214 va_end(args);
215
216 return res;
217 }
218 #endif
219
220 /*-------------------------
221 SET the callback function
222 */
223 void
224 error_reporter_set_callback(
225 const error_reporter_callback_t new_callback
226 ){
227 extern error_reporter_callback_t g_error_reporter_callback;
228 g_error_reporter_callback = new_callback;
229 }
230

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