Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro to Get function name and parameter values from within function

Tags:

c++

c

macros

I'm working on a debugging/logging program and was wondering if it would be possible to create a macro that I can paste inside each function and it will print the function name and parameter values each time the function's called. Function name can be resovled at compile time, the problem is figuring out how to print parameter values?

UPDATE: I remember reading an article to get parameters but that invovled assembly code and working manipulating stack pointers, which is not cross-platform compatible - something that I need.

like image 926
tunafish24 Avatar asked Oct 23 '22 07:10

tunafish24


1 Answers

You can use the __FUNCTION__ or __func__ macro for the function name. For the parameters, I don't think there's a built-in macro to achieve this.

Other helpful macros are __LINE__ and __FILE__.

EDIT:

__FUNCTION__ and __func__ are not part of the standard, but they are widely supported.

16.8 deals with predefined macros:

__cplusplus
__DATE__
__FILE__
__LINE__
__STDC_HOSTED__
__TIME__

and macros defined by the implementation:

__STDC__
__STDC_VERSION__
__STDC_ISO_10646__
like image 189
Luchian Grigore Avatar answered Nov 02 '22 12:11

Luchian Grigore