CLogger.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * CLogger.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include <boost/date_time/posix_time/posix_time_types.hpp>
  12. #include <boost/date_time/posix_time/time_formatters.hpp>
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CLogger;
  15. class CConsoleHandler;
  16. struct LogRecord;
  17. class ILogTarget;
  18. enum class EConsoleTextColor : int8_t;
  19. namespace ELogLevel
  20. {
  21. #ifdef VCMI_ANDROID
  22. int toAndroid(ELogLevel logLevel);
  23. #endif
  24. }
  25. /// The class CLoggerDomain provides convenient access to super domains from a sub domain.
  26. class DLL_LINKAGE CLoggerDomain
  27. {
  28. public:
  29. /// Constructs a CLoggerDomain with the domain designated by name.
  30. /// Sub-domains can be specified by separating domains by a dot, e.g. "ai.battle". The global domain is named "global".
  31. explicit CLoggerDomain(std::string name);
  32. const std::string& getName() const;
  33. CLoggerDomain getParent() const;
  34. bool isGlobalDomain() const;
  35. static const std::string DOMAIN_GLOBAL;
  36. private:
  37. std::string name;
  38. };
  39. /// The logger is used to log messages to certain targets of a specific domain/name.
  40. /// It is thread-safe and can be used concurrently by several threads.
  41. class DLL_LINKAGE CLogger final: public vstd::CLoggerBase
  42. {
  43. public:
  44. ELogLevel::ELogLevel getLevel() const;
  45. void setLevel(ELogLevel::ELogLevel level);
  46. const CLoggerDomain & getDomain() const;
  47. /// Logger access methods
  48. static CLogger * getLogger(const CLoggerDomain & domain);
  49. static CLogger * getGlobalLogger();
  50. void log(ELogLevel::ELogLevel level, const std::string & message) const override;
  51. void log(ELogLevel::ELogLevel level, const boost::format & fmt) const override;
  52. void addTarget(std::unique_ptr<ILogTarget> && target);
  53. void clearTargets();
  54. /// Returns true if a debug/trace log message will be logged, false if not.
  55. /// Useful if performance is important and concatenating the log message is a expensive task.
  56. bool isDebugEnabled() const override;
  57. bool isTraceEnabled() const override;
  58. private:
  59. explicit CLogger(const CLoggerDomain & domain);
  60. inline ELogLevel::ELogLevel getEffectiveLevel() const override; /// Returns the log level applied on this logger whether directly or indirectly.
  61. inline void callTargets(const LogRecord & record) const;
  62. CLoggerDomain domain;
  63. CLogger * parent;
  64. ELogLevel::ELogLevel level;
  65. std::vector<std::unique_ptr<ILogTarget> > targets;
  66. mutable std::mutex mx;
  67. static std::recursive_mutex smx;
  68. };
  69. /* ---------------------------------------------------------------------------- */
  70. /* Implementation/Detail classes, Private API */
  71. /* ---------------------------------------------------------------------------- */
  72. /// The class CLogManager is a global storage for logger objects.
  73. class DLL_LINKAGE CLogManager : public boost::noncopyable
  74. {
  75. public:
  76. static CLogManager & get();
  77. void addLogger(CLogger * logger);
  78. CLogger * getLogger(const CLoggerDomain & domain); /// Returns a logger or nullptr if no one is registered for the given domain.
  79. std::vector<std::string> getRegisteredDomains() const;
  80. private:
  81. CLogManager();
  82. virtual ~CLogManager();
  83. std::map<std::string, CLogger *> loggers;
  84. mutable std::mutex mx;
  85. static std::recursive_mutex smx;
  86. };
  87. /// The struct LogRecord holds the log message and additional logging information.
  88. struct DLL_LINKAGE LogRecord
  89. {
  90. LogRecord(const CLoggerDomain & domain, ELogLevel::ELogLevel level, const std::string & message);
  91. CLoggerDomain domain;
  92. ELogLevel::ELogLevel level;
  93. std::string message;
  94. boost::posix_time::ptime timeStamp;
  95. std::string threadId;
  96. };
  97. /// The class CLogFormatter formats log records.
  98. ///
  99. /// There are several pattern characters which can be used to format a log record:
  100. /// %d = Date/Time
  101. /// %l = Log level
  102. /// %n = Logger name
  103. /// %t = Thread ID
  104. /// %m = Message
  105. class DLL_LINKAGE CLogFormatter
  106. {
  107. public:
  108. CLogFormatter();
  109. CLogFormatter(std::string pattern);
  110. void setPattern(const std::string & pattern);
  111. void setPattern(std::string && pattern);
  112. const std::string & getPattern() const;
  113. std::string format(const LogRecord & record) const;
  114. private:
  115. std::string pattern;
  116. };
  117. /// The interface ILogTarget is used by all log target implementations. It holds
  118. /// the abstract method write which sub-classes should implement.
  119. class DLL_LINKAGE ILogTarget : public boost::noncopyable
  120. {
  121. public:
  122. virtual ~ILogTarget() { };
  123. virtual void write(const LogRecord & record) = 0;
  124. };
  125. /// The class CColorMapping maps a logger name and a level to a specific color. Supports domain inheritance.
  126. class DLL_LINKAGE CColorMapping
  127. {
  128. public:
  129. CColorMapping();
  130. void setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor color);
  131. EConsoleTextColor getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const;
  132. private:
  133. std::map<std::string, std::map<ELogLevel::ELogLevel, EConsoleTextColor> > map;
  134. };
  135. /// This target is a logging target which writes message to the console.
  136. /// The target may be shared among multiple loggers. All methods except write aren't thread-safe.
  137. /// The console target is intended to be configured once and then added to a logger.
  138. class DLL_LINKAGE CLogConsoleTarget : public ILogTarget
  139. {
  140. public:
  141. explicit CLogConsoleTarget(CConsoleHandler * console);
  142. bool isColoredOutputEnabled() const;
  143. void setColoredOutputEnabled(bool coloredOutputEnabled);
  144. ELogLevel::ELogLevel getThreshold() const;
  145. void setThreshold(ELogLevel::ELogLevel threshold);
  146. const CLogFormatter & getFormatter() const;
  147. void setFormatter(const CLogFormatter & formatter);
  148. const CColorMapping & getColorMapping() const;
  149. void setColorMapping(const CColorMapping & colorMapping);
  150. void write(const LogRecord & record) override;
  151. private:
  152. #if !defined(VCMI_MOBILE)
  153. CConsoleHandler * console;
  154. #endif
  155. ELogLevel::ELogLevel threshold;
  156. bool coloredOutputEnabled;
  157. CLogFormatter formatter;
  158. CColorMapping colorMapping;
  159. mutable std::mutex mx;
  160. };
  161. /// This target is a logging target which writes messages to a log file.
  162. /// The target may be shared among multiple loggers. All methods except write aren't thread-safe.
  163. /// The file target is intended to be configured once and then added to a logger.
  164. class DLL_LINKAGE CLogFileTarget : public ILogTarget
  165. {
  166. public:
  167. /// Constructs a CLogFileTarget and opens the file designated by filePath. If the append parameter is true, the file
  168. /// will be appended to. Otherwise the file designated by filePath will be truncated before being opened.
  169. explicit CLogFileTarget(const boost::filesystem::path & filePath, bool append = true);
  170. ~CLogFileTarget();
  171. const CLogFormatter & getFormatter() const;
  172. void setFormatter(const CLogFormatter & formatter);
  173. void write(const LogRecord & record) override;
  174. private:
  175. std::fstream file;
  176. CLogFormatter formatter;
  177. mutable std::mutex mx;
  178. };
  179. VCMI_LIB_NAMESPACE_END