libmoost
/home/mhx/git/github/libmoost/include/moost/logging/pseudo_ostream.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00035 #ifndef MOOST_LOGGING_PSEUDO_OSTREAM_HPP__
00036 #define MOOST_LOGGING_PSEUDO_OSTREAM_HPP__
00037 
00038 #include <boost/thread.hpp>
00039 
00040 namespace moost { namespace logging {
00041 
00052 class pseudo_ostream
00053 {
00054    public:
00055       pseudo_ostream() : pout_(0) {}
00056 
00062       void attach(std::ostream & out)
00063       {
00064          boost::lock_guard<boost::mutex> lock(mtx_);
00065          pout_ = &out;
00066       }
00067 
00071       void detach()
00072       {
00073          pout_ = 0;
00074       }
00075 
00084       template <typename T>
00085       pseudo_ostream & operator << (T const & t)
00086       {
00087          boost::lock_guard<boost::mutex> lock(mtx_);
00088          if(pout_)
00089          {
00090             *pout_ << t;
00091          }
00092 
00093          return *this;
00094       }
00095 
00103       typedef std::ostream & (*iomanip_t)(std::ostream &);
00104       pseudo_ostream & operator << (iomanip_t const & iomanip)
00105       {
00106          boost::lock_guard<boost::mutex> lock(mtx_);
00107          if(pout_)
00108          {
00109             iomanip(*pout_);
00110          }
00111 
00112          return *this;
00113       }
00114 
00120       operator void *() const
00121       {
00122          boost::lock_guard<boost::mutex> lock(mtx_);
00123          return pout_ ? *pout_ : static_cast<void *>(0);
00124       }
00125 
00126    private:
00127       std::ostream * pout_;
00128       mutable boost::mutex mtx_;
00129 
00130 };
00131 
00132 }}
00133 
00134 #endif