Blackwood Blackwood - 6 days ago 4
C++ Question

Is it possible to create a C++ wrapper for easylogging++ that mimics the easylogging++ syntax?

The easylogging++ code defines a macro that makes it very simple to use:

LOG(logLevel) << "This mimics std::cout syntax. " << 1 << " + " << 1 << " = " << 2;


I want to make a wrapper class for easylogging++. I can easily create a function with two parameters to wrap the above line. However, is it possible to mimic this syntax in a wrapper class? For example:

Logger logger;
logger(logLevel) << "Line " << 1 << " of log text.";


I know I can easily overload the insertion operator, but that still leaves me with having to write another function to set the log level each time.

UPDATE:

Thanks to Starl1ght's answer I was able to get this working. I figured I would share in case anyone else has a similar need.

I created two overloads. One was for () and the other for <<.

Logger &operator()(logLevelT logLevel) {
mLogLevel = logLevel;
return *this;
}


template <typename T>
Logger &operator<<(T const &value) {
LOG(mLogLevel) << value;
return *this;
}

Answer

You must overload operator() so, it will set internal log level and return *this as type Logger&, so, overloaded operator<< will work on returned reference with necessary log-level set.

Something like this:

Logger& Logger::operator()(LogLevel level) {
    // set internal log level
    return *this;
}