CLoggerBase.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * CLoggerBase.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. namespace ELogLevel
  12. {
  13. enum ELogLevel
  14. {
  15. NOT_SET = 0,
  16. TRACE,
  17. DEBUG,
  18. INFO,
  19. WARN,
  20. ERROR
  21. };
  22. inline std::string to_string(ELogLevel level)
  23. {
  24. switch (level) {
  25. case NOT_SET:
  26. return "not set";
  27. case TRACE:
  28. return "trace";
  29. case DEBUG:
  30. return "debug";
  31. case INFO:
  32. return "info";
  33. case WARN:
  34. return "warn";
  35. case ERROR:
  36. return "error";
  37. default:
  38. #ifdef NO_STD_TOSTRING
  39. return "invalid";
  40. #else
  41. return std::string("invalid (") + std::to_string(level) + ")";
  42. #endif
  43. }
  44. }
  45. }
  46. namespace vstd
  47. {
  48. class DLL_LINKAGE CLoggerBase
  49. {
  50. public:
  51. virtual ~CLoggerBase();
  52. virtual void log(ELogLevel::ELogLevel level, const std::string & message) const = 0;
  53. virtual void log(ELogLevel::ELogLevel level, const boost::format & fmt) const = 0;
  54. /// Returns true if a debug/trace log message will be logged, false if not.
  55. /// Useful if performance is important and concatenating the log message is a expensive task.
  56. virtual bool isDebugEnabled() const = 0;
  57. virtual bool isTraceEnabled() const = 0;
  58. template<typename T, typename ... Args>
  59. void log(ELogLevel::ELogLevel level, const std::string & format, T t, Args ... args) const
  60. {
  61. try
  62. {
  63. boost::format fmt(format);
  64. makeFormat(fmt, t, args...);
  65. log(level, fmt);
  66. }
  67. catch(...)
  68. {
  69. log(ELogLevel::ERROR, "Log formatting failed, format was:");
  70. log(ELogLevel::ERROR, format);
  71. }
  72. }
  73. /// Log methods for various log levels
  74. inline void error(const std::string & message) const
  75. {
  76. log(ELogLevel::ERROR, message);
  77. };
  78. template<typename T, typename ... Args>
  79. void error(const std::string & format, T t, Args ... args) const
  80. {
  81. log(ELogLevel::ERROR, format, t, args...);
  82. }
  83. inline void warn(const std::string & message) const
  84. {
  85. log(ELogLevel::WARN, message);
  86. };
  87. template<typename T, typename ... Args>
  88. void warn(const std::string & format, T t, Args ... args) const
  89. {
  90. log(ELogLevel::WARN, format, t, args...);
  91. }
  92. inline void info(const std::string & message) const
  93. {
  94. log(ELogLevel::INFO, message);
  95. };
  96. template<typename T, typename ... Args>
  97. void info(const std::string & format, T t, Args ... args) const
  98. {
  99. log(ELogLevel::INFO, format, t, args...);
  100. }
  101. inline void debug(const std::string & message) const
  102. {
  103. log(ELogLevel::DEBUG, message);
  104. };
  105. template<typename T, typename ... Args>
  106. void debug(const std::string & format, T t, Args ... args) const
  107. {
  108. log(ELogLevel::DEBUG, format, t, args...);
  109. }
  110. inline void trace(const std::string & message) const
  111. {
  112. log(ELogLevel::TRACE, message);
  113. };
  114. template<typename T, typename ... Args>
  115. void trace(const std::string & format, T t, Args ... args) const
  116. {
  117. log(ELogLevel::TRACE, format, t, args...);
  118. }
  119. private:
  120. template <typename T>
  121. void makeFormat(boost::format & fmt, T t) const
  122. {
  123. fmt % t;
  124. }
  125. template <typename T, typename ... Args>
  126. void makeFormat(boost::format & fmt, T t, Args ... args) const
  127. {
  128. fmt % t;
  129. makeFormat(fmt, args...);
  130. }
  131. };
  132. /// RAII class for tracing the program execution.
  133. /// It prints "Leaving function XYZ" automatically when the object gets destructed.
  134. class DLL_LINKAGE CTraceLogger
  135. {
  136. public:
  137. CTraceLogger(const CLoggerBase * logger, const std::string & beginMessage, const std::string & endMessage);
  138. CTraceLogger(const CTraceLogger & other) = delete;
  139. ~CTraceLogger();
  140. private:
  141. const CLoggerBase * logger;
  142. std::string endMessage;
  143. };
  144. }//namespace vstd
  145. /// Macros for tracing the control flow of the application conveniently. If the LOG_TRACE macro is used it should be
  146. /// the first statement in the function. Logging traces via this macro have almost no impact when the trace is disabled.
  147. ///
  148. #define RAII_TRACE(logger, onEntry, onLeave) \
  149. std::unique_ptr<vstd::CTraceLogger> ctl00; \
  150. if(logger->isTraceEnabled()) \
  151. ctl00 = make_unique<vstd::CTraceLogger>(logger, onEntry, onLeave);
  152. #define LOG_TRACE(logger) RAII_TRACE(logger, \
  153. boost::str(boost::format("Entering %s.") % BOOST_CURRENT_FUNCTION), \
  154. boost::str(boost::format("Leaving %s.") % BOOST_CURRENT_FUNCTION))
  155. #define LOG_TRACE_PARAMS(logger, formatStr, params) RAII_TRACE(logger, \
  156. boost::str(boost::format("Entering %s: " + std::string(formatStr) + ".") % BOOST_CURRENT_FUNCTION % params), \
  157. boost::str(boost::format("Leaving %s.") % BOOST_CURRENT_FUNCTION))
  158. extern DLL_LINKAGE vstd::CLoggerBase * logGlobal;
  159. extern DLL_LINKAGE vstd::CLoggerBase * logBonus;
  160. extern DLL_LINKAGE vstd::CLoggerBase * logNetwork;
  161. extern DLL_LINKAGE vstd::CLoggerBase * logAi;
  162. extern DLL_LINKAGE vstd::CLoggerBase * logAnim;
  163. extern DLL_LINKAGE vstd::CLoggerBase * logMod;