How to get name of calling function

P

Prashant

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
 
V

Victor Bazarov

Prashant said:
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
 
A

Ali Cehreli

Prashant said:
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
 
V

Victor Bazarov

Ali Cehreli said:
Prashant said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top