Home:ALL Converter>macros with variable number of arguments

macros with variable number of arguments

Ask Time:2012-10-10T19:17:21         Author:unresolved_external

Json Formatter

I've been developing application which uses logging mechanism, and I implemented sort of printf function which takes various number of arguments and prints to log certain message, what I want is to add a function name to this function, but I dont what to write this argument in each function call.

So I decided to write a macros, turns out it not so easy

#define WriteToLogParams(szMessage, nLogLevel, param1, param2) WriteToLogParamsFunc(szMessage, __FUNCDNAME__, nLogLevel, param1, param2)

First I thought that there is some kind of macros overloading and I can easily do it , but turns out if I write another macros with the same name but with different number of argument it wont compile. So to make it work I should make each macros name unique.

So is there are any intelligent way to do this?

Thank you on advance.

Author:unresolved_external,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12818049/macros-with-variable-number-of-arguments
tozka :

You can use __VA_ARGS__ macro \n\nfor example:\n\nWriteToLogParamsFunc(const char *__file, int __line, const char* __func, int nLogLevel, const char *szMessage, ...);\n\n#define WriteToLogParams(nLogLevel, szMessage, ...) WriteToLogParamsFunc(__FILE__, __LINE__, __FUNCTION__, nLogLevel, szMessage, __VA_ARGS__ )\n",
2012-10-10T11:26:49
yy