CLoggerBase.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. template<typename T, typename ... Args>
  55. void log(ELogLevel::ELogLevel level, const std::string & format, T t, Args ... args) const
  56. {
  57. boost::format fmt(format);
  58. makeFormat(fmt, t, args...);
  59. log(level, fmt);
  60. }
  61. /// Log methods for various log levels
  62. inline void error(const std::string & message) const
  63. {
  64. log(ELogLevel::ERROR, message);
  65. };
  66. template<typename T, typename ... Args>
  67. void error(const std::string & format, T t, Args ... args) const
  68. {
  69. log(ELogLevel::ERROR, format, t, args...);
  70. }
  71. inline void warn(const std::string & message) const
  72. {
  73. log(ELogLevel::WARN, message);
  74. };
  75. template<typename T, typename ... Args>
  76. void warn(const std::string & format, T t, Args ... args) const
  77. {
  78. log(ELogLevel::WARN, format, t, args...);
  79. }
  80. inline void info(const std::string & message) const
  81. {
  82. log(ELogLevel::INFO, message);
  83. };
  84. template<typename T, typename ... Args>
  85. void info(const std::string & format, T t, Args ... args) const
  86. {
  87. log(ELogLevel::INFO, format, t, args...);
  88. }
  89. inline void debug(const std::string & message) const
  90. {
  91. log(ELogLevel::DEBUG, message);
  92. };
  93. template<typename T, typename ... Args>
  94. void debug(const std::string & format, T t, Args ... args) const
  95. {
  96. log(ELogLevel::DEBUG, format, t, args...);
  97. }
  98. inline void trace(const std::string & message) const
  99. {
  100. log(ELogLevel::TRACE, message);
  101. };
  102. template<typename T, typename ... Args>
  103. void trace(const std::string & format, T t, Args ... args) const
  104. {
  105. log(ELogLevel::TRACE, format, t, args...);
  106. }
  107. private:
  108. template <typename T>
  109. void makeFormat(boost::format & fmt, T t) const
  110. {
  111. fmt % t;
  112. }
  113. template <typename T, typename ... Args>
  114. void makeFormat(boost::format & fmt, T t, Args ... args) const
  115. {
  116. fmt % t;
  117. makeFormat(fmt, args...);
  118. }
  119. };
  120. }