CLogger.h 6.6 KB

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