Retrieving the name of a function inside the function.

Hello. I'm looking for a method where I can retrieve the name of a function within the function itself. I've found there C++ methods such as func that are described to perform what I'm after, but I don't find those to be available on the Arduino.

My use case is to leverage a common logging structure between multiple functions such as:

time: functionName: messageBody

eg,

void function1()
{
// do something

char* messagePayload = "blah.one.blah";

// log

Serial.print(millis());Serial.print(" : ");Serial.print(functionName);
Serial.print(": ");Serial.println(messagePayload);
}

void function2()
{
// do something2

char* messagePayload = "blah.two.blah";

// log

Serial.print(millis());Serial.print(" : ");Serial.print(functionName);
Serial.print(": ");Serial.println(messagePayload);
}

Is there a method to achieve this on the arduino?

These predefined macros are more a preprocessor thing and I don’t think the preprocessor keeps track of what function each line is in. You could use FILE and LINE to determine where in your code the log message came from.

This seems to work

void setup()
{
  Serial.begin(115200);
  Serial.println(__func__);
  foo();
  bar();
}

void loop()
{
}

void foo()
{
  Serial.println(__func__);
}

void bar()
{
  Serial.println(__func__);
}

But if you are going to type

  Serial.println(__func__);

you might just as well type

  Serial.println("foo");

UKHeliBob:
you might just as well type

  Serial.println("foo");

...but that would be confusing when bar() runs

for reference
https://gcc.gnu.org/onlinedocs/gcc/Function-Names.html

AWOL:
...but that would be confusing when bar() runs

I don't know whether you are being sarcastic or not (I hope so), but obviously when you log from bar() you use

Serial.println("bar");

The OPs original code fragments printed the function name from within the function doing the logging, but in any case the logging would be better done using a function to which parameters such as the function name were passed. He did, after all say that he wanted to

leverage a common logging structure between multiple functions

UKHeliBob:
I don't know whether you are being sarcastic or not (I hope so), but obviously when you log from bar() you use

Serial.println("bar");

The OPs original code fragments printed the function name from within the function doing the logging, but in any case the logging would be better done using a function to which parameters such as the function name were passed. He did, after all say that he wanted to

But if you use the predefined func, and you decide to change the name of the function, you don't have to change anything else.
If you use the predefined func in a macro, you can use the macro anywhere.

AWOL:
But if you use the predefined func, and you decide to change the name of the function, you don't have to change anything else.
If you use the predefined func in a macro, you can use the macro anywhere.

All true, of course, but the OP presented the printing of the function name as a problem when it is nothing of the sort. In fact he provided the solution in his code snippet which, as a matter of interest, used duplicate logging code rather than calling a function.

UKHeliBob:
This seems to work

void foo()

{
 Serial.println(func);
}

void bar()
{
 Serial.println(func);
}

Yes, this does work. I wont bother trying to determine which fat-fingering was causing error prior to my post. Regardless, Thanks.

AWOL:
But if you use the predefined func, and you decide to change the name of the function, you don't have to change anything else.
If you use the predefined func in a macro, you can use the macro anywhere.

That is entirely the purpose behind my initial request.

UKHeliBob:
All true, of course, but the OP presented the printing of the function name as a problem when it is nothing of the sort. In fact he provided the solution in his code snippet which, as a matter of interest, used duplicate logging code rather than calling a function.

I agree, one method would be to call a logging function, but currently I'm looking at just copy/paste where needed - which is why I said "common logging structure". However, if I used a specific function for logging, I would need to derive the parent calling function, not the name of the current function, no?

However, if I used a specific function for logging, I would need to derive the parent calling function, not the name of the current function, no?

A macro would be more appropriate, it seems to me