/[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 377 - (show annotations) (download) (as text)
Wed Mar 15 11:27:22 2006 UTC (14 years, 2 months ago) by johnpye
File MIME type: text/x-chdr
File size: 8256 byte(s)
By default, no xterm console colour codes are used now.
More output in extfn test python script.
pygtk/interface/Makefile cleans up gcov files when 'make clean'.
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 Variadic macros to allow nice succint logging and error reporting
79 calls from C dialects that support them (GCC, C99 and others)
80
81 If you don't support variadic macros, you will still get the messages
82 but without the file/function/line number.
83 */
84 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
85 # define ERROR_REPORTER_DEBUG(args...) error_reporter(ASC_PROG_NOTE, __FILE__, __LINE__, __func__, ##args)
86 # define ERROR_REPORTER_HERE(SEV,args...) error_reporter(SEV,__FILE__, __LINE__, __func__, ##args)
87 # define ERROR_REPORTER_NOLINE(SEV,args...) error_reporter(SEV, NULL, 0, NULL, ##args)
88 # define CONSOLE_DEBUG(args...) (fprintf(stderr, ERR_BOLD "%s:%d (%s): ", __FILE__,__LINE__,__func__) + \
89 fprintf(stderr, ##args) + \
90 fprintf(stderr, ERR_NORM "\n"))
91
92 #elif defined(HAVE_C99)
93 # define ERROR_REPORTER_DEBUG(...) error_reporter(ASC_PROG_NOTE,__FILE__,__LINE__,__func__,## __VA_ARGS__)
94 # define ERROR_REPORTER_HERE(SEV,...) error_reporter(SEV,__FILE__,__LINE__,__func__, ## __VA_ARGS__)
95 # define ERROR_REPORTER_NOLINE(SEV,...) error_reporter(SEV,NULL,0,NULL, ## __VA_ARGS__)
96 # define CONSOLE_DEBUG(...) (fprintf(stderr, ERR_BOLD "%s:%d (%s): ", __FILE__,__LINE__,__func__) + \
97 fprintf(stderr, ##__VA_ARGS__) + \
98 fprintf(stderr, ERR_NORM "\n"))
99
100 #else
101 # define ERROR_REPORTER_DEBUG error_reporter_note_no_line
102 # define ERROR_REPORTER_HERE error_reporter_here
103 # define ERROR_REPORTER_NOLINE error_reporter_noline
104 # define CONSOLE_DEBUG console_debug
105 int error_reporter_note_no_line(const char *fmt,...);
106 int error_reporter_here(const error_severity_t sev, const char *fmt,...);
107 int error_reporter_noline(const error_severity_t sev, const char *fmt,...);
108 int console_debug(const char *fmt,...);
109 #endif
110
111 #define ERROR_REPORTER_START_NOLINE(SEV) error_reporter_start(SEV,NULL,0,NULL);
112 #define ERROR_REPORTER_START_HERE(SEV) error_reporter_start(SEV,__FILE__,__LINE__,__func__);
113
114 #define ERROR_REPORTER_STAT(sev,stat,msg) \
115 error_reporter(sev,Asc_ModuleFileName(stat->mod),stat->linenum,NULL,msg)
116
117 /**
118 Error severity codes. This will be used to visually
119 the seriousness of errors. ASC_PROG_ERRORs for example
120 might be red, or be highlighted with a (!) icon, etc.
121 */
122 typedef enum error_severity_enum{
123 ASC_USER_SUCCESS=0
124 ,ASC_USER_NOTE=1 /**< a note to the user */
125 ,ASC_USER_WARNING /**< the user has done something bad but tolerable */
126 ,ASC_USER_ERROR /**< the user has done something wrong */
127 ,ASC_PROG_NOTE /**< a note for the programmer */
128 ,ASC_PROG_WARNING /**< the program encounters an unexpected state */
129 ,ASC_PROG_ERROR /**< the program has failed but can ignore and continue (maybe) */
130 ,ASC_PROG_FATAL /**< fatal error, program will exit */
131 } error_severity_t;
132
133 /** An alias for ASC_PROG_ERROR */
134 #define ASC_PROG_ERR ASC_PROG_ERROR
135
136 #define ERROR_REPORTER_MAX_MSG 4096 /* no particular reason */
137
138 typedef struct{
139 unsigned char iscaching; /** set to true for fprintf_error_reporter to do its work */
140 error_severity_t sev;
141 const char *filename;
142 int line;
143 const char *func;
144 char msg[ERROR_REPORTER_MAX_MSG];
145 } error_reporter_meta_t;
146
147 /**
148 This is the drop-in replacement for Asc_FPrintf. Anythin you attempt
149 to print to stderr will be captured and passed to the error_reporter_callback
150 function for handling.
151 */
152 int fprintf_error_reporter(FILE *file, const char *fmt, ...);
153
154 /**
155 If file!=stderr, this will do the usual thing. If file==stderr, it will output
156 the character via fprintf_error_reporter.
157 */
158 int fputc_error_reporter(int c, FILE *file); /* just calls fprintf_error_reporter */
159
160 /**
161 This replaces the standard 'fflush' of Asc_FFlush. If file!=stderr, it will
162 call the standard fflush. If file==stderr, it will call error_reporter_end_flush.
163 */
164 int fflush_error_reporter(FILE *file);
165
166 /**
167 Start a cached error report. This means that multiple frprintf_error_reporter calls will
168 be stored in a global string until an error_reporter_end_flush is encountered.
169 */
170 int error_reporter_start(const error_severity_t sev, const char *filename, const int line, const char *func);
171
172 /**
173 Output the contents of the checked global string as an error report
174 */
175 int error_reporter_end_flush();
176
177 /**
178 This #define saves you typing the list of arguments in your
179 callback function declarations.
180 */
181 #define ERROR_REPORTER_CALLBACK_ARGS \
182 const error_severity_t sev \
183 , const char *filename \
184 , const int line \
185 , const char *funcname \
186 , const char *fmt \
187 , const va_list args
188
189 /*
190 In you have functions which pass-through callback parameters,
191 this #define ensures that if their ordering/naming changes,
192 you won't have to go hunting and change stuff.
193 */
194 #define ERROR_REPORTER_CALLBACK_VARS \
195 sev, filename, line, funcname, fmt, args
196
197 /*
198 Define the type of the function pointer to be used for all
199 error reporting functions. The final argument is a va_list.
200 You should use 'vsnprintf' of 'vfprintf' to output your
201 message to the desired file or string, see <stdio.h> for these.
202 */
203 typedef int (*error_reporter_callback_t)(
204 ERROR_REPORTER_CALLBACK_ARGS
205 );
206
207 /**
208 Use this function directly for 'richer' reporting of
209 of error messages.
210
211 @return follows the style of fprintf
212 */
213 DLEXPORT int error_reporter(
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 /**
223 This format of the error reporter is useful if you must call it
224 from another variable-argument-list function.
225 */
226 int va_error_reporter(ERROR_REPORTER_CALLBACK_ARGS);
227
228 /**
229 Set error reporting callback function using this
230 function. If left unset, errors will be printed
231 to standard error, which is effectively what the
232 hitherto FPRINTF has done.
233 */
234 void error_reporter_set_callback(
235 const error_reporter_callback_t new_callback
236 );
237
238 #endif /* ASC_ERROR_H */

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