I needed a C++ logger, but I was not sure which of the many logging implementations I should use. So I decided to pick one, and implement a wrapper over that logger. In case I needed to change the implementation, I only need to change the wrapper. For my wrapper class, I shall support five standard log levels:
enum LogLevel { TRACE, DEBUG, INFO,
(
Read more... )
Comments 2
Reply
std::ostringstream stream;
stream << foo << bar << baz;
logger.log(level, stream);
This is about as much typing as simply accepting a std::string:
std::ostringstream stream;
stream << foo << bar << baz;
logger.log(level, stream.str());
Being an extensive user of the logger class myself, I wanted to type much less, like:
logger.log(level) << foo << bar << baz;
I did consider using varargs, but I don't think you can provide a C++ object as a vararg parameter. On the other hand, many authors of C++ classes provide the following override:
std::ostream& operator<<(std::ostream& stream, const MyClass& object);
Which is the C++ counterpart of the Object.toString() method in Java. So I wanted to leverage on the overridden << operator as well.
Reply
Leave a comment