/[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 62 - (show annotations) (download) (as text)
Wed Nov 30 05:53:20 2005 UTC (14 years, 7 months ago) by johnpye
File MIME type: text/x-csrc
File size: 5186 byte(s)
My first commit:
Added 'error_reporter' for standardised error reporting
Working on getting external functions running again
Also: issue 130, issue 68, issue 131, issue 89, issue 127, issue 138, issue 94.
1 #include <string.h>
2
3 #include "error.h"
4
5 #define TEST_DEFAULT_FPRINTF
6
7 #ifdef TEST_DEFAULT_FPRINTF
8 # define ERR_RED "\033[31;1m"
9 # define ERR_GRN "\033[32;2m"
10 # define ERR_NORM "\033[0m"
11 #else
12 # define ERR_RED ""
13 # define ERR_GRN ""
14 # define ERR_NORM ""
15 #endif
16
17 /**
18 Global variable which stores the pointer to the callback
19 function being used.
20 */
21 static error_reporter_callback_t g_error_reporter_callback;
22
23 /**
24 Global variable which holds cached error info for
25 later output
26 */
27 static error_reporter_meta_t g_error_reporter_cache;
28
29 /**
30 Default error reporter. To use this error reporter, set
31 the callback pointer to NULL.
32 */
33 int error_reporter_default_callback(ERROR_REPORTER_CALLBACK_ARGS){
34 char *sevmsg="";
35 char *endtxt="\n";
36 int res=0;
37 switch(sev){
38 case ASC_PROG_FATAL: sevmsg = ERR_RED "PROGRAM FATAL ERROR: " ERR_NORM; break;
39 case ASC_PROG_ERROR: sevmsg = "PROGRAM ERROR: "; break;
40 case ASC_PROG_WARNING: sevmsg = "PROGRAM WARNING: "; break;
41 case ASC_PROG_NOTE: sevmsg = ERR_GRN; endtxt=ERR_NORM; break; /* default, keep unembellished for now */
42 case ASC_USER_ERROR: sevmsg = "ERROR: "; break;
43 case ASC_USER_WARNING: sevmsg = "WARNING: "; break;
44 case ASC_USER_NOTE: sevmsg = "NOTE: "; break;
45 case ASC_USER_SUCCESS: sevmsg = "SUCCESS: "; break;
46 }
47
48 res = ASC_FPRINTF(ASCERR,sevmsg);
49 if(filename!=NULL){
50 res += ASC_FPRINTF(ASCERR,"%s:%d: ",filename,line);
51 }
52 res += ASC_VFPRINTF(ASCERR,fmt,args);
53 res += ASC_FPRINTF(ASCERR,endtxt);
54
55 return res;
56 }
57
58 /*--------------------------
59 REPORT the error
60 */
61 int
62 va_error_reporter(
63 const error_severity_t sev
64 , const char *errfile, const int errline
65 , const char *fmt
66 , 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,fmt,args);
74 }else{
75 /* fprintf(stderr,"CALLING G_ERROR_REPORTER_CALLBACK\n"); */
76 res = g_error_reporter_callback(sev,errfile,errline,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-15,14,"... (truncate)");
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,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){
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
161 return 1;
162 }
163
164 int
165 error_reporter_end_flush(){
166 extern error_reporter_meta_t g_error_reporter_cache;
167 extern error_reporter_callback_t g_error_reporter_callback;
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.msg
174 );
175 g_error_reporter_cache.iscaching = 0;
176
177 return 0; /* output must be compatible with fflush */
178 }
179
180 /*--------------------------
181 REPORT the error
182 */
183 int
184 error_reporter(
185 const error_severity_t sev
186 , const char *errfile, const int errline
187 , const char *fmt, ...
188 ){
189 int res;
190 va_list args;
191
192 va_start(args,fmt);
193 res = va_error_reporter(sev,errfile,errline,fmt,args);
194 va_end(args);
195
196 return res;
197 }
198
199 #ifndef __GNUC
200 int error_reporter_note_no_line(const char *fmt,...){
201 int res;
202 va_list args;
203
204 va_start(args,fmt);
205 res = va_error_reporter(ASC_PROG_NOTE,"unknown-file",0,fmt,args);
206 va_end(args);
207
208 return res;
209 }
210 #endif
211
212 /*-------------------------
213 SET the callback function
214 */
215 void
216 error_reporter_set_callback(
217 const error_reporter_callback_t new_callback
218 ){
219 extern error_reporter_callback_t g_error_reporter_callback;
220 g_error_reporter_callback = new_callback;
221 }
222

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