Boost logo

Boost Users :

Subject: Re: [Boost-users] using embedded stringstream on a single line.
From: Nat Goodspeed (nat_at_[hidden])
Date: 2009-08-27 19:31:23


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 <boost/lambda/lambda.hpp>

// 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().


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net