-1

I have a simple state machine, there is a class called IState with one function :

IState doSomething(int i);

I have many inheritors, Astate, BState ...

i want to log in which state i'm currently at, i thought maybe i could write in all my implementation of doSomething the following line :

writeToLog("current function is" << __function__);

but unfortunately it doesn't help , because all the functions named identically, is there a way to write the full function name with it's class name?

Thanks

OopsUser
  • 4,642
  • 7
  • 46
  • 71

3 Answers3

1

If your writeToLog function just expects a C string then you can do this:

writeToLog("current function is" __FUNCTION__);

Note that there is no comma (or any other operator) between "current function is" and __FUNCTION__ - C and related languages automatically concatenate string literals.

Note also the use of __FUNCTION__ rather than __function__.

Paul R
  • 208,748
  • 37
  • 389
  • 560
1

For g++ to get the class name you can use: Is there a __CLASS__ macro in C++?

For function names you can always use the __FUNCTION__ macro.

And if I remember correctly for MSVC the __FUNCTION__ macro expands to contain the class name too.

And the mandatory links:

https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html

http://msdn.microsoft.com/en-us/library/b0084kay.aspx

Community
  • 1
  • 1
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
0

The Standard's requirements of __func__ are:

The function-local predefined variable func is defined as if a definition of the form

static const char __func__[] = "function-name ";

had been provided, where function-name is an implementation-defined string. It is unspecified whether such a variable has an address distinct from that of any other object in the program.

Any other macro you may be using is non-Standard - you'll have to look to your compiler documentation or share which one you're using.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252