Is there a C++ macro that obtains the current namespace and function name? Example:
namespace foo {
namespace bar {
void baz(int i, double d) {
std::cout << MACRO << std::endl;
}
}
}
would print foo::bar::baz
. I know of __FUNCTION__
but it doesn't give the namespace. And BOOST_CURRENT_FUNCTION
gives the whole signature, incl. arguments and return type:
void foo::bar::baz(int, double)
Maybe, is it possible to write a macro that extracts the namespace and function name from BOOST_CURRENT_FUNCTION
?
I want that for logging purposes, to get a logging string like
foo::bar::baz -- blah logging message blah
As far as I know, it's not possible (not portably). However from the full signature you can extract those arguments. Of course it requires parsing said signature... which is not so easy :x
Here is the function I use at the moment:
And its accompagnying tests:
It works in combination with gcc's
__func__
macro.The
StringRef
class is similar tollvm::StringRef
.I think it should cover your needs if you can afford the extra parsing. It's quite fast: no backtracking and no dynamic allocation, so should not be an issue (especially compared to writing to a file...).
Hope it helps.