CLogger.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. enum ELogLevel
  17. {
  18. NOT_SET = 0,
  19. TRACE,
  20. DEBUG,
  21. INFO,
  22. WARN,
  23. ERROR
  24. };
  25. }
  26. /**
  27. * The logger domain provides convenient access to super domains from a sub domain.
  28. */
  29. class DLL_LINKAGE CLoggerDomain
  30. {
  31. public:
  32. /**
  33. * Constructor.
  34. *
  35. * @param name The domain name. Sub-domains can be specified by separating domains by a dot, e.g. "ai.battle". The global domain is named "global".
  36. */
  37. CLoggerDomain(const std::string & name);
  38. // Accessors
  39. std::string getName() const;
  40. // Methods
  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. // Constants
  54. /** Constant to the global domain name. */
  55. static const std::string DOMAIN_GLOBAL;
  56. private:
  57. std::string name;
  58. };
  59. class CGLogger;
  60. /**
  61. * The logger stream provides a stream-like way of logging messages.
  62. */
  63. class DLL_LINKAGE CLoggerStream
  64. {
  65. public:
  66. /**
  67. * Constructs a new logger stream.
  68. *
  69. * @param logger The logger which should be used to log the generated message to.
  70. * @param level The log level of the generated message.
  71. */
  72. CLoggerStream(const CGLogger & logger, ELogLevel::ELogLevel level);
  73. ~CLoggerStream();
  74. // Methods
  75. /**
  76. * Writes data to the logger stream.
  77. *
  78. * @param data Any data can be written to the stream.
  79. */
  80. template<typename T>
  81. CLoggerStream & operator<<(const T & data)
  82. {
  83. if(!sbuffer) sbuffer = new std::stringstream();
  84. (*sbuffer) << data;
  85. return *this;
  86. }
  87. private:
  88. // Data members
  89. const CGLogger & logger;
  90. ELogLevel::ELogLevel level;
  91. std::stringstream * sbuffer;
  92. };
  93. /**
  94. * The logger is used to log messages to certain targets of a specific domain/name. Temporary name is
  95. * CGLogger, should be renamed to CLogger after refactoring.
  96. */
  97. class DLL_LINKAGE CGLogger : public boost::noncopyable
  98. {
  99. public:
  100. ~CGLogger();
  101. // Accessors
  102. inline ELogLevel::ELogLevel getLevel() const;
  103. void setLevel(ELogLevel::ELogLevel level);
  104. const CLoggerDomain & getDomain() const;
  105. // Methods
  106. /**
  107. * Gets a logger by domain.
  108. *
  109. * @param domain The logger domain.
  110. * @return the logger object
  111. */
  112. static CGLogger * getLogger(const CLoggerDomain & domain);
  113. /**
  114. * Gets the global logger which is the parent of all domain-specific loggers.
  115. *
  116. * @return the global logger object
  117. */
  118. static CGLogger * getGlobalLogger();
  119. /**
  120. * Logs a message with the trace level.
  121. *
  122. * @param message The message to log.
  123. */
  124. void trace(const std::string & message) const;
  125. /**
  126. * Returns a logger stream with the trace level.
  127. *
  128. * @return the logger stream
  129. */
  130. CLoggerStream traceStream() const;
  131. /**
  132. * Logs a message with the debug level.
  133. *
  134. * @param message The message to log.
  135. */
  136. void debug(const std::string & message) const;
  137. /**
  138. * Returns a logger stream with the debug level.
  139. *
  140. * @return the logger stream
  141. */
  142. CLoggerStream debugStream() const;
  143. /**
  144. * Logs a message with the info level.
  145. *
  146. * @param message The message to log.
  147. */
  148. void info(const std::string & message) const;
  149. /**
  150. * Returns a logger stream with the info level.
  151. *
  152. * @return the logger stream
  153. */
  154. CLoggerStream infoStream() const;
  155. /**
  156. * Logs a message with the warn level.
  157. *
  158. * @param message The message to log.
  159. */
  160. void warn(const std::string & message) const;
  161. /**
  162. * Returns a logger stream with the warn level.
  163. *
  164. * @return the logger stream
  165. */
  166. CLoggerStream warnStream() const;
  167. /**
  168. * Logs a message with the error level.
  169. *
  170. * @param message The message to log.
  171. */
  172. void error(const std::string & message) const;
  173. /**
  174. * Returns a logger stream with the error level.
  175. *
  176. * @return the logger stream
  177. */
  178. CLoggerStream errorStream() const;
  179. /**
  180. * Logs a message of the given log level.
  181. *
  182. * @param level The log level of the message to log.
  183. * @param message The message to log.
  184. */
  185. inline void log(ELogLevel::ELogLevel level, const std::string & message) const;
  186. /**
  187. * Adds a target to this logger and indirectly to all loggers which derive from this logger.
  188. * The logger holds strong-ownership of the target object.
  189. *
  190. * @param target The log target to add.
  191. */
  192. void addTarget(ILogTarget * target);
  193. private:
  194. // Methods
  195. explicit CGLogger(const CLoggerDomain & domain);
  196. CGLogger * getParent() const;
  197. inline ELogLevel::ELogLevel getEffectiveLevel() const;
  198. inline void callTargets(const LogRecord & record) const;
  199. inline std::list<ILogTarget *> getTargets() const;
  200. // Data members
  201. CLoggerDomain domain;
  202. CGLogger * parent;
  203. ELogLevel::ELogLevel level;
  204. std::list<ILogTarget *> targets;
  205. mutable boost::shared_mutex mx;
  206. static boost::mutex smx;
  207. };