CLoggerBase.h 4.8 KB

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