home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!sun-barr!cs.utexas.edu!usc!wupost!darwin.sura.net!jvnc.net!yale.edu!ira.uka.de!math.fu-berlin.de!zrz.tu-berlin.de!cs.tu-berlin.de!jutta
- From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
- Subject: Re: Variable arguments & macros...
- Message-ID: <1992Jul22.155429.14829@cs.tu-berlin.de>
- Keywords: macros, varargs
- Sender: news@cs.tu-berlin.de
- Organization: Techn. University of Berlin, Germany
- References: <markkj.711771545@munagin>
- Date: Wed, 22 Jul 1992 15:54:29 GMT
- Lines: 49
-
- markkj@mullian.ee.mu.OZ.AU (Mark Johnston) writes:
- > I'm wondering if anyone has any really devious ways of writing macros
- > so that variable arguments can be used. [..] I have a function
- >
- > int writeErrorMsg(int quit, char *file, int line, char *format, ...)
- [..]
- > #define error(x) writeErrorMsg(0, __FILE__, __LINE__, x)
- > #define fatal(x) writeErrorMsg(1, __FILE__, __LINE__, x)
- [...]
- > error("The error code is %1d.", errCode);
-
- I did something like that once, although the details are rather messy.
- In your error handling module, have three variables and two functions:
-
- #include <stdarg.h>
- #include <stdio.h>
-
- static char * file;
- static int line, level;
-
- static void writeError(char * fmt, ...) { /* from C FAQ part 6 */
- va_list ap;
- fprintf(stderr, "file \"%s\", line %d: ", file, line);
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap );
- va_end(ap);
- fprintf(stderr, "\n");
- if (level == 1) abort();
- }
-
- void (* locateError(int v, int l, char * f))(char *, ...) {
- level = v; file = f; line = l;
- return writeError;
- }
-
- Then declare and #define in a header file:
-
- extern void (* locateError(int, int, char *))(char *, ...);
-
- #define error (locateError(0, __LINE__, __FILE__))
- #define fatal (locateError(1, __LINE__, __FILE__))
-
- And use:
-
- error("switch failed (event.type==%lx)", (long)e.type);
- fatal("%s: failed to allocate %ld bytes.", progname, (long)size);
-
- Regards,
- Jutta (jutta@cs.tu-berlin.de)
-