CLoggerBase.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. }
  23. namespace vstd
  24. {
  25. class DLL_LINKAGE CLoggerBase
  26. {
  27. public:
  28. virtual ~CLoggerBase(){};
  29. virtual void log(ELogLevel::ELogLevel level, const std::string & message) const = 0;
  30. template<typename T, typename ... Args>
  31. void log(ELogLevel::ELogLevel level, const std::string & format, T t, Args ... args)
  32. {
  33. boost::format fmt(format);
  34. makeFormat(fmt, t, args...);
  35. log(level, fmt.str());
  36. }
  37. /// Log methods for various log levels
  38. inline void error(const std::string & message) const
  39. {
  40. log(ELogLevel::ERROR, message);
  41. };
  42. template<typename T, typename ... Args>
  43. void error(const std::string & format, T t, Args ... args)
  44. {
  45. log(ELogLevel::ERROR, format, t, args...);
  46. }
  47. inline void warn(const std::string & message) const
  48. {
  49. log(ELogLevel::WARN, message);
  50. };
  51. template<typename T, typename ... Args>
  52. void warn(const std::string & format, T t, Args ... args)
  53. {
  54. log(ELogLevel::WARN, format, t, args...);
  55. }
  56. inline void info(const std::string & message) const
  57. {
  58. log(ELogLevel::INFO, message);
  59. };
  60. template<typename T, typename ... Args>
  61. void info(const std::string & format, T t, Args ... args)
  62. {
  63. log(ELogLevel::INFO, format, t, args...);
  64. }
  65. inline void debug(const std::string & message) const
  66. {
  67. log(ELogLevel::DEBUG, message);
  68. };
  69. template<typename T, typename ... Args>
  70. void debug(const std::string & format, T t, Args ... args)
  71. {
  72. log(ELogLevel::DEBUG, format, t, args...);
  73. }
  74. inline void trace(const std::string & message) const
  75. {
  76. log(ELogLevel::TRACE, message);
  77. };
  78. template<typename T, typename ... Args>
  79. void trace(const std::string & format, T t, Args ... args)
  80. {
  81. log(ELogLevel::TRACE, format, t, args...);
  82. }
  83. private:
  84. template <typename T>
  85. void makeFormat(boost::format & fmt, T t)
  86. {
  87. fmt % t;
  88. }
  89. template <typename T, typename ... Args>
  90. void makeFormat(boost::format & fmt, T t, Args ... args)
  91. {
  92. fmt % t;
  93. makeFormat(fmt, args...);
  94. }
  95. };
  96. }