CLogger.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. class CGLogger;
  66. /**
  67. * The logger stream provides a stream-like way of logging messages.
  68. */
  69. class DLL_LINKAGE CLoggerStream
  70. {
  71. public:
  72. CLoggerStream(const CGLogger & logger, ELogLevel::ELogLevel level);
  73. ~CLoggerStream();
  74. template<typename T>
  75. CLoggerStream & operator<<(const T & data)
  76. {
  77. if(!sbuffer) sbuffer = new std::stringstream();
  78. (*sbuffer) << data;
  79. return *this;
  80. }
  81. private:
  82. const CGLogger & logger;
  83. ELogLevel::ELogLevel level;
  84. std::stringstream * sbuffer;
  85. };
  86. /**
  87. * The logger is used to log messages to certain targets of a specific domain/name. Temporary name is
  88. * CGLogger, should be renamed to CLogger after refactoring.
  89. */
  90. class DLL_LINKAGE CGLogger : public boost::noncopyable
  91. {
  92. public:
  93. /**
  94. * Gets a logger by domain.
  95. *
  96. * @param domain The logger domain.
  97. * @return the logger object
  98. */
  99. static CGLogger * getLogger(const CLoggerDomain & domain);
  100. /**
  101. * Gets the global logger which is the parent of all domain-specific loggers.
  102. *
  103. * @return the global logger object
  104. */
  105. static CGLogger * getGlobalLogger();
  106. /**
  107. * Logs a message with the trace level.
  108. *
  109. * @param message The message to log.
  110. */
  111. void trace(const std::string & message) const;
  112. CLoggerStream traceStream() const;
  113. /**
  114. * Logs a message with the debug level.
  115. *
  116. * @param message The message to log.
  117. */
  118. void debug(const std::string & message) const;
  119. CLoggerStream debugStream() const;
  120. /**
  121. * Logs a message with the info level.
  122. *
  123. * @param message The message to log.
  124. */
  125. void info(const std::string & message) const;
  126. CLoggerStream infoStream() const;
  127. /**
  128. * Logs a message with the warn level.
  129. *
  130. * @param message The message to log.
  131. */
  132. void warn(const std::string & message) const;
  133. CLoggerStream warnStream() const;
  134. /**
  135. * Logs a message with the error level.
  136. *
  137. * @param message The message to log.
  138. */
  139. void error(const std::string & message) const;
  140. CLoggerStream errorStream() const;
  141. /**
  142. * Logs a message of the given log level.
  143. *
  144. * @param level The log level of the message to log.
  145. * @param message The message to log.
  146. */
  147. inline void log(ELogLevel::ELogLevel level, const std::string & message) const;
  148. /**
  149. * Gets the log level applied for this logger. The default level for the root logger is INFO.
  150. *
  151. * @return the log level
  152. */
  153. inline ELogLevel::ELogLevel getLevel() const;
  154. /**
  155. * Sets the log level.
  156. *
  157. * @param level The log level.
  158. */
  159. void setLevel(ELogLevel::ELogLevel level);
  160. /**
  161. * Gets the logger domain.
  162. *
  163. * @return the domain of the logger
  164. */
  165. const CLoggerDomain & getDomain() const;
  166. /**
  167. * Adds a target to this logger and indirectly to all loggers which derive from this logger.
  168. * The logger holds strong-ownership of the target object.
  169. *
  170. * @param target The log target to add.
  171. */
  172. void addTarget(ILogTarget * target);
  173. /**
  174. * Destructor.
  175. */
  176. ~CGLogger();
  177. private:
  178. /**
  179. * Constructor.
  180. *
  181. * @param domain The domain of the logger.
  182. */
  183. explicit CGLogger(const CLoggerDomain & domain);
  184. /**
  185. * Gets the parent logger.
  186. *
  187. * @return the parent logger or nullptr if this is the root logger
  188. */
  189. CGLogger * getParent() const;
  190. /**
  191. * Gets the effective log level.
  192. *
  193. * @return the effective log level with respect to parent log levels
  194. */
  195. inline ELogLevel::ELogLevel getEffectiveLevel() const;
  196. /**
  197. * Calls all targets in the hierarchy to write the message.
  198. *
  199. * @param record The log record to write.
  200. */
  201. inline void callTargets(const LogRecord & record) const;
  202. /**
  203. * Gets all log targets attached to this logger.
  204. *
  205. * @return all log targets as a list
  206. */
  207. inline std::list<ILogTarget *> getTargets() const;
  208. /** The domain of the logger. */
  209. CLoggerDomain domain;
  210. /** A reference to the parent logger. */
  211. CGLogger * parent;
  212. /** The log level of the logger. */
  213. ELogLevel::ELogLevel level;
  214. /** A list of log targets. */
  215. std::list<ILogTarget *> targets;
  216. /** The shared mutex for providing synchronous thread-safe access to the logger. */
  217. mutable boost::shared_mutex mx;
  218. /** The unique mutex for providing thread-safe get logger access. */
  219. static boost::mutex smx;
  220. };
  221. //extern CLogger * logGlobal;