2016-08-12 14:51:14 +02:00
|
|
|
/*
|
|
|
|
* CLoggerBase.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace ELogLevel
|
|
|
|
{
|
|
|
|
enum ELogLevel
|
|
|
|
{
|
|
|
|
NOT_SET = 0,
|
|
|
|
TRACE,
|
|
|
|
DEBUG,
|
|
|
|
INFO,
|
|
|
|
WARN,
|
|
|
|
ERROR
|
|
|
|
};
|
2016-10-02 20:21:20 +02:00
|
|
|
|
|
|
|
inline std::string to_string(ELogLevel level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case NOT_SET:
|
|
|
|
return "not set";
|
|
|
|
case TRACE:
|
|
|
|
return "trace";
|
|
|
|
case DEBUG:
|
|
|
|
return "debug";
|
|
|
|
case INFO:
|
|
|
|
return "info";
|
|
|
|
case WARN:
|
|
|
|
return "warn";
|
|
|
|
case ERROR:
|
|
|
|
return "error";
|
|
|
|
default:
|
2017-05-25 19:57:20 +02:00
|
|
|
#ifdef NO_STD_TOSTRING
|
|
|
|
return "invalid";
|
|
|
|
#else
|
2016-10-02 20:21:20 +02:00
|
|
|
return std::string("invalid (") + std::to_string(level) + ")";
|
2017-05-25 19:57:20 +02:00
|
|
|
#endif
|
2016-10-02 20:21:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-12 14:51:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace vstd
|
|
|
|
{
|
|
|
|
class DLL_LINKAGE CLoggerBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~CLoggerBase(){};
|
|
|
|
|
|
|
|
virtual void log(ELogLevel::ELogLevel level, const std::string & message) const = 0;
|
2016-11-28 18:34:20 +02:00
|
|
|
virtual void log(ELogLevel::ELogLevel level, const boost::format & fmt) const = 0;
|
2016-08-12 14:51:14 +02:00
|
|
|
|
2016-08-12 18:20:57 +02:00
|
|
|
template<typename T, typename ... Args>
|
2016-08-20 12:51:20 +02:00
|
|
|
void log(ELogLevel::ELogLevel level, const std::string & format, T t, Args ... args) const
|
2016-08-12 18:20:57 +02:00
|
|
|
{
|
|
|
|
boost::format fmt(format);
|
|
|
|
makeFormat(fmt, t, args...);
|
2016-11-28 18:34:20 +02:00
|
|
|
log(level, fmt);
|
2016-08-12 18:20:57 +02:00
|
|
|
}
|
|
|
|
|
2016-08-12 14:51:14 +02:00
|
|
|
/// Log methods for various log levels
|
2016-08-12 18:20:57 +02:00
|
|
|
inline void error(const std::string & message) const
|
2016-08-12 14:51:14 +02:00
|
|
|
{
|
2016-08-12 18:20:57 +02:00
|
|
|
log(ELogLevel::ERROR, message);
|
2016-08-12 14:51:14 +02:00
|
|
|
};
|
|
|
|
|
2016-08-12 18:20:57 +02:00
|
|
|
template<typename T, typename ... Args>
|
2016-08-20 12:51:20 +02:00
|
|
|
void error(const std::string & format, T t, Args ... args) const
|
2016-08-12 14:51:14 +02:00
|
|
|
{
|
2016-08-12 18:20:57 +02:00
|
|
|
log(ELogLevel::ERROR, format, t, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void warn(const std::string & message) const
|
|
|
|
{
|
|
|
|
log(ELogLevel::WARN, message);
|
2016-08-12 14:51:14 +02:00
|
|
|
};
|
|
|
|
|
2016-08-12 18:20:57 +02:00
|
|
|
template<typename T, typename ... Args>
|
2016-08-20 12:51:20 +02:00
|
|
|
void warn(const std::string & format, T t, Args ... args) const
|
2016-08-12 18:20:57 +02:00
|
|
|
{
|
|
|
|
log(ELogLevel::WARN, format, t, args...);
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:51:14 +02:00
|
|
|
inline void info(const std::string & message) const
|
|
|
|
{
|
|
|
|
log(ELogLevel::INFO, message);
|
|
|
|
};
|
|
|
|
|
2016-08-12 18:20:57 +02:00
|
|
|
template<typename T, typename ... Args>
|
2016-08-20 12:51:20 +02:00
|
|
|
void info(const std::string & format, T t, Args ... args) const
|
2016-08-12 14:51:14 +02:00
|
|
|
{
|
2016-08-12 18:20:57 +02:00
|
|
|
log(ELogLevel::INFO, format, t, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void debug(const std::string & message) const
|
|
|
|
{
|
|
|
|
log(ELogLevel::DEBUG, message);
|
2016-08-12 14:51:14 +02:00
|
|
|
};
|
|
|
|
|
2016-08-12 18:20:57 +02:00
|
|
|
|
|
|
|
template<typename T, typename ... Args>
|
2016-08-20 12:51:20 +02:00
|
|
|
void debug(const std::string & format, T t, Args ... args) const
|
2016-08-12 14:51:14 +02:00
|
|
|
{
|
2016-08-12 18:20:57 +02:00
|
|
|
log(ELogLevel::DEBUG, format, t, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void trace(const std::string & message) const
|
|
|
|
{
|
|
|
|
log(ELogLevel::TRACE, message);
|
2016-08-12 14:51:14 +02:00
|
|
|
};
|
2016-08-12 18:20:57 +02:00
|
|
|
|
|
|
|
template<typename T, typename ... Args>
|
2016-08-20 12:51:20 +02:00
|
|
|
void trace(const std::string & format, T t, Args ... args) const
|
2016-08-12 18:20:57 +02:00
|
|
|
{
|
|
|
|
log(ELogLevel::TRACE, format, t, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
2016-08-20 12:51:20 +02:00
|
|
|
void makeFormat(boost::format & fmt, T t) const
|
2016-08-12 18:20:57 +02:00
|
|
|
{
|
|
|
|
fmt % t;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename ... Args>
|
2016-08-20 12:51:20 +02:00
|
|
|
void makeFormat(boost::format & fmt, T t, Args ... args) const
|
2016-08-12 18:20:57 +02:00
|
|
|
{
|
|
|
|
fmt % t;
|
|
|
|
makeFormat(fmt, args...);
|
|
|
|
}
|
2016-08-12 14:51:14 +02:00
|
|
|
};
|
|
|
|
}
|