5

I am exploring a large code-base and i am not a gdb fan. I would like add a LOG(INFO) << __PRETTY_FUNCTION__ in the first line of each function in the code-base. But that's very tedious. Does anyone know a hack to make all function calls to print a LOG message with its function name?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1159517
  • 5,390
  • 8
  • 30
  • 47
  • I believe GCC lets you install a function to be called at the beginning of every other function. – chris Jan 18 '14 at 19:49
  • can write small script to do that. – Mzf Jan 18 '14 at 19:52
  • I doubt you actually want this; the output will be so verbose & ugly it won't be helpful. Why not use a dedicated profiling tool to instrument this for you, or go at it from the other direction like Dieter's solution below? – tenfour Jan 18 '14 at 20:28
  • possible duplicate of [Automatically adding Enter/Exit Function Logs to a Project](http://stackoverflow.com/questions/2281739/automatically-adding-enter-exit-function-logs-to-a-project) – Jonathan Leffler Jan 18 '14 at 20:29

2 Answers2

3

I do 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();
}
0

Seems a duplicate of Automatically adding Enter/Exit Function Logs to a Project

However, You can consider using gprof that has the capability to generate a runtime call tree. You can have dot graphs, which might be easier to read.

Community
  • 1
  • 1
Pluc
  • 901
  • 8
  • 21