I am trying to write a thread safe logger class so that i can do the exact same as with cout but with thread safety.
here is the logger class (still working on the type of lock required)
class logger { public: logger(LOGGER::output_type type); logger(const logger& orig); virtual ~logger(); template <typename T> logger & operator << (const T & data){ boost::mutex::scoped_lock io_mutex_lock(io_mutex); (*out)<<data; return *this; } private: static boost::mutex io_mutex; std::ostream * out; }; The poblem is I cannot do the following
log<<"asdfg";
I have to instead do
log<<string("asdfg");int i = 10;
log<<string ("i = ") << i << endl;
following is the compilation error.
gcc.compile.c++ src/simpleThread/bin/gcc-4.4.5/debug/simpleThread.o src/simpleThread/simpleThread.cc: In function ‘int main()’: src/simpleThread/simpleThread.cc:28: error: no match for ‘operator<<’ in ‘((logger*)logOut.logger::operator<< [with T = char [18]](((const char (&)[18])"fibonacci thread ")))->logger::operator<< [with T = int](((const int&)((const int*)(& i)))) << std::endl’ So I guess i am missing some important concept of C++. Please let me know what it is? Is my requirement even achievable
thanks, Kiran