How can I print every function name when a function is called in C ++?

I am learning a large code base and I am not a fan of gdb. I would like to add LOG(INFO) << __PRETTY_FUNCTION__in the first line of each function in the code base. But it is very tiring. Does anyone know to crack all function calls to print a LOG message with its function name?

+4
source share
2 answers

I am doing something similar to:

#include <iostream>

class LogScope
{
    public:
    LogScope(const char* scope, const char* file, int line = 0)
    :   m_scope(scope), m_file(file), m_line(line)
    {
        std::clog << "[Begin] " << m_scope << ", " << m_file << ", " << m_line << std::endl;
    }

    ~LogScope() {
        std::clog << "[End]   "  << m_scope << ", " << m_file << ", " << m_line << std::endl;
    }

    private:
    const char* m_scope;
    const char* m_file;
    int m_line;
};

#define NAME_AT_LINE_2(Name, Line) Name##_##Line
#define NAME_AT_LINE_1(Name, Line) NAME_AT_LINE_2(Name, Line)
#define NAME_AT_LINE(Name) NAME_AT_LINE_1(Name, __LINE__)

#define LOG_SCOPE \
    ::LogScope NAME_AT_LINE(log_scope)(__FUNCTION__, __FILE__, __LINE__)

void f() {
    LOG_SCOPE;
}

int main() {
    LOG_SCOPE;
    f();
}
+2
source

All Articles