Based on my logger
class LogBuilder{public: ~LogBuilder() { // Write the message // for example, std::cout << log_message.c_str() << std::endl; } template <class T> LogBuilder & operator<<( const T &t ) { log_message << t; return *this; }private: std::ostrstream log_message;
class Logger{public: LogBuilder warning( void ) const { return LogBuilder( Level.WARNING); }
Usage:
Logger log; log.warning() << "Hello" << "World" << 1983 << 2.5f << some_variable;
This relies on return value optimization (although that isn't required) and stack object life cycle (should be a non-issue, at least I'm not aware of any, excluding possibly VC6. Boost uses same approach in some classes, so it wouldn't appear to be an issue.
The only thing to look out for is that destructor of LogBuilder cannot throw an exception.