Avi Bahra wrote:
Is there anything in boost/design pattern that can help with following:
std::stringstream ss;
ss << "SUBMIT: Task(" << absPath << ") ";
log( Log::DBG, ss );
The code I am working on does this all over the place, what I would like
is :
log( Log::DBG, "SUBMIT: Task(" << absPath << ") " );
My colleague came up with a really cute mechanism:
#include <sstream>
#include
// helper, see STRINGIZE() macro
template <typename Functor>
std::string stringize_f(Functor const & f)
{
std::ostringstream out;
f(out);
return out.str();
}
#define STRINGIZE(EXPRESSION) \
(stringize_f(boost::lambda::_1 << EXPRESSION))
You'd use it something like:
log(Log::DBG, STRINGIZE("SUBMIT: Task(" << absPath << ") "));
Or you could wrap your log() function in a LOG() macro:
#define LOG(level, EXPRESSION) \
log(level, STRINGIZE(EXPRESSION))
which would permit your original syntax, using LOG() instead of log().