Adding Prolog to Epilog

N

Neo

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?
 
K

Kai-Uwe Bux

Neo said:
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?

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
 
N

Nate Barney

Kai-Uwe Bux said:
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:

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

Nate
 
K

Kai-Uwe Bux

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

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
 
N

Nate Barney

Kai-Uwe Bux said:
The reason is: there is no __FUNCTION__ macro in C++. If your compiler
supports it then it does so as an extension.

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

Nate
 
J

Jack Klein

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

Nate

Yes, there is. It's not a part of the C++ language, and it only
exists as a non-standard extension on some implementations.
 
B

BobR

Jack Klein wrote in message said:
Yes, there is. It's not a part of the C++ language, and it only
exists as a non-standard extension on some implementations.

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++. :-}
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top