Richard Howells wrote:
Well it might be crude but sometimes the pre-processor is your friend...
template<class T> MyLogger & operator<<(MyLogger & log, T o) { #if defined(_DEBUG) // code goes here #else // leave empty #endif }
...but your problem is still there because you don’t know what to put in 'code goes here'. I think you still need a family of strongly typed MyLogger::print overloads to call there.
That would be the easy part. It becomes trickier, when you want to specify a logging severity level, such as in: the_logger.printf< Log::DBG >( "some message %d.", 42 ); you may want to use manipulators, as in: the_logger << Log::dbg << "some message" << 42; but then again, when using operator<<, you have no guarantees of what happens if several threads are writing to the same logger. Then you may get mixed/mangled messages. To fix this you may want to use a "flush" manipulator, but (to my taste) at that level the complexity of the operator approach defeats its purpose, i.e. simplicity and elegance. Dan