How to find the __FUNCTION__, __LINE__ and __FILE__ functions of the parent function?

I just fooled myself: I wanted to track the process, and for this I wrote a function trace()that contains the following line of code:

printf("%s[%s:%d], %s\n", __FUNCTION__, __FILE__, __LINE__, s_message);

I was hoping to see which function I am in, in which file and which line in this file, but I just saw information about the file in which I programmed this function trace().

Is it possible, #defineanyway, to tell the C compiler to take the mentioned macros from the parent of the calling function?

+5
source share
2 answers

You need to wrap it in a macro, for example:

void _trace(char const *function, char const *file, long line, char const *message) {
  printf("%s[%s:%ld], %s\n", function, file, line, message);
}

#define trace(message) _trace(__FUNCTION__, __FILE__, __LINE__, (message))
+8
source

, . , 3 , , . , , trace().

trace() const char * , . , mTrace, , __FUNCTION__,__FILE__, __LINE__. , mTrace , , , trace().

Hth

+1

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


All Articles