Sign in to follow this  

Macro's and variable length args

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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__))

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this