C++ Logger Design

Oct 04, 2017 03:28

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

Leave a comment

Comments 2

nyankoframe October 8 2017, 08:24:50 UTC
Maybe a logger that takes in a std::ostringstream which can contain the desired message? Or for an interface usable from C code, using varargs.

Reply

nuah October 8 2017, 12:00:32 UTC
Accepting a std::ostringstream means that the user needs to write code like:

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

Up