A better way ...
Bruce Korb
korbb@datadesign.com
Wed Feb 25 20:56:00 GMT 1998
Kaveh R. Ghazi wrote:
> ... Eg:
>
> FATAL ((stderr, "%s, %d, %f", arg1, arg2, arg3));
> MESSAGE (verbose, (stderr, "%s, %d, %f", arg1, arg2, arg3));
>
> It gets expanded to "do {blah; blah; fprintf (...); blah; } while(0);"
> ...
> If y'all think this is a good idea, I can add some similar
> macros to a common header file and start converting gcc source to use
> them. Suggestions on how those macros would be specifically defined
> to suit gcc are welcome.
> ...
> /* Print message if <verbose> is >= <verbose_level>. */
> #define MESSAGE(verbose_level, body) \
> do { \
> if (!quiet && verbose >= (verbose_level)) \
> fprintf body; \
> } while (0)
>
> /* Report continuable error. */
> #define ERROR(body) \
> do { \
> fprintf ...
>
> /* Report fatal error and exit with status 1. Function never returns. */
> #define FATAL(body) \
> do { \
> fprintf ...; exit (1);...
Once upon a time, a couple of lives ago, I spent a lot of time
figuring out how to display certain system events in a specialized
SVR4 implementation. It seems if you want to go to the trouble
of re-engineering internal display messages, then you may as well
consider something very flexible. It is relatively easy to do
during a reengineering phase and not worth the bother at any other
time.
We used the eight priority levels defined in sys/syslog.h:
> /*
> * Priorities (these are ordered)
> */
> #define LOG_EMERG 0 /* system is unusable */
> #define LOG_ALERT 1 /* action must be taken immediately */
> #define LOG_CRIT 2 /* critical conditions */
> #define LOG_ERR 3 /* error conditions */
> #define LOG_WARNING 4 /* warning conditions */
> #define LOG_NOTICE 5 /* normal but significant condition */
> #define LOG_INFO 6 /* informational */
> #define LOG_DEBUG 7 /* debug-level messages */
I think I added "TRACE" after "NOTICE" and dropped "ALERT", however.
and we defined numerous "areas of interest" inside the OS.
"Areas of interest" may not be of interest for a compiler, tho.
When the OS was built, we defined the minimum priority required
for an event to be compiled in, and as it ran we could adjust
the priorities required for events to be emitted. In the end,
it looked something like this (for the 'FOO' area):
#define STMT(s) do { s } while(0)
#if (FOO_LEVEL >= LOG_ERR)
#define FOO_ERR(b) STMT( if (foo_level < LOG_ERR) break; \
fprintf b; \
if ((unsigned)foo_limit >= LOG_ERR) exit( 1 );
)
#else
#define FOO_ERR(b)
#endif
Obviously, somewhat different inside an OS, tho.
More information about the Gcc
mailing list