9 * This module manages the production of diagnostic messages.
13 #include <stdio.h> /* FILE */
22 ** Different types of diagnostics.
24 typedef enum {DIAGLOG, DIAGINFO, DIAGWARN, DIAGERROR, DIAGFATAL, DIAGDEBUG} DiagLevels;
27 #define DIAGSUPPRESS 0
30 * Maximum size for the message and for filenames
32 #define MAX_MESSAGE_LENGTH 512
33 #define MAX_FILENAME_LENGTH 512
36 ** Directs #level#-level diagnostic messages to the file #whereTo#. Messages
37 ** for any level may be supressed by passing DIAGSUPPRESS as the second
38 ** parameter to this routine. By default, all messages are suppressed.
41 DirectDiagnostics(DiagLevels level,
46 ** Returns the file to which #level#-level diagnostics are being directed. May
47 ** be used to save off the direction for a change/restore, or for writing out
48 ** non-message-based information.
51 DiagnosticsDirection(DiagLevels level);
55 ** Produces the #level#-level diagnostic message #message#, which is used as
56 ** the format string in a call to fprintf(). #sourcefilename# and
57 ** #sourcelinenumber# are appended to the message. Additional fprintf()
58 ** arguments may be passed after #message#.
61 PositionedDiagnostic(DiagLevels level,
69 ** Produces the #level#-level diagnostic message #message#, which is used as
70 ** the format string in a call to fprintf(). Additional fprintf()
71 ** arguments may be passed after #message#.
74 Diagnostic(DiagLevels level,
80 ** These macros are provided for the usual case where DIAGFATAL messages are
81 ** emitted just before aborting the program and DIAGERROR messages just before
82 ** returning a failure value from a function. The ERROR, WARN, INFO, and LOG
83 ** macros are just a handy shorthand. Unfortunately, the varargs concept
84 ** doesn't extend to macros, which means separate macros have to be defined for
85 ** various parameter counts. The peculiar use of do ... while here prevents
86 ** compilation problems in cases such as:
87 ** if (foo < bar) FAIL("ick!"); else something();
88 ** The embedded if (1) satisfies the picky compilers that complain about
89 ** execution not reaching the loop test otherwise.
91 #define ABORT(message) \
93 PositionedDiagnostic(DIAGFATAL,__FILE__, __LINE__,message); \
96 #define ABORT1(message,a) \
98 PositionedDiagnostic(DIAGFATAL,__FILE__,__LINE__,message,a); \
101 #define ABORT2(message,a,b) \
103 PositionedDiagnostic(DIAGFATAL,__FILE__,__LINE__,message,a,b); \
106 #define ABORT3(message,a,b,c) \
108 PositionedDiagnostic(DIAGFATAL,__FILE__,__LINE__,message,a,b,c); \
111 #define ABORT4(message,a,b,c,d) \
113 PositionedDiagnostic(DIAGFATAL,__FILE__,__LINE__,message,a,b,c,d); \
116 #define ABORT5(message,a,b,c,d,e) \
118 PositionedDiagnostic(DIAGFATAL,__FILE__,__LINE__,message,a,b,c,d,e); \
122 #define FAIL(message) \
124 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message); \
127 #define FAIL1(message,a) \
129 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a); \
132 #define FAIL2(message,a,b) \
134 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a,b); \
137 #define FAIL3(message,a,b,c) \
139 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a,b,c); \
142 #define FAIL4(message,a,b,c,d) \
144 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a,b,c,d); \
147 #define FAIL5(message,a,b,c,d,e) \
148 do {PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a,b,c,d,e); \
152 #define ERROR(message) \
153 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message)
154 #define ERROR1(message,a) \
155 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a)
156 #define ERROR2(message,a,b) \
157 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a,b)
158 #define ERROR3(message,a,b,c) \
159 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a,b,c)
160 #define ERROR4(message,a,b,c,d) \
161 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a,b,c,d)
162 #define ERROR5(message,a,b,c,d,e) \
163 PositionedDiagnostic(DIAGERROR,__FILE__,__LINE__,message,a,b,c,d,e)
165 #define WARN(message) \
166 PositionedDiagnostic(DIAGWARN,__FILE__,__LINE__,message)
167 #define WARN1(message,a) \
168 PositionedDiagnostic(DIAGWARN,__FILE__,__LINE__,message,a)
169 #define WARN2(message,a,b) \
170 PositionedDiagnostic(DIAGWARN,__FILE__,__LINE__,message,a,b)
171 #define WARN3(message,a,b,c) \
172 PositionedDiagnostic(DIAGWARN,__FILE__,__LINE__,message,a,b,c)
173 #define WARN4(message,a,b,c,d) \
174 PositionedDiagnostic(DIAGWARN,__FILE__,__LINE__,message,a,b,c,d)
175 #define WARN5(message,a,b,c,d,e) \
176 PositionedDiagnostic(DIAGWARN,__FILE__,__LINE__,message,a,b,c,d,e)
178 #define INFO(message) \
179 PositionedDiagnostic(DIAGINFO,__FILE__,__LINE__,message)
180 #define INFO1(message,a) \
181 PositionedDiagnostic(DIAGINFO,__FILE__,__LINE__,message,a)
182 #define INFO2(message,a,b) \
183 PositionedDiagnostic(DIAGINFO,__FILE__,__LINE__,message,a,b)
184 #define INFO3(message,a,b,c) \
185 PositionedDiagnostic(DIAGINFO,__FILE__,__LINE__,message,a,b,c)
186 #define INFO4(message,a,b,c,d) \
187 PositionedDiagnostic(DIAGINFO,__FILE__,__LINE__,message,a,b,c,d)
188 #define INFO5(message,a,b,c,d,e) \
189 PositionedDiagnostic(DIAGINFO,__FILE__,__LINE__,message,a,b,c,d,e)
191 #define LOG(message) \
192 PositionedDiagnostic(DIAGLOG,__FILE__,__LINE__,message)
193 #define LOG1(message,a) \
194 PositionedDiagnostic(DIAGLOG,__FILE__,__LINE__,message,a)
195 #define LOG2(message,a,b) \
196 PositionedDiagnostic(DIAGLOG,__FILE__,__LINE__,message,a,b)
197 #define LOG3(message,a,b,c) \
198 PositionedDiagnostic(DIAGLOG,__FILE__,__LINE__,message,a,b,c)
199 #define LOG4(message,a,b,c,d) \
200 PositionedDiagnostic(DIAGLOG,__FILE__,__LINE__,message,a,b,c,d)
201 #define LOG5(message,a,b,c,d,e) \
202 PositionedDiagnostic(DIAGLOG,__FILE__,__LINE__,message,a,b,c,d,e)
204 /* /usr/include/macros.h defines a DEBUG macro, so we use DDEBUG instead. */
205 #define DDEBUG(message) \
206 PositionedDiagnostic(DIAGDEBUG,__FILE__,__LINE__,message)
207 #define DDEBUG1(message,a) \
208 PositionedDiagnostic(DIAGDEBUG,__FILE__,__LINE__,message,a)
209 #define DDEBUG2(message,a,b) \
210 PositionedDiagnostic(DIAGDEBUG,__FILE__,__LINE__,message,a,b)
211 #define DDEBUG3(message,a,b,c) \
212 PositionedDiagnostic(DIAGDEBUG,__FILE__,__LINE__,message,a,b,c)
213 #define DDEBUG4(message,a,b,c,d) \
214 PositionedDiagnostic(DIAGDEBUG,__FILE__,__LINE__,message,a,b,c,d)
215 #define DDEBUG5(message,a,b,c,d,e) \
216 PositionedDiagnostic(DIAGDEBUG,__FILE__,__LINE__,message,a,b,c,d,e)