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

Get my free ebook about C++17!

More than 50 pages about the new Language Standard.

C++17 in detail, by Bartlomiej Filipek

C++17 In Detail
© 2017, Bartlomiej Filipek, Blogger platform
Any opinions expressed herein are in no way representative of those of my employers.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.