Macro's and variable length args

Started by
2 comments, last by REspawn 16 years, 3 months ago
Hi, I have a message log and its function to log a message is this...

void log(LogMessageType messageType, 
			 string message, 
			 const string fileName, 
			 const int lineNumber, 
			 const string functionName);


so I wrote a handy macro that i could call to log the message...

#define logMessage(type,message)(MessageLog::getInstance()->log(type, message, __FILE__, __LINE__, __FUNCTION__))


So today I added support so the log could take extra parameters such as in in so the message could be like this "Loaded %i files" and it works fine, but im wondering how i can pass the variable args via the macro? Thanks for any help. heres the new calls if they help with the problem...

void log(LogMessageType messageType, 
			 string message, 
			 const string fileName, 
			 const int lineNumber, 
			 const string functionName, ...);


#define logMessage(type,message,...)(MessageLog::getInstance()->log(type, message, __FILE__, __LINE__, __FUNCTION__))

Advertisement
Vararg macros are included in C99, but I don't know how supported that is. I think GCC has had an extension for this for some time.

Best to stick with more C++ style methods such as boost::format, or similar operator chaining methods.
I agree with rip-off, but if you really need to use variadic functions, and you're using Visual Studio 2005 or later, you can use the __VA_ARGS__ predefined macro which expands to whatever you have in the ... part of your macro.

See Variadic Macros.

But variadic functions are evil anyway...
Cheers guys

This topic is closed to new replies.

Advertisement