Can I get a pointer to the current function?

Is it possible to get a pointer to the current function? If so, how can I do this?

Motivation: I have a function that performs some logging, and I would like to call

log(currentfunc, "blabla") 

Which makes some conclusion, for example.

+4
source share
3 answers

You can get the name of the current function (but not a pointer to it) through the predefined identifier __func__ , which is part of C99.

 log(__func__, "blabla"); 

Here is the link

+5
source

I'm not sure about the function pointer, but the predefined identifier __func__ returns the name of the function. Maybe this can help ...

In fact, I would replace your log function with a macro, so you do not need to insert a name every time as such:

 #define log(x) log(__func__,x) 
+2
source

You can use a combination of __ FILE __ and __ LINE __ . This will work with Microsoft and GCC compilers.

0
source

Source: https://habr.com/ru/post/1436373/


All Articles