/[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 190 - (show annotations) (download) (as text)
Mon Jan 16 08:53:30 2006 UTC (14 years, 2 months ago) by johnpye
File MIME type: text/x-chdr
File size: 7015 byte(s)
- Simplifying many 'error_reporter' calls with new macros:
    ERROR_REPORTER_NOLINE and ERROR_REPORTER_HERE
- Adding some cygwin cases in the PyGTK build.
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 ERROR_REPORTER_NOLINE(SEV,MSG,args...) error_reporter(SEV,NULL,0,MSG, ##args)
62 # define CONSOLE_DEBUG(MSG,args...) fprintf(stderr,"%s:%d (%s): " MSG "\n", __FILE__,__LINE__,__func__, ##args)
63
64 #elif defined(HAVE_C99)
65 # define ERROR_REPORTER_DEBUG(MSG,...) error_reporter(ASC_PROG_NOTE,__FILE__,__LINE__,"%s: " MSG,__func__,## __VA_ARGS__)
66 # define ERROR_REPORTER_HERE(SEV,MSG,...) error_reporter(SEV,__FILE__,__LINE__,"%s: " MSG, __func__, ## __VA_ARGS__)
67 # define ERROR_REPORTER_NOLINE(SEV,MSG,...) error_reporter(SEV,NULL,0,MSG, ## __VA_ARGS__)
68 # define CONSOLE_DEBUG(MSG,...) fprintf(stderr,"%s:%d (%s): " MSG "\n", __FILE__,__LINE__,__func__, ## __VA_ARGS__)
69
70 #else
71 # define ERROR_REPORTER_DEBUG error_reporter_note_no_line
72 # define ERROR_REPORTER_HERE error_reporter_here
73 # define ERROR_REPORTER_NOLINE error_reporter_noline
74 # define CONSOLE_DEBUG console_debug
75 int error_reporter_note_no_line(const char *fmt,...);
76 int error_reporter_here(const error_severity_t sev, const char *fmt,...);
77 int error_reporter_noline(const error_severity_t sev, const char *fmt,...);
78 int console_debug(const char *fmt,...);
79 #endif
80
81 #define ERROR_REPORTER_STAT(sev,stat,msg) \
82 error_reporter(sev,Asc_ModuleFileName(stat->mod),stat->linenum,msg)
83
84 /**
85 Error severity codes. This will be used to visually
86 the seriousness of errors. ASC_PROG_ERRORs for example
87 might be red, or be highlighted with a (!) icon, etc.
88 */
89 typedef enum error_severity_enum{
90 ASC_USER_SUCCESS=0
91 ,ASC_USER_NOTE=1 /**< a note to the user */
92 ,ASC_USER_WARNING /**< the user has done something bad but tolerable */
93 ,ASC_USER_ERROR /**< the user has done something wrong */
94 ,ASC_PROG_NOTE /**< a note for the programmer */
95 ,ASC_PROG_WARNING /**< the program encounters an unexpected state */
96 ,ASC_PROG_ERROR /**< the program has failed but can ignore and continue (maybe) */
97 ,ASC_PROG_FATAL /**< fatal error, program will exit */
98 } error_severity_t;
99
100 /** An alias for ASC_PROG_ERROR */
101 #define ASC_PROG_ERR ASC_PROG_ERROR
102
103 #define ERROR_REPORTER_MAX_MSG 4096 /* no particular reason */
104
105 typedef struct{
106 unsigned char iscaching; /** set to true for fprintf_error_reporter to do its work */
107 error_severity_t sev;
108 const char *filename;
109 int line;
110 char msg[ERROR_REPORTER_MAX_MSG];
111 } error_reporter_meta_t;
112
113 /**
114 This is the drop-in replacement for Asc_FPrintf. Anythin you attempt
115 to print to stderr will be captured and passed to the error_reporter_callback
116 function for handling.
117 */
118 int fprintf_error_reporter(FILE *file, const char *fmt, ...);
119
120 /**
121 If file!=stderr, this will do the usual thing. If file==stderr, it will output
122 the character via fprintf_error_reporter.
123 */
124 int fputc_error_reporter(int c, FILE *file); /* just calls fprintf_error_reporter */
125
126 /**
127 This replaces the standard 'fflush' of Asc_FFlush. If file!=stderr, it will
128 call the standard fflush. If file==stderr, it will call error_reporter_end_flush.
129 */
130 int fflush_error_reporter(FILE *file);
131
132 /**
133 Start a cached error report. This means that multiple frprintf_error_reporter calls will
134 be stored in a global string until an error_reporter_end_flush is encountered.
135 */
136 int error_reporter_start(const error_severity_t sev, const char *filename, const int line);
137
138 /**
139 Output the contents of the checked global string as an error report
140 */
141 int error_reporter_end_flush();
142
143 /**
144 This #define saves you typing the list of arguments in your
145 callback function declarations.
146 */
147 #define ERROR_REPORTER_CALLBACK_ARGS \
148 const error_severity_t sev \
149 , const char *filename, const int line \
150 , const char *fmt, const va_list args
151
152 /*
153 In you have functions which pass-through callback parameters,
154 this #define ensures that if their ordering/naming changes,
155 you won't have to go hunting and change stuff.
156 */
157 #define ERROR_REPORTER_CALLBACK_VARS \
158 sev, filename, line, fmt, args
159
160 /*
161 Define the type of the function pointer to be used for all
162 error reporting functions. The final argument is a va_list.
163 You should use 'vsnprintf' of 'vfprintf' to output your
164 message to the desired file or string, see <stdio.h> for these.
165 */
166 typedef int (*error_reporter_callback_t)(
167 ERROR_REPORTER_CALLBACK_ARGS
168 );
169
170 /**
171 Use this function directly for 'richer' reporting of
172 of error messages.
173
174 @return follows the style of fprintf
175 */
176 int error_reporter(
177 const error_severity_t sev
178 , const char *errfile, const int errline
179 , const char *fmt, ...
180 );
181
182 /**
183 This format of the error reporter is useful if you must call it
184 from another variable-argument-list function.
185 */
186 int va_error_reporter(ERROR_REPORTER_CALLBACK_ARGS);
187
188 /**
189 Set error reporting callback function using this
190 function. If left unset, errors will be printed
191 to standard error, which is effectively what the
192 hitherto FPRINTF has done.
193 */
194 void error_reporter_set_callback(
195 const error_reporter_callback_t new_callback
196 );
197
198 #endif /* ASC_ERROR_H */

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