Hi I'd like to write a macro for the function with a variable argument, for
example

#define PrivateLog(szFormat,...) PublicLogWrite(_TODAY_,szFormat,...)
// where _TODAY_ is a static string containing e.g. "May 23, 2007"

void PublicLogWrite( char *szToday, char *szFmt, ... )
{
va_list arglist;
va_start( arglist, szFmt );
// ... e.g. vsprintf()
va_end( arglist );
// ... e.g. fprintf()
}

so the caller should look like,
PrivateLog( "Error:%s ReturnCode=%d", szErrorMessage, nError );
PrivateLog( "Info:%s Len=%d #Lines=%d", szInfoMessage, nLen, nNumLines );

But those 2 lines generates the compilation errors:
error C2059: syntax error : '...'

Anyone knows how to resolve this?
TIA.

Re: C macro for ellipsis(...) by Igor

Igor
Wed May 23 08:40:27 CDT 2007

<Lee> wrote in message
news:B6BC5926-CFE3-4979-8176-ADE3F82E2823@microsoft.com
> Hi I'd like to write a macro for the function with a variable
> argument, for example
>
> #define PrivateLog(szFormat,...) PublicLogWrite(_TODAY_,szFormat,...)
> // where _TODAY_ is a static string containing e.g. "May 23, 2007"
>
> void PublicLogWrite( char *szToday, char *szFmt, ... )
> {
> va_list arglist;
> va_start( arglist, szFmt );
> // ... e.g. vsprintf()
> va_end( arglist );
> // ... e.g. fprintf()
> }

VC doesn't support variadic macros. I'd do something like this:

void VPublicLogWrite( char *szToday, char *szFmt, va_list l)
{
// ... e.g. vsprintf()
}

void PublicLogWrite( char *szToday, char *szFmt, ... )
{
va_list arglist;
va_start( arglist, szFmt );
VPublicLogWrite(szToday, szFmt, arglist);
va_end( arglist );
}

void PrivateLog( char *szFmt, ... )
{
va_list arglist;
va_start( arglist, szFmt );
VPublicLogWrite(_TODAY_, szFmt, arglist);
va_end( arglist );
}

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: C macro for ellipsis(...) by Lee>

Lee>
Wed May 23 09:13:01 CDT 2007

Thank you Igor for your reply.
What I am really doing is to write a function for the error report with the
line # and the source filename.
To be more specific, the above PublicLogWrite() is as follows:

void PublicLogWrite( long nLineNumber, char *szCPPname, char *szFmt, ... )
{
va_list arglist;
va_start( arglist, szFmt );
// ... e.g. vsprintf()
va_end( arglist );
// ... e.g. fprintf() to report lin# and caller's CPP filename.
}

When I wrote it initially, that function didn't have the first two
parameters, and used __LINE__ and __FILE__ inside the function.
Although I made that function be inline, as long as it is stored in a
separate file (e.g. H or CPP), __LINE__ and __FILE__ were refering to the
separate file.
So I was trying to use a macro, such as,

#define PrivateLog(szFormat,...)
PublicLogWrite(__LINE__,__FILE__,szFormat,...)
so that other developers didn't have to put the extra two parameters.

Any other suggestion?
Thanks again.

Re: C macro for ellipsis(...) by Alex

Alex
Wed May 23 09:20:54 CDT 2007

"Igor Tandetnik" wrote:
> VC doesn't support variadic macros.

Actually, it does since VC++ 2005:

"Variadic Macros"
http://msdn2.microsoft.com/pt-br/library/ms177415(VS.80).aspx

Alex


Re: C macro for ellipsis(...) by Lee>

Lee>
Wed May 23 09:42:01 CDT 2007

Thank you all.

> #define PrivateLog(szFormat,...) PublicLogWrite(_TODAY_,szFormat,...)

After I changed it to the following, it works.
#define PrivateLog(szFormat,...) PublicLogWrite(_TODAY_,szFormat,__VA_ARGS__)




"Alex Blekhman" wrote:

> "Igor Tandetnik" wrote:
> > VC doesn't support variadic macros.
>
> Actually, it does since VC++ 2005:
>
> "Variadic Macros"
> http://msdn2.microsoft.com/pt-br/library/ms177415(VS.80).aspx
>
> Alex
>
>

Re: C macro for ellipsis(...) by Igor

Igor
Wed May 23 10:46:49 CDT 2007

Lee wrote:
> Thank you Igor for your reply.
> What I am really doing is to write a function for the error report
> with the line # and the source filename.
> To be more specific, the above PublicLogWrite() is as follows:
>
> void PublicLogWrite( long nLineNumber, char *szCPPname, char *szFmt,
> ... ) {
> va_list arglist;
> va_start( arglist, szFmt );
> // ... e.g. vsprintf()
> va_end( arglist );
> // ... e.g. fprintf() to report lin# and caller's CPP filename.
> }
>
> When I wrote it initially, that function didn't have the first two
> parameters, and used __LINE__ and __FILE__ inside the function.

I see you got your answer. In case you are curious how one can do it
without variadic macros (e.g. to support VC7), consider this:

struct LogHelper {
int line;
const char* file;

LogHelper(int l, const char* f) : line(l), file(f) {}

void operator() const (const char* format, ...) {
va_list arglist;
va_start( arglist, format );
VPublicLogWrite(line, file, format, arglist);
va_end( arglist );
}
};

#define PrivateLog LogHelper(__LINE__, __FILE__)


For a more elaborate example, see ATLTRACE in atltrace.h
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: C macro for ellipsis(...) by aslan

aslan
Thu May 24 04:10:42 CDT 2007

<Lee>, iletisinde Å?unu yazdı,
news:599F1847-32F4-44F8-A287-2AC14FD6E55B@microsoft.com...
> Thank you Igor for your reply.
> What I am really doing is to write a function for the error report with
> the
> line # and the source filename.
> To be more specific, the above PublicLogWrite() is as follows:
>
> void PublicLogWrite( long nLineNumber, char *szCPPname, char *szFmt, ... )
> {
> va_list arglist;
> va_start( arglist, szFmt );
> // ... e.g. vsprintf()
> va_end( arglist );
> // ... e.g. fprintf() to report lin# and caller's CPP filename.
> }
>
> When I wrote it initially, that function didn't have the first two
> parameters, and used __LINE__ and __FILE__ inside the function.
> Although I made that function be inline, as long as it is stored in a
> separate file (e.g. H or CPP), __LINE__ and __FILE__ were refering to the
> separate file.
> So I was trying to use a macro, such as,
>
> #define PrivateLog(szFormat,...)
> PublicLogWrite(__LINE__,__FILE__,szFormat,...)
> so that other developers didn't have to put the extra two parameters.
>
> Any other suggestion?
> Thanks again.
If you always pass a literal string as szFormat, yo can try this

#define FMT(x) __LINE__ __FILE__ x

then for example:
PublicLogWrite( FMT("%s\n"), pszStr);
would be OK for you