CLoggerBase.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. return std::string("invalid (") + std::to_string(level) + ")";
  39. }
  40. }
  41. }
  42. namespace vstd
  43. {
  44. class DLL_LINKAGE CLoggerBase
  45. {
  46. public:
  47. virtual ~CLoggerBase(){};
  48. virtual void log(ELogLevel::ELogLevel level, const std::string & message) const = 0;
  49. template<typename T, typename ... Args>
  50. void log(ELogLevel::ELogLevel level, const std::string & format, T t, Args ... args) const
  51. {
  52. boost::format fmt(format);
  53. makeFormat(fmt, t, args...);
  54. log(level, fmt.str());
  55. }
  56. /// Log methods for various log levels
  57. inline void error(const std::string & message) const
  58. {
  59. log(ELogLevel::ERROR, message);
  60. };
  61. template<typename T, typename ... Args>
  62. void error(const std::string & format, T t, Args ... args) const
  63. {
  64. log(ELogLevel::ERROR, format, t, args...);
  65. }
  66. inline void warn(const std::string & message) const
  67. {
  68. log(ELogLevel::WARN, message);
  69. };
  70. template<typename T, typename ... Args>
  71. void warn(const std::string & format, T t, Args ... args) const
  72. {
  73. log(ELogLevel::WARN, format, t, args...);
  74. }
  75. inline void info(const std::string & message) const
  76. {
  77. log(ELogLevel::INFO, message);
  78. };
  79. template<typename T, typename ... Args>
  80. void info(const std::string & format, T t, Args ... args) const
  81. {
  82. log(ELogLevel::INFO, format, t, args...);
  83. }
  84. inline void debug(const std::string & message) const
  85. {
  86. log(ELogLevel::DEBUG, message);
  87. };
  88. template<typename T, typename ... Args>
  89. void debug(const std::string & format, T t, Args ... args) const
  90. {
  91. log(ELogLevel::DEBUG, format, t, args...);
  92. }
  93. inline void trace(const std::string & message) const
  94. {
  95. log(ELogLevel::TRACE, message);
  96. };
  97. template<typename T, typename ... Args>
  98. void trace(const std::string & format, T t, Args ... args) const
  99. {
  100. log(ELogLevel::TRACE, format, t, args...);
  101. }
  102. private:
  103. template <typename T>
  104. void makeFormat(boost::format & fmt, T t) const
  105. {
  106. fmt % t;
  107. }
  108. template <typename T, typename ... Args>
  109. void makeFormat(boost::format & fmt, T t, Args ... args) const
  110. {
  111. fmt % t;
  112. makeFormat(fmt, args...);
  113. }
  114. };
  115. }