/[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 188 - (show annotations) (download) (as text)
Mon Jan 16 07:47:02 2006 UTC (18 years, 6 months ago) by johnpye
File MIME type: text/x-chdr
File size: 6714 byte(s)
Adding some missing 'EXT' code, adding 'error_reporter_here' functionality.
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 This nice macro on GNU C allows quick-and-dirty debug error
53 messages using the ERROR_REPORTER_DEBUG macro. On non GNU C
54 systems, you will still get the error messages, but the location
55 of the error won't be reported, because you don't support
56 variadic macros.
57 */
58 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
59 # define ERROR_REPORTER_DEBUG(MSG,args...) error_reporter(ASC_PROG_NOTE,__FILE__,__LINE__,"%s: " MSG, __func__, ##args)
60 # define ERROR_REPORTER_HERE(SEV,MSG,args...) error_reporter(SEV,__FILE__,__LINE__,"%s: " MSG, __func__, ##args)
61 # define CONSOLE_DEBUG(MSG,args...) fprintf(stderr,"%s:%d (%s): " MSG "\n", __FILE__,__LINE__,__func__, ##args)
62 #elif defined(HAVE_C99)
63 # define ERROR_REPORTER_DEBUG(MSG,...) error_reporter(ASC_PROG_NOTE,__FILE__,__LINE__,"%s: " MSG, __func__, ## __VA_ARGS__)
64 # define ERROR_REPORTER_HERE(SEV,MSG,...) error_reporter(SEV,__FILE__,__LINE__,"%s: " MSG, __func__, ## __VA_ARGS__)
65 # define CONSOLE_DEBUG(MSG,...) fprintf(stderr,"%s:%d (%s): " MSG "\n", __FILE__,__LINE__,__func__, ## __VA_ARGS__)
66 #else
67 # define ERROR_REPORTER_DEBUG error_reporter_note_no_line
68 # define ERROR_REPORTER_HERE error_reporter_here
69 # define CONSOLE_DEBUG console_debug
70 int error_reporter_note_no_line(const char *fmt,...);
71 int error_reporter_here(const error_severity_t sev, const char *fmt,...);
72 int console_debug(const char *fmt,...);
73 #endif
74
75 #define ERROR_REPORTER_STAT(sev,stat,msg) \
76 error_reporter(sev,Asc_ModuleFileName(stat->mod),stat->linenum,msg)
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 /** An alias for ASC_PROG_ERROR */
95 #define ASC_PROG_ERR ASC_PROG_ERROR
96
97 #define ERROR_REPORTER_MAX_MSG 4096 /* no particular reason */
98
99 typedef struct{
100 unsigned char iscaching; /** set to true for fprintf_error_reporter to do its work */
101 error_severity_t sev;
102 const char *filename;
103 int line;
104 char msg[ERROR_REPORTER_MAX_MSG];
105 } error_reporter_meta_t;
106
107 /**
108 This is the drop-in replacement for Asc_FPrintf. Anythin you attempt
109 to print to stderr will be captured and passed to the error_reporter_callback
110 function for handling.
111 */
112 int fprintf_error_reporter(FILE *file, const char *fmt, ...);
113
114 /**
115 If file!=stderr, this will do the usual thing. If file==stderr, it will output
116 the character via fprintf_error_reporter.
117 */
118 int fputc_error_reporter(int c, FILE *file); /* just calls fprintf_error_reporter */
119
120 /**
121 This replaces the standard 'fflush' of Asc_FFlush. If file!=stderr, it will
122 call the standard fflush. If file==stderr, it will call error_reporter_end_flush.
123 */
124 int fflush_error_reporter(FILE *file);
125
126 /**
127 Start a cached error report. This means that multiple frprintf_error_reporter calls will
128 be stored in a global string until an error_reporter_end_flush is encountered.
129 */
130 int error_reporter_start(const error_severity_t sev, const char *filename, const int line);
131
132 /**
133 Output the contents of the checked global string as an error report
134 */
135 int error_reporter_end_flush();
136
137 /**
138 This #define saves you typing the list of arguments in your
139 callback function declarations.
140 */
141 #define ERROR_REPORTER_CALLBACK_ARGS \
142 const error_severity_t sev \
143 , const char *filename, const int line \
144 , const char *fmt, const va_list args
145
146 /*
147 In you have functions which pass-through callback parameters,
148 this #define ensures that if their ordering/naming changes,
149 you won't have to go hunting and change stuff.
150 */
151 #define ERROR_REPORTER_CALLBACK_VARS \
152 sev, filename, line, fmt, args
153
154 /*
155 Define the type of the function pointer to be used for all
156 error reporting functions. The final argument is a va_list.
157 You should use 'vsnprintf' of 'vfprintf' to output your
158 message to the desired file or string, see <stdio.h> for these.
159 */
160 typedef int (*error_reporter_callback_t)(
161 ERROR_REPORTER_CALLBACK_ARGS
162 );
163
164 /**
165 Use this function directly for 'richer' reporting of
166 of error messages.
167
168 @return follows the style of fprintf
169 */
170 int error_reporter(
171 const error_severity_t sev
172 , const char *errfile, const int errline
173 , const char *fmt, ...
174 );
175
176 /**
177 This format of the error reporter is useful if you must call it
178 from another variable-argument-list function.
179 */
180 int va_error_reporter(ERROR_REPORTER_CALLBACK_ARGS);
181
182 /**
183 Set error reporting callback function using this
184 function. If left unset, errors will be printed
185 to standard error, which is effectively what the
186 hitherto FPRINTF has done.
187 */
188 void error_reporter_set_callback(
189 const error_reporter_callback_t new_callback
190 );
191
192 #endif /* ASC_ERROR_H */

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