0

我想通过 boostlog 库保护对用于多线程日志记录的日志文件的访问。

我试过这个流类

class ThreadSafeStream
{
public:
    template <typename TInput>
    const ThreadSafeStream& operator<< (const TInput &tInput) const
    {
         // some thread safe file access    
         return *this;
    }
};

以这种方式使用它(text_sink 是一个 boostlog 对象):

    //...
m_spSink.reset(new text_sink); 
text_sink::locked_backend_ptr pBackend = m_spSink->locked_backend();

const boost::shared_ptr< ThreadSafeStream >& spFileStream = boost::make_shared<ThreadSafeStream>();

pBackend->add_stream(spFileStream); // this causes the compilation error

我得到了这个神秘的错误:cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'

整个编译错误:

Log.cpp(79): error C2664: 'boost::log2_mt_nt5::sinks::basic_text_ostream_backend<CharT>::add_stream' : cannot convert parameter 1 from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T> &'
1>          with
1>          [
1>              CharT=char
1>          ]
1>          and
1>          [
1>              T=ThreadSafeStream
1>          ]
1>          and
1>          [
1>              T=std::basic_ostream<char,std::char_traits<char>>
1>          ]
1>          Reason: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'
1>          with
1>          [
1>              T=ThreadSafeStream
1>          ]
1>          and
1>          [
1>              T=std::basic_ostream<char,std::char_traits<char>>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

我怀疑我没有很好地定义 operator<<()... 但我没有找到问题所在。

这是 addStream 的原型void add_stream(shared_ptr< stream_type > const& strm);typedef std::basic_ostream< target_char_type > stream_type;

4

1 回答 1

2

从错误消息中可能pBackend->add_stream需要一个shared_ptr(!) 到一段ostream时间,您将它提供shared_ptrThreadSafeStream一个完全不相关的类型。您需要创建一个最有可能add_streamThreadSafeStreams 一起使用的重载方法。

于 2012-11-29T17:01:34.563 回答