#include <iostream>
#include <string>
#include <type_traits>
// macro for logging in a boost::log style
#define LOGABLE_TYPE typename std::remove_reference<decltype(*this)>::type
#define LOG_DEBUG this->Logable<LOGABLE_TYPE>::_loggerObj.logStream("debug")
#define LOG_INFO this->Logable<LOGABLE_TYPE>::_loggerObj.logStream("info")
#define LOG_WARN this->Logable<LOGABLE_TYPE>::_loggerObj.logStream("warning")
#define LOG_ERROR this->Logable<LOGABLE_TYPE>::_loggerObj.logStream("error")
class LogableImpl
{
private:
std::string _channelName;
public:
LogableImpl(const std::string & channelName): _channelName(channelName) {}
std::ostream & logStream(const std::string & severetyLevel)
{
std::cout << _channelName << " " << severetyLevel;
return std::cout;
}
};
template <class Descendant>
class Logable
{
protected:
Logable(const std::string & channelName): _loggerObj(channelName) {}
LogableImpl _loggerObj;
};
class Base: private Logable<Base>
{
public:
Base()
: Logable<Base>("Base")
{}
void someMethod()
{
LOG_INFO << "some method is called" << std::endl;
LOG_ERROR << "an error happened" << std::endl;
}
};
class Derived: public Base, private Logable<Derived>
{
public:
Derived()
: Logable<Derived>("Derived")
{}
void someAnotherMethod()
{
LOG_INFO << "another method is called" << std::endl;
LOG_ERROR << "another error is happened" << std::endl;
}
};
int main()
{
Base b;
Derived d;
b.someMethod();
d.someMethod();
d.someAnotherMethod();
return 0;
}