CLoggerBase.h 2.8 KB

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