2013-04-05 13:29:46 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CLogger.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
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
#include "../CConsoleHandler.h"
|
|
|
|
|
|
|
|
class CLogger;
|
2013-04-06 00:27:24 +03:00
|
|
|
struct LogRecord;
|
2013-04-05 13:29:46 +03:00
|
|
|
class ILogTarget;
|
|
|
|
|
|
|
|
namespace ELogLevel
|
|
|
|
{
|
2014-08-21 23:26:28 +03:00
|
|
|
enum ELogLevel
|
|
|
|
{
|
|
|
|
NOT_SET = 0,
|
|
|
|
TRACE,
|
|
|
|
DEBUG,
|
|
|
|
INFO,
|
|
|
|
WARN,
|
|
|
|
ERROR
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef VCMI_ANDROID
|
|
|
|
int toAndroid(ELogLevel logLevel);
|
|
|
|
#endif
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
/// The class CLoggerDomain provides convenient access to super domains from a sub domain.
|
2013-04-05 13:29:46 +03:00
|
|
|
class DLL_LINKAGE CLoggerDomain
|
|
|
|
{
|
|
|
|
public:
|
2013-04-14 22:24:31 +03:00
|
|
|
/// Constructs a CLoggerDomain with the domain designated by name.
|
|
|
|
/// Sub-domains can be specified by separating domains by a dot, e.g. "ai.battle". The global domain is named "global".
|
2014-08-21 23:26:28 +03:00
|
|
|
explicit CLoggerDomain(std::string name);
|
2013-04-06 21:34:06 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
const std::string& getName() const;
|
2013-04-14 22:24:31 +03:00
|
|
|
CLoggerDomain getParent() const;
|
|
|
|
bool isGlobalDomain() const;
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
static const std::string DOMAIN_GLOBAL;
|
2013-04-05 13:29:46 +03:00
|
|
|
|
|
|
|
private:
|
2013-04-14 22:24:31 +03:00
|
|
|
std::string name;
|
2013-04-05 13:29:46 +03:00
|
|
|
};
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
/// The class CLoggerStream provides a stream-like way of logging messages.
|
2013-04-06 20:45:02 +03:00
|
|
|
class DLL_LINKAGE CLoggerStream
|
|
|
|
{
|
|
|
|
public:
|
2013-04-14 22:24:31 +03:00
|
|
|
CLoggerStream(const CLogger & logger, ELogLevel::ELogLevel level);
|
|
|
|
~CLoggerStream();
|
2013-04-06 20:45:02 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
template<typename T>
|
|
|
|
CLoggerStream & operator<<(const T & data)
|
|
|
|
{
|
2014-08-11 21:17:17 +03:00
|
|
|
if(!sbuffer)
|
|
|
|
sbuffer = new std::stringstream(std::ios_base::out);
|
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
(*sbuffer) << data;
|
2014-08-11 21:17:17 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
return *this;
|
|
|
|
}
|
2013-04-06 20:45:02 +03:00
|
|
|
|
|
|
|
private:
|
2013-04-14 22:24:31 +03:00
|
|
|
const CLogger & logger;
|
|
|
|
ELogLevel::ELogLevel level;
|
|
|
|
std::stringstream * sbuffer;
|
2013-04-06 20:45:02 +03:00
|
|
|
};
|
|
|
|
|
2014-05-10 22:12:33 +03:00
|
|
|
/// The logger is used to log messages to certain targets of a specific domain/name.
|
|
|
|
/// It is thread-safe and can be used concurrently by several threads.
|
2013-04-11 18:58:01 +03:00
|
|
|
class DLL_LINKAGE CLogger
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
|
|
|
public:
|
2013-04-14 22:24:31 +03:00
|
|
|
inline ELogLevel::ELogLevel getLevel() const;
|
|
|
|
void setLevel(ELogLevel::ELogLevel level);
|
|
|
|
const CLoggerDomain & getDomain() const;
|
2013-04-06 21:34:06 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
/// Logger access methods
|
|
|
|
static CLogger * getLogger(const CLoggerDomain & domain);
|
|
|
|
static CLogger * getGlobalLogger();
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
/// Log methods for various log levels
|
|
|
|
void trace(const std::string & message) const;
|
|
|
|
void debug(const std::string & message) const;
|
|
|
|
void info(const std::string & message) const;
|
|
|
|
void warn(const std::string & message) const;
|
|
|
|
void error(const std::string & message) const;
|
2014-08-11 21:17:17 +03:00
|
|
|
|
|
|
|
/// Log streams for various log levels
|
|
|
|
CLoggerStream traceStream() const;
|
|
|
|
CLoggerStream debugStream() const;
|
|
|
|
CLoggerStream infoStream() const;
|
|
|
|
CLoggerStream warnStream() const;
|
2013-04-14 22:24:31 +03:00
|
|
|
CLoggerStream errorStream() const;
|
2013-04-06 20:45:02 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
inline void log(ELogLevel::ELogLevel level, const std::string & message) const;
|
2013-04-06 20:45:02 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
void addTarget(unique_ptr<ILogTarget> && target);
|
|
|
|
void clearTargets();
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
/// Returns true if a debug/trace log message will be logged, false if not.
|
|
|
|
/// Useful if performance is important and concatenating the log message is a expensive task.
|
|
|
|
bool isDebugEnabled() const;
|
|
|
|
bool isTraceEnabled() const;
|
2013-04-10 19:28:14 +03:00
|
|
|
|
2013-04-05 13:29:46 +03:00
|
|
|
private:
|
2013-04-14 22:24:31 +03:00
|
|
|
explicit CLogger(const CLoggerDomain & domain);
|
|
|
|
inline ELogLevel::ELogLevel getEffectiveLevel() const; /// Returns the log level applied on this logger whether directly or indirectly.
|
|
|
|
inline void callTargets(const LogRecord & record) const;
|
|
|
|
|
|
|
|
CLoggerDomain domain;
|
|
|
|
CLogger * parent;
|
|
|
|
ELogLevel::ELogLevel level;
|
2013-06-29 16:05:48 +03:00
|
|
|
std::vector<unique_ptr<ILogTarget> > targets;
|
2013-04-14 22:24:31 +03:00
|
|
|
mutable boost::mutex mx;
|
|
|
|
static boost::recursive_mutex smx;
|
2013-04-05 13:29:46 +03:00
|
|
|
};
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
extern DLL_LINKAGE CLogger * logGlobal;
|
|
|
|
extern DLL_LINKAGE CLogger * logBonus;
|
|
|
|
extern DLL_LINKAGE CLogger * logNetwork;
|
|
|
|
extern DLL_LINKAGE CLogger * logAi;
|
2013-07-16 21:12:47 +03:00
|
|
|
extern DLL_LINKAGE CLogger * logAnim;
|
2013-04-10 19:28:14 +03:00
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// RAII class for tracing the program execution.
|
|
|
|
/// It prints "Leaving function XYZ" automatically when the object gets destructed.
|
2013-04-20 14:34:01 +03:00
|
|
|
class DLL_LINKAGE CTraceLogger : boost::noncopyable
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CTraceLogger(const CLogger * logger, const std::string & beginMessage, const std::string & endMessage);
|
|
|
|
~CTraceLogger();
|
|
|
|
|
|
|
|
private:
|
|
|
|
const CLogger * logger;
|
|
|
|
std::string endMessage;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Macros for tracing the control flow of the application conveniently. If the LOG_TRACE macro is used it should be
|
|
|
|
/// the first statement in the function. Logging traces via this macro have almost no impact when the trace is disabled.
|
2013-04-20 14:34:01 +03:00
|
|
|
///
|
|
|
|
#define RAII_TRACE(logger, onEntry, onLeave) \
|
|
|
|
unique_ptr<CTraceLogger> ctl00; \
|
|
|
|
if(logger->isTraceEnabled()) \
|
|
|
|
ctl00 = make_unique<CTraceLogger>(logger, onEntry, onLeave);
|
|
|
|
|
|
|
|
#define LOG_TRACE(logger) RAII_TRACE(logger, \
|
|
|
|
boost::str(boost::format("Entering %s.") % BOOST_CURRENT_FUNCTION), \
|
|
|
|
boost::str(boost::format("Leaving %s.") % BOOST_CURRENT_FUNCTION))
|
|
|
|
|
|
|
|
|
|
|
|
#define LOG_TRACE_PARAMS(logger, formatStr, params) RAII_TRACE(logger, \
|
|
|
|
boost::str(boost::format("Entering %s: " + std::string(formatStr) + ".") % BOOST_CURRENT_FUNCTION % params), \
|
|
|
|
boost::str(boost::format("Leaving %s.") % BOOST_CURRENT_FUNCTION))
|
2013-04-09 17:31:36 +03:00
|
|
|
|
2013-04-07 13:17:55 +03:00
|
|
|
/* ---------------------------------------------------------------------------- */
|
|
|
|
/* Implementation/Detail classes, Private API */
|
|
|
|
/* ---------------------------------------------------------------------------- */
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
/// The class CLogManager is a global storage for logger objects.
|
2013-04-07 13:17:55 +03:00
|
|
|
class DLL_LINKAGE CLogManager : public boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
2013-04-14 22:24:31 +03:00
|
|
|
static CLogManager & get();
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
void addLogger(CLogger * logger);
|
|
|
|
CLogger * getLogger(const CLoggerDomain & domain); /// Returns a logger or nullptr if no one is registered for the given domain.
|
2013-04-07 13:17:55 +03:00
|
|
|
|
|
|
|
private:
|
2013-04-14 22:24:31 +03:00
|
|
|
CLogManager();
|
|
|
|
~CLogManager();
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
std::map<std::string, CLogger *> loggers;
|
|
|
|
mutable boost::mutex mx;
|
|
|
|
static boost::recursive_mutex smx;
|
2013-04-07 13:17:55 +03:00
|
|
|
};
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
/// The struct LogRecord holds the log message and additional logging information.
|
2013-04-07 13:17:55 +03:00
|
|
|
struct DLL_LINKAGE LogRecord
|
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
LogRecord(const CLoggerDomain & domain, ELogLevel::ELogLevel level, const std::string & message)
|
2014-09-23 16:01:43 +03:00
|
|
|
: domain(domain), level(level), message(message), timeStamp(boost::posix_time::microsec_clock::local_time()),
|
2014-08-11 21:17:17 +03:00
|
|
|
threadId(boost::this_thread::get_id()) { }
|
2013-04-14 22:24:31 +03:00
|
|
|
|
|
|
|
CLoggerDomain domain;
|
|
|
|
ELogLevel::ELogLevel level;
|
|
|
|
std::string message;
|
|
|
|
boost::posix_time::ptime timeStamp;
|
|
|
|
boost::thread::id threadId;
|
2013-04-07 13:17:55 +03:00
|
|
|
};
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
/// The class CLogFormatter formats log records.
|
|
|
|
///
|
|
|
|
/// There are several pattern characters which can be used to format a log record:
|
|
|
|
/// %d = Date/Time
|
|
|
|
/// %l = Log level
|
|
|
|
/// %n = Logger name
|
|
|
|
/// %t = Thread ID
|
|
|
|
/// %m = Message
|
2013-04-07 13:17:55 +03:00
|
|
|
class DLL_LINKAGE CLogFormatter
|
|
|
|
{
|
|
|
|
public:
|
2013-04-14 22:24:31 +03:00
|
|
|
CLogFormatter();
|
2014-08-11 21:17:17 +03:00
|
|
|
CLogFormatter(const CLogFormatter & copy);
|
|
|
|
CLogFormatter(CLogFormatter && move);
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
CLogFormatter(const std::string & pattern);
|
|
|
|
|
|
|
|
CLogFormatter & operator=(const CLogFormatter & copy);
|
|
|
|
CLogFormatter & operator=(CLogFormatter && move);
|
2014-04-01 22:46:47 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
void setPattern(const std::string & pattern);
|
2014-08-11 21:17:17 +03:00
|
|
|
void setPattern(std::string && pattern);
|
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
const std::string & getPattern() const;
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
std::string format(const LogRecord & record) const;
|
2013-04-07 13:17:55 +03:00
|
|
|
|
|
|
|
private:
|
2013-04-14 22:24:31 +03:00
|
|
|
std::string pattern;
|
2014-04-01 22:46:47 +03:00
|
|
|
mutable std::stringstream dateStream;
|
2013-04-07 13:17:55 +03:00
|
|
|
};
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
/// The interface ILogTarget is used by all log target implementations. It holds
|
|
|
|
/// the abstract method write which sub-classes should implement.
|
2013-04-07 13:17:55 +03:00
|
|
|
class DLL_LINKAGE ILogTarget : public boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
2013-04-14 22:24:31 +03:00
|
|
|
virtual ~ILogTarget() { };
|
|
|
|
virtual void write(const LogRecord & record) = 0;
|
2013-04-07 13:17:55 +03:00
|
|
|
};
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
/// The class CColorMapping maps a logger name and a level to a specific color. Supports domain inheritance.
|
2013-04-07 13:17:55 +03:00
|
|
|
class DLL_LINKAGE CColorMapping
|
|
|
|
{
|
|
|
|
public:
|
2013-04-14 22:24:31 +03:00
|
|
|
CColorMapping();
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
void setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor::EConsoleTextColor color);
|
|
|
|
EConsoleTextColor::EConsoleTextColor getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const;
|
2013-04-07 13:17:55 +03:00
|
|
|
|
|
|
|
private:
|
2013-04-14 22:24:31 +03:00
|
|
|
std::map<std::string, std::map<ELogLevel::ELogLevel, EConsoleTextColor::EConsoleTextColor> > map;
|
2013-04-07 13:17:55 +03:00
|
|
|
};
|
|
|
|
|
2014-05-10 22:12:33 +03:00
|
|
|
/// This target is a logging target which writes message to the console.
|
|
|
|
/// The target may be shared among multiple loggers. All methods except write aren't thread-safe.
|
|
|
|
/// The console target is intended to be configured once and then added to a logger.
|
2013-04-07 13:17:55 +03:00
|
|
|
class DLL_LINKAGE CLogConsoleTarget : public ILogTarget
|
|
|
|
{
|
|
|
|
public:
|
2013-04-14 22:24:31 +03:00
|
|
|
explicit CLogConsoleTarget(CConsoleHandler * console);
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
bool isColoredOutputEnabled() const;
|
|
|
|
void setColoredOutputEnabled(bool coloredOutputEnabled);
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
ELogLevel::ELogLevel getThreshold() const;
|
|
|
|
void setThreshold(ELogLevel::ELogLevel threshold);
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
const CLogFormatter & getFormatter() const;
|
|
|
|
void setFormatter(const CLogFormatter & formatter);
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
const CColorMapping & getColorMapping() const;
|
|
|
|
void setColorMapping(const CColorMapping & colorMapping);
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-22 17:49:28 +03:00
|
|
|
void write(const LogRecord & record) override;
|
2013-04-07 13:17:55 +03:00
|
|
|
|
|
|
|
private:
|
2013-04-14 22:24:31 +03:00
|
|
|
CConsoleHandler * console;
|
|
|
|
ELogLevel::ELogLevel threshold;
|
|
|
|
bool coloredOutputEnabled;
|
|
|
|
CLogFormatter formatter;
|
|
|
|
CColorMapping colorMapping;
|
|
|
|
mutable boost::mutex mx;
|
2013-04-07 13:17:55 +03:00
|
|
|
};
|
|
|
|
|
2014-05-10 22:12:33 +03:00
|
|
|
/// This target is a logging target which writes messages to a log file.
|
|
|
|
/// The target may be shared among multiple loggers. All methods except write aren't thread-safe.
|
|
|
|
/// The file target is intended to be configured once and then added to a logger.
|
2013-04-07 13:17:55 +03:00
|
|
|
class DLL_LINKAGE CLogFileTarget : public ILogTarget
|
|
|
|
{
|
|
|
|
public:
|
2014-08-27 13:31:58 +03:00
|
|
|
/// Constructs a CLogFileTarget and opens the file designated by filePath. If the append parameter is true, the file
|
2013-04-14 22:24:31 +03:00
|
|
|
/// will be appended to. Otherwise the file designated by filePath will be truncated before being opened.
|
2014-08-27 13:31:58 +03:00
|
|
|
explicit CLogFileTarget(boost::filesystem::path filePath, bool append = true);
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
const CLogFormatter & getFormatter() const;
|
|
|
|
void setFormatter(const CLogFormatter & formatter);
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2013-04-22 17:49:28 +03:00
|
|
|
void write(const LogRecord & record) override;
|
2013-04-07 13:17:55 +03:00
|
|
|
|
|
|
|
private:
|
2014-08-11 21:17:17 +03:00
|
|
|
boost::filesystem::ofstream file;
|
2013-04-14 22:24:31 +03:00
|
|
|
CLogFormatter formatter;
|
|
|
|
mutable boost::mutex mx;
|
2013-04-07 13:17:55 +03:00
|
|
|
};
|