Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - How to get name of calling function

 
Thread Tools Search this Thread
Old 07-23-2004, 10:50 PM   #1
Default How to get name of calling function


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
  Reply With Quote
Old 07-23-2004, 10:59 PM   #2
Victor Bazarov
 
Posts: n/a
Default Re: How to get name of calling function
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
  Reply With Quote
Old 07-24-2004, 01:05 AM   #3
Ali Cehreli
 
Posts: n/a
Default Re: How to get name of calling function
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
  Reply With Quote
Old 07-24-2004, 01:56 AM   #4
Victor Bazarov
 
Posts: n/a
Default Re: How to get name of calling function
"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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
equivalent function for itoa in Linux gcc compiler suse Software 0 03-06-2009 05:30 AM
How to assign a returns value of a javascript function to a hiddenfield in a webpart Chander Software 0 12-20-2007 09:14 AM
How to call C# function in javascript visj4u Software 2 04-23-2007 03:24 PM
I lost the "Help and Support" function from my start menu Keith A+ Certification 1 03-14-2005 03:05 PM
Please STOP calling them DVDs amelrob@aol.com DVD Video 183 12-15-2003 03:45 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46