C++17 In Detail

30 December 2011

__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

C++17 In Detail
© 2017, Bartlomiej Filipek, Blogger platform
Disclaimer: Any opinions expressed herein are in no way representative of those of my employers. All data and information provided on this site is for informational purposes only. I try to write complete and accurate articles, but the web-site will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.