Last Update:
__FUNCTION__ macro in Visual C++
Recently, I’ve found something interesting in Visual Studio that improved my Logger!
I had a function called AddMsg
/** appends message to the log file */
void AddMsg(LOG_MODE m, int level, char* strModule, char *strMsg, ...);
But, when you wanted to add a comment you needed to use quite complicated and long syntnax, like:
/** one need to pass function name... quite boring task */
gLogger->AddMsg(lmNormal, 0, "class::func", "bla... param = %d", param);
// gLogger is a singleton...
But, there is a very useful define in Visual Studio, called __FUNCTION__ that returns name of a current function. So one can use it to automate passing function name to the logMsg function. To do that, I needed to define macro:
#define LOG(m, l, ...) gLogger->AddMsg(m, l, __FUNCTION__, __VA_ARGS__)
And now one can simply write:
LOG(lmNormal, "bla bla bla... param = %d", param);
In that way logging becomes a bit more simple…
Some more info
- __VA_ARGS__ can be used in macros that support “…” format, like for the printf function.
- In GCC there is also the same macro, but more documentation here
- More macros in VS: link to msdn documentation
I've prepared a valuable bonus for you!
Learn all major features of recent C++ Standards on my Reference Cards!
Check it out here: