Reputation: 13709
The accepted answer to the question C++ concatenating __FILE__ and __LINE__ macros? work for FILE and LINE, but I would like to add the func macro too.
#define S1(x) #x
#define S2(x) S1(x)
#define S3(x) x
#define LOCATION __FILE__ " : " S3(__func__) " : " S2(__LINE__)
this gives error
log.cpp|15 col 15 error| note: in definition of macro ‘S3’
Also tried,
#define LOCATION __FILE__ " : " __func__ " : " S2(__LINE__)
gives error
log.cpp|30 col 9 error| note: in expansion of macro ‘LOCATION’
I understand that LINE was a integer hence needed #x, but func is a char array. How do I get this working? Any help?
Upvotes: 0
Views: 2404
Reputation: 9
__func__ is a variable (not a macro unlike __FILE__ or __LINE__)
This related question Treating __func__ as a string literal instead of a predefined identifier has good explanation.
Upvotes: 0
Reputation: 13709
As cpplearner mentioned correctly __func__
is not a macro, it's a special function-local predefined variable. I achieved desired result by
#define S1(x) #x
#define S2(x) S1(x)
#define LOCATION string(__func__) + " : " S2(__LINE__) + " : "
and I use it by sending the string to a wrapper log function
void log(string s) {
cout << s <<endl;
}
void abc(){
log(LOCATION + string("some message "));
}
Outputs:
abc : 23 : some message
Upvotes: 2