| From: | Mike Mascari <mascarm(at)mascari(dot)com> | 
|---|---|
| To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> | 
| Cc: | Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, Peter Eisentraut <peter_e(at)gmx(dot)net>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> | 
| Subject: | Re: elog() proposal | 
| Date: | 2002-02-23 10:03:39 | 
| Message-ID: | 3C7768FB.91E6C6D8@mascari.com | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-hackers | 
Tom Lane wrote:
> 
> Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)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(at)mascari(dot)com
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Bradley Baetz | 2002-02-23 10:21:07 | |
| Previous Message | Oleg Bartunov | 2002-02-23 08:46:59 | Re: new module contrib/tree for 7.2 ? |