How to get name of calling function

Discussion in 'C++' started by Prashant, Jul 23, 2004.

  1. Prashant

    Prashant Guest

    Okay this is my problem. I have a function, MainDisplay(char*) that
    takes in an argument of a string to display. Simple. I want this
    function to also be able to display the function that called it. This
    is where i'm lost. So for example:

    #include <iostream>
    using namespace std;

    void MainDisplay(char* msg) {
    //here 'Function' is the name of the function that MainDisplay
    //was called from
    cout << Function << " wrote " << msg << endl;
    }

    int main() {
    MainDisplay("Hi");
    }

    //end

    I want to see something like:

    main() wrote Hi

    I've tried many things. One involved using C macros. For example:

    #include <iostream>
    using namespace std;

    #define DisplayMsg(msg) MainDisplay(__FUNCTION__, msg);

    void MainDisplay(char* Function, char* msg) {
    cout << Function << " wrote " << msg << endl;
    }

    int main() {
    DisplayMsg("Hi");
    }

    //end

    This has the following output:

    main wrote Hi.

    This seems to work fine. But SOMETIMES, I get a segmentation fault
    when I try to do any io streaming (not limited to cout). I have no
    idea why, but people have told me it's because it garbles up some
    areas of memory that its not supposed to. So, not being able to
    isolate the cause of the problem, I deemed it unsafe and unpredictable
    and decided to abandon this method.

    Next I tried this:

    #include <iostream>
    using namespace std;

    inline void MainDisplay(char* msg) {
    cout << __FUNCTION__ << " wrote " << msg << endl;
    }

    int main() {
    MainDisplay("Hi");
    }

    //end

    I came to this method after hours of searching for a solution. It
    said that the code for an inline function gets replaced wherever the
    call is made. This would imply that the call MainDisplay("Hi") would
    be replaced entirely by
    cout << __FUNCTION__ << " wrote " << msg << endl;
    but to my dismay the output was:

    MainDisplay wrote Hi

    Is there a solution to this without using C macros? Is there even a
    stable solution at all? Why don't inline functions work like i'm
    expecting them to?

    Thanks,

    Prashant
    Prashant, Jul 23, 2004
    #1
    1. Advertising

  2. Prashant wrote:
    > Okay this is my problem. I have a function, MainDisplay(char*) that
    > takes in an argument of a string to display.


    Shouldn't it be MainDisplay(char const *)?


    > Simple. I want this
    > function to also be able to display the function that called it. This
    > is where i'm lost. So for example:
    >
    > #include <iostream>
    > using namespace std;
    >
    > void MainDisplay(char* msg) {
    > //here 'Function' is the name of the function that MainDisplay
    > //was called from
    > cout << Function << " wrote " << msg << endl;
    > }
    >
    > int main() {
    > MainDisplay("Hi");
    > }
    >
    > //end
    >
    > I want to see something like:
    >
    > main() wrote Hi
    >
    > I've tried many things. One involved using C macros. For example:
    >
    > #include <iostream>
    > using namespace std;
    >
    > #define DisplayMsg(msg) MainDisplay(__FUNCTION__, msg);


    At this point in time __FUNCTION__ is not part of C++ language, IIRC.

    >
    > void MainDisplay(char* Function, char* msg) {


    Again, this should be

    void MainDisplay(char const* Function, char const* msg) {

    > cout << Function << " wrote " << msg << endl;
    > }
    >
    > int main() {
    > DisplayMsg("Hi");
    > }
    >
    > //end
    >
    > This has the following output:
    >
    > main wrote Hi.
    >
    > This seems to work fine. But SOMETIMES, I get a segmentation fault
    > when I try to do any io streaming (not limited to cout). I have no
    > idea why, but people have told me it's because it garbles up some
    > areas of memory that its not supposed to.


    Nothing in the code you posted indicates that it would do that.

    > So, not being able to
    > isolate the cause of the problem, I deemed it unsafe and unpredictable
    > and decided to abandon this method.


    Completely unfounded. If you were unable to locate the cause it
    doesn't mean the whole method is flawed. If I pour water into my
    car's tank, and then it won't start, why should I blame the car?

    >
    > Next I tried this:
    >
    > #include <iostream>
    > using namespace std;
    >
    > inline void MainDisplay(char* msg) {
    > cout << __FUNCTION__ << " wrote " << msg << endl;


    AFAIK, __FUNCTION__ always gives the name of the current function,
    so here you would always get 'MainDisplay'.

    > }
    >
    > int main() {
    > MainDisplay("Hi");
    > }
    >
    > //end
    >
    > I came to this method after hours of searching for a solution. It
    > said that the code for an inline function gets replaced wherever the
    > call is made. This would imply that the call MainDisplay("Hi") would
    > be replaced entirely by
    > cout << __FUNCTION__ << " wrote " << msg << endl;


    The call is replaced with the body at the _compilation_ time, but
    the __FUNCTION__ macro is replaced with the function name at the
    _preprocessing_ time. So, even before the call is replaced with
    the statement above, the statement is changed (by the preprocessor).

    > but to my dismay the output was:
    >
    > MainDisplay wrote Hi


    Sure.

    The inlined function must NOT have a different effect than the non-
    inlined one.

    >
    > Is there a solution to this without using C macros? Is there even a
    > stable solution at all? Why don't inline functions work like i'm
    > expecting them to?


    There is no solution in C++, period. You should think of supplying
    the function name yourself, without macros:

    int main() {
    MainDisplay("main", "Hello");

    Victor
    Victor Bazarov, Jul 23, 2004
    #2
    1. Advertising

  3. Prashant

    Ali Cehreli Guest

    On Fri, 23 Jul 2004 14:59:25 -0700, Victor Bazarov wrote:

    > Prashant wrote:
    >> Okay this is my problem. I have a function, MainDisplay(char*) that
    >> takes in an argument of a string to display.

    >
    > Shouldn't it be MainDisplay(char const *)?
    >
    >
    > > Simple. I want this
    >> function to also be able to display the function that called it. This
    >> is where i'm lost. So for example:

    [...]
    > There is no solution in C++, period. You should think of supplying the
    > function name yourself, without macros:
    >
    > int main() {
    > MainDisplay("main", "Hello");


    Prashant can simplify at least that part with a macro:

    #include <iostream>

    void MainDisplay(char const * function, char const * msg)
    {
    std::cout << function << " wrote " << msg << '\n';
    }

    #define MAIN_DISPLAY(x) MainDisplay(__FUNCTION__, (x))

    void foo()
    {
    MAIN_DISPLAY("Yellow");
    }

    int main()
    {
    MAIN_DISPLAY("Hello");
    foo();
    }

    Ali
    Ali Cehreli, Jul 24, 2004
    #3
  4. "Ali Cehreli" <> wrote...
    > On Fri, 23 Jul 2004 14:59:25 -0700, Victor Bazarov wrote:
    >
    > > Prashant wrote:
    > >> Okay this is my problem. I have a function, MainDisplay(char*) that
    > >> takes in an argument of a string to display.

    > >
    > > Shouldn't it be MainDisplay(char const *)?
    > >
    > >
    > > > Simple. I want this
    > >> function to also be able to display the function that called it. This
    > >> is where i'm lost. So for example:

    > [...]
    > > There is no solution in C++, period. You should think of supplying the
    > > function name yourself, without macros:
    > >
    > > int main() {
    > > MainDisplay("main", "Hello");

    >
    > Prashant can simplify at least that part with a macro:
    >
    > #include <iostream>
    >
    > void MainDisplay(char const * function, char const * msg)
    > {
    > std::cout << function << " wrote " << msg << '\n';
    > }
    >
    > #define MAIN_DISPLAY(x) MainDisplay(__FUNCTION__, (x))
    >


    Sigh... If you read the thread before replying, you'd see that __FUNCTION__
    does not exist in C++. And, AFAICT, it is not going to exist.

    The only commonly accepted equivalent is the use of __FILE__ and __LINE__.
    The OP should look into that. But if he doesn't, there is nothing we can
    do about it.

    V
    Victor Bazarov, Jul 24, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    2
    Views:
    2,790
    ding feng
    Jun 25, 2003
  2. Replies:
    5
    Views:
    578
  3. Andreas Lagemann
    Replies:
    8
    Views:
    464
    Mike Wahler
    Jan 10, 2005
  4. tiwy
    Replies:
    0
    Views:
    429
  5. hzgt9b

    Get name of calling function...

    hzgt9b, Mar 5, 2008, in forum: Javascript
    Replies:
    3
    Views:
    158
    hzgt9b
    Mar 11, 2008
Loading...

Share This Page