Re: elog() proposal - Mailing list pgsql-hackers

From Mike Mascari
Subject Re: elog() proposal
Date
Msg-id 3C7768FB.91E6C6D8@mascari.com
Whole thread Raw
In response to Re: elog() proposal  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-hackers
Tom Lane wrote:
> 
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > OK, so elog(ERROR, ...) and PGError(msg, ...) would be the same.  Makes
> > sense.  Should we consider hiding these in macros so they really still
> > call elog(ERROR, ...) for backward compatiblity?
> 
> I would love to make them macros, but I don't know a portable way to
> create macros with variable numbers of arguments.  Do you feel like
> writing double parens?
> 
>         PGERROR((msg, ...))

I'm not sure about complete portability, but the trick that I use works
under both Linux/gcc and Windows/Visual C++. It is something like this:

#define elog mkError(__FILE__, __LINE__)

I then have the following prototypes:

----

typedef void (*throwError_ptr) (const char *message, ...);
throwError_ptr mkError(const char *file, int line);
void throwError(const char *message, ...);

-----

The implementation looks like:

throwError_ptr mkError(const char *file, int line) {
sprintf(global_message, "File: %s\nLine: %i\n", file, line);return (throwError)

}


void throwError(const char *message, ...) {
va_list arg_ptr;char buffer[MAX_MESSAGE];
va_start(arg_ptr, message);vsnprintf(buffer, sizeof(buffer), message, arg_ptr);va_end(arg_ptr);
fprintf(stderr, "Error: %s\n%s", global_message, message); 
}

-----

So, I can write:

...
if (i < 0) {
elog("Illegal index specified: %i", i);

}
...

which becomes
mkError(__FILE__, __LINE__) ("Illegal index specified: %i", i);


Shouldn't that be portable?

Mike Mascari
mascarm@mascari.com


pgsql-hackers by date:

Previous
From: Sean Chittenden
Date:
Subject: IANA postgres service type-o!
Next
From: Tom Lane
Date:
Subject: Re: elog() proposal