0

I wanted to protect the access to a log file that I use for multithreaded logging with boostlog library.

I tried this stream class

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

using it this way (text_sink is a boostlog object):

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

and I get this mysterious error: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'

the whole compile error:

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

I suspect that I am not well defining the operator<<()... but I don't find what is wrong.

this is the prototype of addStream: void add_stream(shared_ptr< stream_type > const& strm); with typedef std::basic_ostream< target_char_type > stream_type;

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • Which line is actually giving the error? Is it the call to `add_stream`? If so, can you provide the method signature to that? – Dave S Nov 29 '12 at 16:59
  • `pBackend->add_stream(spFileStream);` causes the trouble. I'm searching the prototype and gonna put it in the question. – Stephane Rolland Nov 29 '12 at 17:01

1 Answers1

2

From the error message presuably pBackend->add_stream is expected a shared_ptr (!) to an ostream while you're giving it a shared_ptr to a ThreadSafeStream which is a totally unrelated type. You'll need to create an overloaded add_stream method that works with ThreadSafeStreams most likely.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • 1
    I cannot modify the boostlog code to fit my need... However I obviously need to inherit `std::basic_ostream` in my class `ThreadSafeStream` – Stephane Rolland Nov 29 '12 at 17:10