473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jul 22 '05 #1
3 12444
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
Jul 22 '05 #2
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
Jul 22 '05 #3
"Ali Cehreli" <ac******@yahoo.com> 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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Woof-Woof | last post by:
Why doesn't my mouse wheel work in the code windows in VB5 under XP? I can scroll the properties and project windows using the wheel, but not the actual code windows (XP). Everything works fine...
21
by: TheKeith | last post by:
I heard that the name attribute is deprecated in html 4.01 strict. Is it recommended that you use the ID attribute for images along with the getElementById method instead of the old way? Thanks.
6
by: yyy | last post by:
my question is rather theoretical than practical in the pure programming aspect... is "name mangling" the same as "name decorating" ? browsing the web, you might find multiple people saying...
2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
4
by: IMS.Rushikesh | last post by:
Hi All, I am trying to execute below code but it gives me an COMException ///// Code Start //// public string GetName(Excel.Range range) { try { if (range.Name != null)
2
by: Don | last post by:
This may seem like an odd question, but is it possible to get the name of the class & function that is calling a function of another class? e.g. Public Class CallerName Public Shared Function...
8
by: Jeffrey Barish | last post by:
I believe that the answer to my question is no, but I want to be sure that I understand this issue correctly: Suppose that there are two classes defined as follows: class A(object): def...
3
by: Rico | last post by:
Hello, I have a generic process that logs errors from different sources. When I call this code, I'd like to also submit the name of the function or sub that is raising the error without having...
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
12
by: mcarthybn | last post by:
hi , I am working on huge C programming project.I am doin debugging only by printf measn i dont have any dubugger .So it is very difficult know who is actually calling a function and i am stuck...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.