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
    feasible?
     
    Neo, Dec 1, 2006
    #1
    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 \
    X PICK_A_REALLY_UNLIKELY_NAME ( __FILE__, __LINE__ ); {}

    void dummy ( void ) {
    ENTER_SCOPE;
    }

    int main ( void ) {
    ENTER_SCOPE;
    dummy();
    }



    Best

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

  3. Neo

    Nate Barney Guest

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

    Nate
     
    Nate Barney, Dec 1, 2006
    #3
  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.


    Best

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

    Nate Barney Guest

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

    Nate
     
    Nate Barney, Dec 1, 2006
    #5
  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
    #6
  7. Neo

    Ron Natalie Guest

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

    BobR Guest

    Add:
    You could try:
    __func__

    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
    #8
    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.