Adding Prolog to Epilog

Discussion in 'C++' started by Neo, Dec 1, 2006.

  1. Neo

    Neo Guest

    I want to log information for a function call like

    void Add(void)

    When above function gets called, it should output strings like

    Entering Add
    Exiting Add

    So every function call should get logged. I want to add this
    funcionality to each function I am going to write and it should get
    added implicitly.
    Can I create function prolog and epilogs? How to do that ? Is that
    Neo, Dec 1, 2006
    1. Advertisements

  2. Neo

    Kai-Uwe Bux Guest

    Well, almost. The tricky part is the function name. If you are content with
    file name and line number, you can write a macro along the following lines:

    #include <iostream>
    #include <string>
    #include <sstream>

    struct X {

    std::string msg;

    X ( char const * file, unsigned long line )
    std::stringstream str;
    str << file << " [" << line << "]";
    msg = str.str();
    std::cout << "enter:" << msg << '\n';

    ~X ( void ) {
    std::cout << "leave:" << msg << '\n';


    #define ENTER_SCOPE \

    void dummy ( void ) {

    int main ( void ) {


    Kai-Uwe Bux
    Kai-Uwe Bux, Dec 1, 2006
    1. Advertisements

  3. Neo

    Nate Barney Guest

    Am I missing something? Is there a reason __FUNCTION__ won't work?

    Nate Barney, Dec 1, 2006
  4. Neo

    Kai-Uwe Bux Guest

    The reason is: there is no __FUNCTION__ macro in C++. If your compiler
    supports it then it does so as an extension.


    Kai-Uwe Bux
    Kai-Uwe Bux, Dec 1, 2006
  5. Neo

    Nate Barney Guest

    Ah, wasn't aware of that. I am duly humbled.

    Nate Barney, Dec 1, 2006
  6. Neo

    Jack Klein Guest

    Yes, there is. It's not a part of the C++ language, and it only
    exists as a non-standard extension on some implementations.
    Jack Klein, Dec 2, 2006
  7. Neo

    Ron Natalie Guest

    There's no such thing in C++.
    Ron Natalie, Dec 2, 2006
  8. Neo

    BobR Guest

    You could try:

    The GCC docs state that it is standard[1].

    [1] -
    ....... __func__ is defined by the ISO standard C99:

    Yeah, I know, C is not C++. :-}
    BobR, Dec 2, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.