Question

Is there an easy way to build a string which includes the _FILE_ and _LINE_ values?

I could do something like:

std::stringstream ss;
ss << "Error in "<<_FILE_<<":"<<_LINE_<<" - too many bees!";
log(ss.str());

But that is a PITA, compared to the ideal which might be:

log("Error in "+_FILE_+":"+_LINE_+" - too many bees!");

Is there a neat way to do this in C++/STL/boost? Note I am limited to older compiler, no C++11!

Was it helpful?

Solution

As Alex commented: double macro expansion to make __LINE__ into a string and let the compiler concatenate the strings for you:

#define S(x) #x
#define S_(x) S(x)
#define S__LINE__ S_(__LINE__)

log("Error in "__FILE__":"S__LINE__" - too many bees!");

to reduce typing, as greatwolf suggested:

#define logfl(s) log("Error in "__FILE__":"S__LINE__" - "s)

logfl("too many bees!");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top