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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 414 - (show annotations) (download) (as text)
Mon Apr 3 04:55:35 2006 UTC (14 years, 6 months ago) by johnpye
File MIME type: text/x-chdr
File size: 9154 byte(s)
Some progress on getting a MSVC build to work with SCons. Issues with linking still.
1 #ifndef ASC_ERROR_H
2 #define ASC_ERROR_H
3 /**
4 This file provides error reporting to a callback function via
5 ASCEND's FPRINTF(ASCERR,...) syntax. It is anticipated that
6 this would gradually be expanded to including richer reporting
7 of errors with severity and source file name and line numbers.
8
9 Usage:
10 error_reporter_start(<error-severity>,<filepath>,<linenumber>);
11 FPRINTF(ASCERR,"half of ");
12 FPRINTF(ASCERR,"your message");
13 error_reporter_end_flush();
14
15 or:
16 error_reporter_start(<error-severity>,<filepath>,<linenumber>
17 ,"format string %s %d etc",<printf-args>,...");
18
19 The first form allows you to use multiple FPRINTF statements to
20 generate your error message. The second form assumes that your
21 entire message will be contained in a single statement.
22
23 Error severities are
24 ASC_(USER|PROG)_(NOTE|WARNING|ERROR)
25 and ASC_USER_SUCCESS
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31
32 /**
33 ascConfig defines ASC_FPRINTF etc, the routines to provide
34 default 'real' printf behaviour on this platform. (As
35 opposed to the sneaky stuff that FPRINTF does in this header)
36 */
37 #include <utilities/ascConfig.h>
38 #include <utilities/ascPrint.h>
39
40 /**
41 FPRINTF(ASCERR,...) messages will by default be treated
42 by the error_reporter as ASC_PROG_NOTE messages. These will
43 gradually need to be replaced with error_severity_t values
44 that accurately reflect the nature of the error.
45 */
46 #define FPRINTF fprintf_error_reporter
47 #define FPUTC fputc_error_reporter
48 #define PUTC fputc_error_reporter
49 #define FFLUSH fflush_error_reporter
50
51 /*
52 By default, don't use coloured output on any terminals. We will reintroduce
53 this later, hopefully. It should be done using CURSES, instead of directly
54 using xterm codes. But that brings its own problems on MinGW and Windows...
55 */
56
57 #ifdef USE_XTERM_COLOR_CODES
58 /** XTERM colour codes used to distinguish between errors of different types.
59
60 @TODO some runtime testing to determine if these should be used or not
61 depending on TERM env var.
62 */
63 # define ERR_RED "\033[31;1m"
64 # define ERR_GRN "\033[32;2m"
65 # define ERR_BLU "\033[34;1m"
66 # define ERR_BRN "\033[33;1m"
67 # define ERR_NORM "\033[0m"
68 # define ERR_BOLD "\033[1m"
69 #else
70 # define ERR_RED ""
71 # define ERR_GRN ""
72 # define ERR_BLU ""
73 # define ERR_BRN ""
74 # define ERR_NORM ""
75 # define ERR_BOLD ""
76 #endif
77
78 /**
79 Error severity codes. This will be used to visually
80 the seriousness of errors. ASC_PROG_ERRORs for example
81 might be red, or be highlighted with a (!) icon, etc.
82 */
83 typedef enum error_severity_enum{
84 ASC_USER_SUCCESS=0
85 ,ASC_USER_NOTE=1 /**< a note to the user */
86 ,ASC_USER_WARNING /**< the user has done something bad but tolerable */
87 ,ASC_USER_ERROR /**< the user has done something wrong */
88 ,ASC_PROG_NOTE /**< a note for the programmer */
89 ,ASC_PROG_WARNING /**< the program encounters an unexpected state */
90 ,ASC_PROG_ERROR /**< the program has failed but can ignore and continue (maybe) */
91 ,ASC_PROG_FATAL /**< fatal error, program will exit */
92 } error_severity_t;
93
94
95 /**
96 Variadic macros to allow nice succint logging and error reporting
97 calls from C dialects that support them (GCC, C99 and others)
98
99 If you don't support variadic macros, you will still get the messages
100 but without the file/function/line number.
101 */
102 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
103 # define ERROR_REPORTER_DEBUG(args...) error_reporter(ASC_PROG_NOTE, __FILE__, __LINE__, __func__, ##args)
104 # define ERROR_REPORTER_HERE(SEV,args...) error_reporter(SEV,__FILE__, __LINE__, __func__, ##args)
105 # define ERROR_REPORTER_NOLINE(SEV,args...) error_reporter(SEV, NULL, 0, NULL, ##args)
106 # define CONSOLE_DEBUG(args...) (fprintf(stderr, ERR_BOLD "%s:%d (%s): ", __FILE__,__LINE__,__func__) + \
107 fprintf(stderr, ##args) + \
108 fprintf(stderr, ERR_NORM "\n"))
109
110 # define ERROR_REPORTER_START_HERE(SEV) error_reporter_start(SEV,__FILE__,__LINE__,__func__);
111
112 #elif defined(HAVE_C99)
113 # define ERROR_REPORTER_DEBUG(...) error_reporter(ASC_PROG_NOTE,__FILE__,__LINE__,__func__,## __VA_ARGS__)
114 # define ERROR_REPORTER_HERE(SEV,...) error_reporter(SEV,__FILE__,__LINE__,__func__, ## __VA_ARGS__)
115 # define ERROR_REPORTER_NOLINE(SEV,...) error_reporter(SEV,NULL,0,NULL, ## __VA_ARGS__)
116 # define CONSOLE_DEBUG(...) (fprintf(stderr, ERR_BOLD "%s:%d (%s): ", __FILE__,__LINE__,__func__) + \
117 fprintf(stderr, ##__VA_ARGS__) + \
118 fprintf(stderr, ERR_NORM "\n"))
119
120 #define ERROR_REPORTER_START_HERE(SEV) error_reporter_start(SEV,__FILE__,__LINE__,__func__);
121
122 #else
123 # define ERROR_REPORTER_DEBUG error_reporter_note_no_line
124 # define ERROR_REPORTER_HERE error_reporter_here
125 # define ERROR_REPORTER_NOLINE error_reporter_noline
126 # define CONSOLE_DEBUG console_debug
127 # define ERROR_REPORTER_START_HERE(SEV) error_reporter_start(SEV,__FILE__,__LINE__,__FUNCTION__);
128 int error_reporter_note_no_line(const char *fmt,...);
129 int error_reporter_here(const error_severity_t sev, const char *fmt,...);
130 int error_reporter_noline(const error_severity_t sev, const char *fmt,...);
131 int console_debug(const char *fmt,...);
132 #endif
133
134 #define ERROR_REPORTER_START_NOLINE(SEV) error_reporter_start(SEV,NULL,0,NULL);
135
136 #define ERROR_REPORTER_STAT(sev,stat,msg) \
137 error_reporter(sev,Asc_ModuleFileName(stat->mod),stat->linenum,NULL,msg)
138
139 /** An alias for ASC_PROG_ERROR */
140 #define ASC_PROG_ERR ASC_PROG_ERROR
141
142 #define ERROR_REPORTER_MAX_MSG 4096 /* no particular reason */
143
144 typedef struct{
145 unsigned char iscaching; /** set to true for fprintf_error_reporter to do its work */
146 error_severity_t sev;
147 const char *filename;
148 int line;
149 const char *func;
150 char msg[ERROR_REPORTER_MAX_MSG];
151 } error_reporter_meta_t;
152
153 /**
154 This is the drop-in replacement for Asc_FPrintf. Anythin you attempt
155 to print to stderr will be captured and passed to the error_reporter_callback
156 function for handling.
157 */
158 int fprintf_error_reporter(FILE *file, const char *fmt, ...);
159
160 /**
161 If file!=stderr, this will do the usual thing. If file==stderr, it will output
162 the character via fprintf_error_reporter.
163 */
164 int fputc_error_reporter(int c, FILE *file); /* just calls fprintf_error_reporter */
165
166 /**
167 This replaces the standard 'fflush' of Asc_FFlush. If file!=stderr, it will
168 call the standard fflush. If file==stderr, it will call error_reporter_end_flush.
169 */
170 int fflush_error_reporter(FILE *file);
171
172 /**
173 Start a cached error report. This means that multiple frprintf_error_reporter calls will
174 be stored in a global string until an error_reporter_end_flush is encountered.
175 */
176 int error_reporter_start(const error_severity_t sev, const char *filename, const int line, const char *func);
177
178 /**
179 Output the contents of the checked global string as an error report
180 */
181 int error_reporter_end_flush();
182
183 /**
184 This #define saves you typing the list of arguments in your
185 callback function declarations.
186 */
187 #define ERROR_REPORTER_CALLBACK_ARGS \
188 const error_severity_t sev \
189 , const char *filename \
190 , const int line \
191 , const char *funcname \
192 , const char *fmt \
193 , const va_list args
194
195 /*
196 In you have functions which pass-through callback parameters,
197 this #define ensures that if their ordering/naming changes,
198 you won't have to go hunting and change stuff.
199 */
200 #define ERROR_REPORTER_CALLBACK_VARS \
201 sev, filename, line, funcname, fmt, args
202
203 /*
204 Define the type of the function pointer to be used for all
205 error reporting functions. The final argument is a va_list.
206 You should use 'vsnprintf' of 'vfprintf' to output your
207 message to the desired file or string, see <stdio.h> for these.
208 */
209 typedef int (*error_reporter_callback_t)(
210 ERROR_REPORTER_CALLBACK_ARGS
211 );
212
213 typedef int (*ErrorReporter_fptr_t)(
214 const error_severity_t sev
215 , const char *errfile
216 , const int errline
217 , const char *errfunc
218 , const char *fmt
219 , ...
220 );
221
222 #ifdef ASC_USE_IMPORTED_ERROR_REPORTER
223 /*
224 If we're using a 'imported error reporter' then this means that we're in
225 a different DLL to the main ASCEND code. In that case, we will have a
226 pointer to our error reporter function, which will be back in the main
227 DLL in fact. We need this header file to refer to that global-variable
228 function pointer instead of assuming that the function is here locally.
229 */
230 static ErrorReporter_fptr_t g_ErrorReporter_fptr;
231 # define error_reporter (*g_ErrorReporter_fptr)
232 #else
233
234 /**
235 Use this function directly for 'richer' reporting of
236 of error messages.
237
238 @return follows the style of fprintf
239 */
240 DLEXPORT int error_reporter(
241 const error_severity_t sev
242 , const char *errfile
243 , const int errline
244 , const char *errfunc
245 , const char *fmt
246 , ...
247 );
248
249 #endif
250
251 /**
252 This format of the error reporter is useful if you must call it
253 from another variable-argument-list function.
254 */
255 int va_error_reporter(ERROR_REPORTER_CALLBACK_ARGS);
256
257 /**
258 Set error reporting callback function using this
259 function. If left unset, errors will be printed
260 to standard error, which is effectively what the
261 hitherto FPRINTF has done.
262 */
263 void error_reporter_set_callback(
264 const error_reporter_callback_t new_callback
265 );
266
267 #endif /* ASC_ERROR_H */

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