CLogger.h 6.8 KB

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