CLogger.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 boost::mutex mx;
  65. static boost::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 boost::mutex mx;
  83. static boost::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(const CLogFormatter & copy);
  113. CLogFormatter(CLogFormatter && move);
  114. CLogFormatter(const std::string & pattern);
  115. CLogFormatter & operator=(const CLogFormatter & copy);
  116. CLogFormatter & operator=(CLogFormatter && move);
  117. void setPattern(const std::string & pattern);
  118. void setPattern(std::string && pattern);
  119. const std::string & getPattern() const;
  120. std::string format(const LogRecord & record) const;
  121. private:
  122. std::string pattern;
  123. };
  124. /// The interface ILogTarget is used by all log target implementations. It holds
  125. /// the abstract method write which sub-classes should implement.
  126. class DLL_LINKAGE ILogTarget : public boost::noncopyable
  127. {
  128. public:
  129. virtual ~ILogTarget() { };
  130. virtual void write(const LogRecord & record) = 0;
  131. };
  132. /// The class CColorMapping maps a logger name and a level to a specific color. Supports domain inheritance.
  133. class DLL_LINKAGE CColorMapping
  134. {
  135. public:
  136. CColorMapping();
  137. void setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor::EConsoleTextColor color);
  138. EConsoleTextColor::EConsoleTextColor getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const;
  139. private:
  140. std::map<std::string, std::map<ELogLevel::ELogLevel, EConsoleTextColor::EConsoleTextColor> > map;
  141. };
  142. /// This target is a logging target which writes message to the console.
  143. /// The target may be shared among multiple loggers. All methods except write aren't thread-safe.
  144. /// The console target is intended to be configured once and then added to a logger.
  145. class DLL_LINKAGE CLogConsoleTarget : public ILogTarget
  146. {
  147. public:
  148. explicit CLogConsoleTarget(CConsoleHandler * console);
  149. bool isColoredOutputEnabled() const;
  150. void setColoredOutputEnabled(bool coloredOutputEnabled);
  151. ELogLevel::ELogLevel getThreshold() const;
  152. void setThreshold(ELogLevel::ELogLevel threshold);
  153. const CLogFormatter & getFormatter() const;
  154. void setFormatter(const CLogFormatter & formatter);
  155. const CColorMapping & getColorMapping() const;
  156. void setColorMapping(const CColorMapping & colorMapping);
  157. void write(const LogRecord & record) override;
  158. private:
  159. #ifndef VCMI_IOS
  160. CConsoleHandler * console;
  161. #endif
  162. ELogLevel::ELogLevel threshold;
  163. bool coloredOutputEnabled;
  164. CLogFormatter formatter;
  165. CColorMapping colorMapping;
  166. mutable boost::mutex mx;
  167. };
  168. /// This target is a logging target which writes messages to a log file.
  169. /// The target may be shared among multiple loggers. All methods except write aren't thread-safe.
  170. /// The file target is intended to be configured once and then added to a logger.
  171. class DLL_LINKAGE CLogFileTarget : public ILogTarget
  172. {
  173. public:
  174. /// Constructs a CLogFileTarget and opens the file designated by filePath. If the append parameter is true, the file
  175. /// will be appended to. Otherwise the file designated by filePath will be truncated before being opened.
  176. explicit CLogFileTarget(boost::filesystem::path filePath, bool append = true);
  177. ~CLogFileTarget();
  178. const CLogFormatter & getFormatter() const;
  179. void setFormatter(const CLogFormatter & formatter);
  180. void write(const LogRecord & record) override;
  181. private:
  182. FileStream file;
  183. CLogFormatter formatter;
  184. mutable boost::mutex mx;
  185. };
  186. VCMI_LIB_NAMESPACE_END