How to find the name of the calling function in C++

2 Answers

0 votes
#include <iostream>
 
#define LOG_CALLER() logCaller(__FUNCTION__)
 
void logCaller(const char* caller) {
    std::cout << "Called by: " << caller << std::endl;
}
 
void callerFunction() {
    LOG_CALLER();
}
 
int main() {
    callerFunction();
}
 
 
 
/*
run:
 
Called by: callerFunction
 
*/

 



answered Nov 7, 2025 by avibootz
0 votes
#include <iostream>

void callerFunction() {
   std::cout << "Called by: " << __FUNCTION__ << std::endl;
}

int main() {
   callerFunction();
}


/*
run:

Called by: callerFunction

*/

 



answered Nov 7, 2025 by avibootz
...