using embedded stringstream on a single line.

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 << ") " ); Best regards, Ta, Avi

On Wed, Aug 26, 2009 at 4:13 PM, Avi Bahra <avibahra@googlemail.com> 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 << ") " );
Well, you could have a look at Boost.Format. It gets you part way there, but you'd still have a std::string rather than std::stringstream. Maybe your log function has a std::string overload? - Rob.

On Wed, Aug 26, 2009 at 4:13 PM, Avi Bahra<avibahra@googlemail.com> 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 << ") " );
Best regards, Ta, Avi
Slightly different, but how about Boost.Format? log( Log::DBG, str(boost::format("SUBMIT: Task( %s) ")%absPath) ); Internally, it uses streams, so your ostream inserters for custom types will get used, I believe. Stuart Dootson

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 << ") " );
Something simple would be to use lexical_cast (assuming your log() function has an overload accepting strings): log(Log::DBG, "Submit: Task(" + boost::lexical_cast<std::string>(absPath) + ")"); Alternatively, to get a nicer interface for the caller, the log() function could be templatized and take care of the streaming. Something like: template<typename Arg1T, typename Arg2T, typename Arg3T> void log(Log::Level l, const Arg1T& arg1 = None(), const Arg2T& arg2 = None(), const Arg3T& arg3 = None()) { std::stringstream ss; ss << arg1 << arg2 << arg3; log(l, ss); } (Assuming a do-nothing operator<<(ostream&, const None&)) used as: log(Log::DBG, "Submit: Task(", absPath, ")");

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().
participants (5)
-
Avi Bahra
-
Eric MALENFANT
-
Nat Goodspeed
-
Robert Jones
-
Stuart Dootson