2
0

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