Determine value of __PRETTY_FUNCTION__ prior to runtime execution
I'm currently using __PRETTY_FUNCTION__
when reporting messages (errors, warnings, etc) to a logfile. This ensures there is no confusion regarding which function created the error message.
At the moment, I would like to automatically take the output from the logfile and link it back up with the code which initially created it. The problem is, I cannot determine an efficient method for matching the __PRETTY_FUNCTION__
output in the logfile to the name of the function within the code.
Initially, I was hoping I could just use gcc -E
to expand all of the macro constants / preprocessor components and directly compare the output of the logfile to the original code. However, __PRETTY_FUNCTION__
is not a macro constant -开发者_开发知识库- it is apparently a variable which is determined at runtime. Therefore, gcc -E
does not work.
From http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html:
These identifiers are not preprocessor macros. In GCC 3.3 and earlier, in C only,
__FUNCTION__
and__PRETTY_FUNCTION__
were treated as string literals; they could be used to initialize char arrays, and they could be concatenated with other string literals. GCC 3.4 and later treat them as variables, like__func__
. In C++,__FUNCTION__
and__PRETTY_FUNCTION__
have always been variables.
Any ideas regarding how I can efficiently match up the output from __PRETTY_FUNCTION__
with the functions in the original code?
Using __LINE__
and __FILE__
is a simple and relatively easy solution. The changes to your log functions should be relatively minor in scope as well. If you want code coverage information you're probably better off using GCOV.
http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/index.html#toc_Gcov
http://bobah.net/d4d/tools/code-coverage-with-gcov
__PRETTY_FUNCTION__
is usually used with __FILE__
and __LINE__
to identify the place in source code.
See Standard Predefined Macros for more details.
精彩评论