I am using the macros __FILE__ and __func__ for retrieving the file and function location.
My code is structured like this… (note i am using a game engine )
Test.h
class TestClass
{
...
void Log(int status);
...
};
Test.cpp
...
void TestClass::Log(int status)
{
printf("%s %s %i",__FILE__,__func__, status);
}
...
.
and i create a class object and use it to log whatever i want to.
example.cpp
#include "Test.h"
...
TestClass tt;
...
tt.Log((int)1);
...
the problem is that the output pastes the file and function name of the Test.cpp, and note the filename and function of example.cpp.
Is there a way around this or do i have to directly use the printf statement again and again?
Thanks
You don’t need to use
printfwherever in the code, but you will need to callTestClass::Logwith extra parameters__FILE__and__func__.Don’t threat however, you can create a macro for that so you don’t have to write down the extra parameters on each call:
and instead of calling
you would call
You’d need to change the
Logsignature to account for the extra parameters of course:Also, I see
Logisn’t accessing any class members, so you can make it even easier and make itstatic:So, if you do it like this, you’d only need to call:
Unfortunately there’s no way to do this automatically, without modifying your code, as macros expand at runtime. What your compiler sees now is:
that is why you need the file name and function name passed as parameters.