CLoggerBase.h 5.0 KB

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