CLogger.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. /**
  60. * The logger stream provides a stream-like way of logging messages.
  61. */
  62. class DLL_LINKAGE CLoggerStream
  63. {
  64. public:
  65. /**
  66. * Constructs a new logger stream.
  67. *
  68. * @param logger The logger which should be used to log the generated message to.
  69. * @param level The log level of the generated message.
  70. */
  71. CLoggerStream(const CGLogger & logger, ELogLevel::ELogLevel level);
  72. ~CLoggerStream();
  73. // Methods
  74. /**
  75. * Writes data to the logger stream.
  76. *
  77. * @param data Any data can be written to the stream.
  78. */
  79. template<typename T>
  80. CLoggerStream & operator<<(const T & data)
  81. {
  82. if(!sbuffer) sbuffer = new std::stringstream();
  83. (*sbuffer) << data;
  84. return *this;
  85. }
  86. private:
  87. // Data members
  88. const CGLogger & logger;
  89. ELogLevel::ELogLevel level;
  90. std::stringstream * sbuffer;
  91. };
  92. /**
  93. * The logger is used to log messages to certain targets of a specific domain/name. Temporary name is
  94. * CGLogger, should be renamed to CLogger after refactoring.
  95. */
  96. class DLL_LINKAGE CGLogger : public boost::noncopyable
  97. {
  98. public:
  99. ~CGLogger();
  100. // Accessors
  101. inline ELogLevel::ELogLevel getLevel() const;
  102. void setLevel(ELogLevel::ELogLevel level);
  103. const CLoggerDomain & getDomain() const;
  104. // Methods
  105. /**
  106. * Gets a logger by domain.
  107. *
  108. * @param domain The logger domain.
  109. * @return the logger object
  110. */
  111. static CGLogger * getLogger(const CLoggerDomain & domain);
  112. /**
  113. * Gets the global logger which is the parent of all domain-specific loggers.
  114. *
  115. * @return the global logger object
  116. */
  117. static CGLogger * getGlobalLogger();
  118. /**
  119. * Logs a message with the trace level.
  120. *
  121. * @param message The message to log.
  122. */
  123. void trace(const std::string & message) const;
  124. /**
  125. * Returns a logger stream with the trace level.
  126. *
  127. * @return the logger stream
  128. */
  129. CLoggerStream traceStream() const;
  130. /**
  131. * Logs a message with the debug level.
  132. *
  133. * @param message The message to log.
  134. */
  135. void debug(const std::string & message) const;
  136. /**
  137. * Returns a logger stream with the debug level.
  138. *
  139. * @return the logger stream
  140. */
  141. CLoggerStream debugStream() const;
  142. /**
  143. * Logs a message with the info level.
  144. *
  145. * @param message The message to log.
  146. */
  147. void info(const std::string & message) const;
  148. /**
  149. * Returns a logger stream with the info level.
  150. *
  151. * @return the logger stream
  152. */
  153. CLoggerStream infoStream() const;
  154. /**
  155. * Logs a message with the warn level.
  156. *
  157. * @param message The message to log.
  158. */
  159. void warn(const std::string & message) const;
  160. /**
  161. * Returns a logger stream with the warn level.
  162. *
  163. * @return the logger stream
  164. */
  165. CLoggerStream warnStream() const;
  166. /**
  167. * Logs a message with the error level.
  168. *
  169. * @param message The message to log.
  170. */
  171. void error(const std::string & message) const;
  172. /**
  173. * Returns a logger stream with the error level.
  174. *
  175. * @return the logger stream
  176. */
  177. CLoggerStream errorStream() const;
  178. /**
  179. * Logs a message of the given log level.
  180. *
  181. * @param level The log level of the message to log.
  182. * @param message The message to log.
  183. */
  184. inline void log(ELogLevel::ELogLevel level, const std::string & message) const;
  185. /**
  186. * Adds a target to this logger and indirectly to all loggers which derive from this logger.
  187. * The logger holds strong-ownership of the target object.
  188. *
  189. * @param target The log target to add.
  190. */
  191. void addTarget(ILogTarget * target);
  192. private:
  193. // Methods
  194. explicit CGLogger(const CLoggerDomain & domain);
  195. CGLogger * getParent() const;
  196. inline ELogLevel::ELogLevel getEffectiveLevel() const;
  197. inline void callTargets(const LogRecord & record) const;
  198. inline std::list<ILogTarget *> getTargets() const;
  199. // Data members
  200. CLoggerDomain domain;
  201. CGLogger * parent;
  202. ELogLevel::ELogLevel level;
  203. std::list<ILogTarget *> targets;
  204. mutable boost::shared_mutex mx;
  205. static boost::mutex smx;
  206. };
  207. /* ---------------------------------------------------------------------------- */
  208. /* Implementation/Detail classes, Private API */
  209. /* ---------------------------------------------------------------------------- */
  210. /**
  211. * The log manager is a global storage of all logger objects.
  212. */
  213. class DLL_LINKAGE CLogManager : public boost::noncopyable
  214. {
  215. public:
  216. ~CLogManager();
  217. // Methods
  218. /**
  219. * Gets an instance of the log manager.
  220. *
  221. * @return an instance of the log manager
  222. */
  223. static CLogManager * get();
  224. /**
  225. * Adds a logger. The log manager holds strong ownership of the logger object.
  226. *
  227. * @param logger The logger to add.
  228. */
  229. void addLogger(CGLogger * logger);
  230. /**
  231. * Gets a logger by domain.
  232. *
  233. * @param domain The domain of the logger.
  234. * @return a logger by domain or nullptr if the logger was not found
  235. */
  236. CGLogger * getLogger(const CLoggerDomain & domain);
  237. private:
  238. // Methods
  239. CLogManager();
  240. // Data members
  241. static CLogManager * instance;
  242. std::map<std::string, CGLogger *> loggers;
  243. mutable boost::shared_mutex mx;
  244. static boost::mutex smx;
  245. };
  246. /**
  247. * The log records holds the log message and additional logging information.
  248. */
  249. struct DLL_LINKAGE LogRecord
  250. {
  251. LogRecord(const CLoggerDomain & domain, ELogLevel::ELogLevel level, const std::string & message)
  252. : domain(domain), level(level), message(message), timeStamp(boost::posix_time::second_clock::local_time()), threadId(boost::this_thread::get_id())
  253. {
  254. }
  255. /** The logger domain. */
  256. CLoggerDomain domain;
  257. /** The log level. */
  258. ELogLevel::ELogLevel level;
  259. /** The message. */
  260. std::string message;
  261. /** The time when the message was created. */
  262. boost::posix_time::ptime timeStamp;
  263. /** The thread id. */
  264. boost::thread::id threadId;
  265. };
  266. /**
  267. * The log formatter formats log records.
  268. *
  269. * There are several pattern characters which can be used to format a log record:
  270. * %d = Date/Time
  271. * %l = Log level
  272. * %n = Logger name
  273. * %t = Thread ID
  274. * %m = Message
  275. */
  276. class DLL_LINKAGE CLogFormatter
  277. {
  278. public:
  279. CLogFormatter();
  280. /**
  281. * Constructor.
  282. *
  283. * @param pattern The pattern to format the log record with.
  284. */
  285. CLogFormatter(const std::string & pattern);
  286. // Accessors
  287. void setPattern(const std::string & pattern);
  288. const std::string & getPattern() const;
  289. // Methods
  290. /**
  291. * Formats a log record.
  292. *
  293. * @param record The log record to format.
  294. * @return the formatted log record as a string
  295. */
  296. std::string format(const LogRecord & record) const;
  297. private:
  298. // Data members
  299. std::string pattern;
  300. };
  301. /**
  302. * The interface log target is used by all log target implementations. It holds
  303. * the abstract method write which sub-classes should implement.
  304. */
  305. class DLL_LINKAGE ILogTarget : public boost::noncopyable
  306. {
  307. public:
  308. virtual ~ILogTarget() { };
  309. /**
  310. * Writes a log record.
  311. *
  312. * @param record The log record to write.
  313. */
  314. virtual void write(const LogRecord & record) = 0;
  315. };
  316. /**
  317. * The color mapping maps a logger name and a level to a specific color.
  318. */
  319. class DLL_LINKAGE CColorMapping
  320. {
  321. public:
  322. /**
  323. * Constructor. There are default color mappings for the root logger, which child loggers inherit if not overriden.
  324. */
  325. CColorMapping();
  326. // Methods
  327. /**
  328. * Sets a console text color for a logger name and a level.
  329. *
  330. * @param domain The domain of the logger.
  331. * @param level The logger level.
  332. * @param color The console text color to use as the mapping.
  333. */
  334. void setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor::EConsoleTextColor color);
  335. /**
  336. * Gets a console text color for a logger name and a level.
  337. *
  338. * @param domain The domain of the logger.
  339. * @param level The logger level.
  340. * @return the console text color which has been applied for the mapping
  341. */
  342. EConsoleTextColor::EConsoleTextColor getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const;
  343. private:
  344. // Data members
  345. std::map<std::string, std::map<ELogLevel::ELogLevel, EConsoleTextColor::EConsoleTextColor> > map;
  346. };
  347. /**
  348. * The console target is a logging target which writes message to the console.
  349. */
  350. class DLL_LINKAGE CLogConsoleTarget : public ILogTarget
  351. {
  352. public:
  353. /**
  354. * Constructor.
  355. *
  356. * @param console Optional. The console handler which is used to output messages to the console.
  357. */
  358. explicit CLogConsoleTarget(CConsoleHandler * console);
  359. // Accessors
  360. bool isColoredOutputEnabled() const;
  361. void setColoredOutputEnabled(bool coloredOutputEnabled);
  362. ELogLevel::ELogLevel getThreshold() const;
  363. void setThreshold(ELogLevel::ELogLevel threshold);
  364. const CLogFormatter & getFormatter() const;
  365. void setFormatter(const CLogFormatter & formatter);
  366. const CColorMapping & getColorMapping() const;
  367. void setColorMapping(const CColorMapping & colorMapping);
  368. // Methods
  369. /**
  370. * Writes a log record to the console.
  371. *
  372. * @param record The log record to write.
  373. */
  374. void write(const LogRecord & record);
  375. private:
  376. // Data members
  377. CConsoleHandler * console;
  378. ELogLevel::ELogLevel threshold;
  379. bool coloredOutputEnabled;
  380. CLogFormatter formatter;
  381. CColorMapping colorMapping;
  382. mutable boost::mutex mx;
  383. };
  384. /**
  385. * The log file target is a logging target which writes messages to a log file.
  386. */
  387. class DLL_LINKAGE CLogFileTarget : public ILogTarget
  388. {
  389. public:
  390. /**
  391. * Constructor.
  392. *
  393. * @param filePath The file path of the log file.
  394. */
  395. explicit CLogFileTarget(const std::string & filePath);
  396. ~CLogFileTarget();
  397. // Accessors
  398. const CLogFormatter & getFormatter() const;
  399. void setFormatter(const CLogFormatter & formatter);
  400. // Methods
  401. /**
  402. * Writes a log record to the log file.
  403. *
  404. * @param record The log record to write.
  405. */
  406. void write(const LogRecord & record);
  407. private:
  408. // Data members
  409. std::ofstream file;
  410. CLogFormatter formatter;
  411. mutable boost::mutex mx;
  412. };