According to this answer we can find the name of the calling function using __builtin__FUNCTION()
in GCC. Is there MSVC equivalent for this?
Yes, such intrinsics (__builtin_FILE()
, __builtin_FUNCTION()
, __builtin_LINE()
, __builtin_COLUMN()
) were added for VS 2019 16.6 Preview 2 (according to this link) to support c++20 std::source_location
.
std::source_location
will be the cross platform solution in the future allowing you to do:
void log(const std::string& message, const std::experimental::source_location& location = std::experimental::source_location::current())
{
std::cout << location.function_name() << ": " << message << "\n";
}
int main()
{
log("test");
}
Until this is available the best solution I am aware of is to use macros to capture the value of __FUNCTION__
and pass it to your function. For example something like this:
void log(const std::string& message, const std::string& function)
{
}
#define LOG(message) log(message, __FUNCTION__)
int main()
{
LOG("test");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With