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"
|
2016-01-26 23:42:15 +02:00
|
|
|
#include "../filesystem/FileStream.h"
|
2013-04-11 18:58:01 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
class CLogger;
|
2013-04-06 00:27:24 +03:00
|
|
|
struct LogRecord;
|
2013-04-05 13:29:46 +03:00
|
|
|
class ILogTarget;
|
|
|
|
|
2016-08-12 14:51:14 +02:00
|
|
|
|
2013-04-05 13:29:46 +03:00
|
|
|
namespace ELogLevel
|
|
|
|
{
|
2014-08-21 23:26:28 +03:00
|
|
|
#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-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.
|
2016-08-12 14:51:14 +02:00
|
|
|
class DLL_LINKAGE CLogger: public vstd::CLoggerBase
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
|
|
|
public:
|
2016-10-02 20:21:20 +02:00
|
|
|
ELogLevel::ELogLevel getLevel() const;
|
2013-04-14 22:24:31 +03:00
|
|
|
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
|
|
|
|
2016-08-12 14:51:14 +02:00
|
|
|
void log(ELogLevel::ELogLevel level, const std::string & message) const override;
|
2016-11-28 18:34:20 +02:00
|
|
|
void log(ELogLevel::ELogLevel level, const boost::format & fmt) const override;
|
2013-04-06 20:45:02 +03:00
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
void addTarget(std::unique_ptr<ILogTarget> && target);
|
2013-04-14 22:24:31 +03:00
|
|
|
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.
|
2017-08-11 19:03:05 +02:00
|
|
|
bool isDebugEnabled() const override;
|
|
|
|
bool isTraceEnabled() const override;
|
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;
|
2015-12-29 04:43:33 +02:00
|
|
|
std::vector<std::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
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------- */
|
|
|
|
/* 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.
|
2016-10-02 20:21:20 +02:00
|
|
|
std::vector<std::string> getRegisteredDomains() const;
|
2013-04-07 13:17:55 +03:00
|
|
|
|
|
|
|
private:
|
2013-04-14 22:24:31 +03:00
|
|
|
CLogManager();
|
2016-10-02 20:21:20 +02:00
|
|
|
virtual ~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)
|
2015-08-30 19:26:19 +02:00
|
|
|
: domain(domain),
|
|
|
|
level(level),
|
|
|
|
message(message),
|
|
|
|
timeStamp(boost::posix_time::microsec_clock::local_time()),
|
|
|
|
threadId(boost::lexical_cast<std::string>(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;
|
2015-08-30 19:26:19 +02:00
|
|
|
std::string 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);
|
2016-01-26 23:42:15 +02:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
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;
|
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:
|
2023-02-25 09:15:09 +02:00
|
|
|
#if !defined(VCMI_IOS) && !defined(VCMI_ANDROID)
|
2013-04-14 22:24:31 +03:00
|
|
|
CConsoleHandler * console;
|
2022-09-27 06:05:10 +02:00
|
|
|
#endif
|
2013-04-14 22:24:31 +03:00
|
|
|
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);
|
2019-05-10 11:34:37 +02:00
|
|
|
~CLogFileTarget();
|
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:
|
2016-01-26 23:42:15 +02:00
|
|
|
FileStream file;
|
2013-04-14 22:24:31 +03:00
|
|
|
CLogFormatter formatter;
|
|
|
|
mutable boost::mutex mx;
|
2013-04-07 13:17:55 +03:00
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|