CLoggerBase.h 2.7 KB

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