4

I have a variadic function:

LogWrite(FILE * fp, int level, const char * filename, const char * function, ...)

It should be called like this:

LogWrite(fp, int Level, __FILE__, __FUNCTION__, "Message: %s", message)

However, I want to write a variadic macro to make it easier to call:

1: #define LOGWRITE_L1(...) LogWrite(file, 1, __FILE__, __FUNCTION__, ...)
or
2: #define LOGWRITE_L1(file, ...) LogWrite(file, 1, __FILE__, __FUNCTION__, ...)

This is so that a developer can call it using:

LOGWRITE_L1(myfile, "Hello!");

instead of

LogWrite(myfile, 1, __FILE__, __FUNCTION__, "Hello!")

However, both those methods give me a compiler error.

1: expected expression before '...'
2:'file' undeclared (first use in this function)

Is this possible or am I wasting my time? I have never used variadic functions before, so not sure. My function works...I called it using the complete declaration and it wrote to the files that I wanted it to.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Sagar
  • 9,456
  • 6
  • 54
  • 96

2 Answers2

7

You have to put __VA_ARGS__ as the replacement for the variadic arguments:

#define LOGWRITE_L1(file, ...) LogWrite(file, 1, __FILE__, __FUNCTION__, __VA_ARGS__)

This is only part of C99, C11 and C++11 standards. Some compilers offer extensions pertaining to the handling of variadic macros; check your manual.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
4

Use __VA_ARGS__ instead of ...

#define LOGWRITE_L1(file, ...) LogWrite(file, 1, __FILE__, __FUNCTION__, __VA_ARGS__)
paul
  • 1,212
  • 11
  • 15