CLogger.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include "../filesystem/FileStream.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CLogger;
  15. struct LogRecord;
  16. class ILogTarget;
  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: 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; /// 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. : domain(domain),
  90. level(level),
  91. message(message),
  92. timeStamp(boost::posix_time::microsec_clock::local_time()),
  93. threadId(boost::lexical_cast<std::string>(boost::this_thread::get_id())) { }
  94. CLoggerDomain domain;
  95. ELogLevel::ELogLevel level;
  96. std::string message;
  97. boost::posix_time::ptime timeStamp;
  98. std::string threadId;
  99. };
  100. /// The class CLogFormatter formats log records.
  101. ///
  102. /// There are several pattern characters which can be used to format a log record:
  103. /// %d = Date/Time
  104. /// %l = Log level
  105. /// %n = Logger name
  106. /// %t = Thread ID
  107. /// %m = Message
  108. class DLL_LINKAGE CLogFormatter
  109. {
  110. public:
  111. CLogFormatter();
  112. CLogFormatter(std::string pattern);
  113. void setPattern(const std::string & pattern);
  114. void setPattern(std::string && pattern);
  115. const std::string & getPattern() const;
  116. std::string format(const LogRecord & record) const;
  117. private:
  118. std::string pattern;
  119. };
  120. /// The interface ILogTarget is used by all log target implementations. It holds
  121. /// the abstract method write which sub-classes should implement.
  122. class DLL_LINKAGE ILogTarget : public boost::noncopyable
  123. {
  124. public:
  125. virtual ~ILogTarget() { };
  126. virtual void write(const LogRecord & record) = 0;
  127. };
  128. /// The class CColorMapping maps a logger name and a level to a specific color. Supports domain inheritance.
  129. class DLL_LINKAGE CColorMapping
  130. {
  131. public:
  132. CColorMapping();
  133. void setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor::EConsoleTextColor color);
  134. EConsoleTextColor::EConsoleTextColor getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const;
  135. private:
  136. std::map<std::string, std::map<ELogLevel::ELogLevel, EConsoleTextColor::EConsoleTextColor> > map;
  137. };
  138. /// This target is a logging target which writes message to the console.
  139. /// The target may be shared among multiple loggers. All methods except write aren't thread-safe.
  140. /// The console target is intended to be configured once and then added to a logger.
  141. class DLL_LINKAGE CLogConsoleTarget : public ILogTarget
  142. {
  143. public:
  144. explicit CLogConsoleTarget(CConsoleHandler * console);
  145. bool isColoredOutputEnabled() const;
  146. void setColoredOutputEnabled(bool coloredOutputEnabled);
  147. ELogLevel::ELogLevel getThreshold() const;
  148. void setThreshold(ELogLevel::ELogLevel threshold);
  149. const CLogFormatter & getFormatter() const;
  150. void setFormatter(const CLogFormatter & formatter);
  151. const CColorMapping & getColorMapping() const;
  152. void setColorMapping(const CColorMapping & colorMapping);
  153. void write(const LogRecord & record) override;
  154. private:
  155. #if !defined(VCMI_MOBILE)
  156. CConsoleHandler * console;
  157. #endif
  158. ELogLevel::ELogLevel threshold;
  159. bool coloredOutputEnabled;
  160. CLogFormatter formatter;
  161. CColorMapping colorMapping;
  162. mutable std::mutex mx;
  163. };
  164. /// This target is a logging target which writes messages to a log file.
  165. /// The target may be shared among multiple loggers. All methods except write aren't thread-safe.
  166. /// The file target is intended to be configured once and then added to a logger.
  167. class DLL_LINKAGE CLogFileTarget : public ILogTarget
  168. {
  169. public:
  170. /// Constructs a CLogFileTarget and opens the file designated by filePath. If the append parameter is true, the file
  171. /// will be appended to. Otherwise the file designated by filePath will be truncated before being opened.
  172. explicit CLogFileTarget(const boost::filesystem::path & filePath, bool append = true);
  173. ~CLogFileTarget();
  174. const CLogFormatter & getFormatter() const;
  175. void setFormatter(const CLogFormatter & formatter);
  176. void write(const LogRecord & record) override;
  177. private:
  178. FileStream file;
  179. CLogFormatter formatter;
  180. mutable std::mutex mx;
  181. };
  182. VCMI_LIB_NAMESPACE_END