CLogger.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. class CGLogger;
  12. struct LogRecord;
  13. class ILogTarget;
  14. namespace ELogLevel
  15. {
  16. /**
  17. * The log level enum holds various log level definitions.
  18. */
  19. enum ELogLevel
  20. {
  21. NOT_SET = 0,
  22. TRACE,
  23. DEBUG,
  24. INFO,
  25. WARN,
  26. ERROR
  27. };
  28. }
  29. /**
  30. * The logger domain provides convenient access to super domains from a sub domain.
  31. */
  32. class DLL_LINKAGE CLoggerDomain
  33. {
  34. public:
  35. /**
  36. * Constructor.
  37. *
  38. * @param domain The domain name. Sub-domains can be specified by separating domains by a dot, e.g. "ai.battle". The global domain is named "global".
  39. */
  40. CLoggerDomain(const std::string & name);
  41. /**
  42. * Gets the parent logger domain.
  43. *
  44. * @return the parent logger domain or the same domain logger if this method has been called on the global domain
  45. */
  46. CLoggerDomain getParent() const;
  47. /**
  48. * Returns true if this domain is the global domain.
  49. *
  50. * @return true if this is the global domain or false if not
  51. */
  52. bool isGlobalDomain() const;
  53. /**
  54. * Gets the name of the domain.
  55. *
  56. * @return the name of the domain
  57. */
  58. std::string getName() const;
  59. /** Constant to the global domain name. */
  60. static const std::string DOMAIN_GLOBAL;
  61. private:
  62. /** The domain name. */
  63. std::string name;
  64. };
  65. /**
  66. * The logger is used to log messages to certain targets of a specific domain/name. Temporary name is
  67. * CGLogger, should be renamed to CLogger after refactoring.
  68. */
  69. class DLL_LINKAGE CGLogger : public boost::noncopyable
  70. {
  71. public:
  72. /**
  73. * Gets a logger by domain.
  74. *
  75. * @param domain The logger domain.
  76. * @return the logger object
  77. */
  78. static CGLogger * getLogger(const CLoggerDomain & domain);
  79. /**
  80. * Gets the global logger which is the parent of all domain-specific loggers.
  81. *
  82. * @return the global logger object
  83. */
  84. static CGLogger * getGlobalLogger();
  85. /**
  86. * Logs a message with the trace level.
  87. *
  88. * @param message The message to log.
  89. */
  90. void trace(const std::string & message) const;
  91. /**
  92. * Logs a message with the debug level.
  93. *
  94. * @param message The message to log.
  95. */
  96. void debug(const std::string & message) const;
  97. /**
  98. * Logs a message with the info level.
  99. *
  100. * @param message The message to log.
  101. */
  102. void info(const std::string & message) const;
  103. /**
  104. * Logs a message with the warn level.
  105. *
  106. * @param message The message to log.
  107. */
  108. void warn(const std::string & message) const;
  109. /**
  110. * Logs a message with the error level.
  111. *
  112. * @param message The message to log.
  113. */
  114. void error(const std::string & message) const;
  115. /**
  116. * Gets the log level applied for this logger. The default level for the root logger is INFO.
  117. *
  118. * @return the log level
  119. */
  120. inline ELogLevel::ELogLevel getLevel() const;
  121. /**
  122. * Sets the log level.
  123. *
  124. * @param level The log level.
  125. */
  126. void setLevel(ELogLevel::ELogLevel level);
  127. /**
  128. * Gets the logger domain.
  129. *
  130. * @return the domain of the logger
  131. */
  132. const CLoggerDomain & getDomain() const;
  133. /**
  134. * Adds a target to this logger and indirectly to all loggers which derive from this logger.
  135. * The logger holds strong-ownership of the target object.
  136. *
  137. * @param target The log target to add.
  138. */
  139. void addTarget(ILogTarget * target);
  140. /**
  141. * Destructor.
  142. */
  143. ~CGLogger();
  144. private:
  145. /**
  146. * Constructor.
  147. *
  148. * @param domain The domain of the logger.
  149. */
  150. explicit CGLogger(const CLoggerDomain & domain);
  151. /**
  152. * Gets the parent logger.
  153. *
  154. * @return the parent logger or nullptr if this is the root logger
  155. */
  156. CGLogger * getParent() const;
  157. /**
  158. * Logs a message of the given log level.
  159. *
  160. * @param level The log level of the message to log.
  161. * @param message The message to log.
  162. */
  163. inline void log(ELogLevel::ELogLevel level, const std::string & message) const;
  164. /**
  165. * Gets the effective log level.
  166. *
  167. * @return the effective log level with respect to parent log levels
  168. */
  169. inline ELogLevel::ELogLevel getEffectiveLevel() const;
  170. /**
  171. * Calls all targets in the hierarchy to write the message.
  172. *
  173. * @param record The log record to write.
  174. */
  175. inline void callTargets(const LogRecord & record) const;
  176. /**
  177. * Gets all log targets attached to this logger.
  178. *
  179. * @return all log targets as a list
  180. */
  181. inline std::list<ILogTarget *> getTargets() const;
  182. /** The domain of the logger. */
  183. CLoggerDomain domain;
  184. /** A reference to the parent logger. */
  185. CGLogger * parent;
  186. /** The log level of the logger. */
  187. ELogLevel::ELogLevel level;
  188. /** A list of log targets. */
  189. std::list<ILogTarget *> targets;
  190. /** The shared mutex for providing synchronous thread-safe access to the logger. */
  191. mutable boost::shared_mutex mx;
  192. /** The unique mutex for providing thread-safe get logger access. */
  193. static boost::mutex smx;
  194. };
  195. //extern CLogger * logGlobal;