1. Classic approach
There are different ways to get function names in GCC C++ compilers.The identifier __func__ is used to get exact function name.__FUNCTION__ is another name for __func__.The identifier __func__ is implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration static const char __func__[] = “function-name”; appeared, where function-name is the name of the lexically-enclosing function.In GCC compilers if you want signature of function also you can use __PRETTY_FUNCTION__ .In MSVC _PRETTY_FUNCTION__ is not defined you can use __FUNCSIG__ instead.
Following example shows all this
#include <iostream>
using namespace std;
void LogInfo(const char* func, const std::string& msg)
{
cout <<"["<<func<<"] :"<<msg;
}
#define Log(msg) LogInfo(__PRETTY_FUNCTION__, msg)
class Calc
{
public:
int Add(int num1, int num2)
{
Log("Addition");
printf ("\n__FUNCTION__ = %s\n", __FUNCTION__);
printf ("\n__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
return num1+num2;
}
int Sub(int num1, int num2)
{
Log("Substraction");
printf ("\n__FUNCTION__ = %s\n", __FUNCTION__);
printf ("\n__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
return num1-num2;
}
};
int main()
{
Calc cal;
cal.Add(10,15);
cal.Add(100,50);
return 0;
}
Output of above code is shown below

2. C++ 20 approach
Using classic approach different compilers has different identifiers. C++ 20 onwards new method exists using std::source_location.
Following example shows this
#include <source_location>
void logInfo(const std::string& message,
const std::source_location& location = std::source_location::current())
{
cout <<"["<<location.function_name()<<"] :"<<message;
}
std::source_location has memeber function like file_name and line to get calling file name and line number respectively.