Boost logo

Boost Users :

Subject: Re: [Boost-users] using embedded stringstream on a single line.
From: Eric MALENFANT (Eric.Malenfant_at_[hidden])
Date: 2009-08-26 13:13:51


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, ")");


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