25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

209 satır
5.4 KiB

  1. #pragma once
  2. #include <sstream>
  3. #include <cpplogging/types.h>
  4. #define cpplogging_log(p_logger, p_level) \
  5. if (p_logger.is_enabled(::cpplogging::log_level::p_level)) \
  6. p_logger.log() \
  7. .level(::cpplogging::log_level::p_level) \
  8. .file(__FILE__) \
  9. .line(__LINE__)
  10. namespace cpplogging
  11. {
  12. struct logger;
  13. namespace __impl
  14. {
  15. /**
  16. * @brief Helper class to create a log entry.
  17. */
  18. struct log_helper
  19. : public std::ostringstream
  20. {
  21. private:
  22. logger& _logger;
  23. log_entry_ptr_s _entry;
  24. private:
  25. /**
  26. * @brief Move constructor.
  27. */
  28. log_helper(log_helper&&) = delete;
  29. /**
  30. * @brief Copy constructor.
  31. */
  32. log_helper(const log_helper&) = delete;
  33. public:
  34. /**
  35. * @brief Constructor.
  36. *
  37. * @param[in] p_logger Logger this log helper belongs to.
  38. */
  39. inline log_helper(logger& p_logger);
  40. /**
  41. * @brief Constructor.
  42. *
  43. * @param[in] p_logger Logger this log helper belongs to.
  44. * @param[in] p_entry Log entry to create.
  45. */
  46. inline log_helper(logger& p_logger, const log_entry_ptr_s& p_entry);
  47. /**
  48. * @brief Destructor. Will finally write the log entry.
  49. */
  50. inline ~log_helper();
  51. /**
  52. * @brief Set the log level of the entry,
  53. */
  54. inline log_helper& level(log_level level);
  55. /**
  56. * @brief Set the sender of the log entry.
  57. */
  58. inline log_helper& sender(const void * sender);
  59. /**
  60. * @brief Set the sender and the sender type of the log entry.
  61. */
  62. template<typename T_sender>
  63. inline log_helper& sender(const T_sender * sender);
  64. /**
  65. * @brief Set the sender type of the log entry.
  66. */
  67. inline log_helper& sender_type(const std::string& sender_type);
  68. /**
  69. * @brief Set the filename of the log entry.
  70. */
  71. inline log_helper& file(const char * file);
  72. /**
  73. * @brief Set the line number of the log entry.
  74. */
  75. inline log_helper& line(int line);
  76. /**
  77. * @brief Set the message of the log entry.
  78. */
  79. inline log_helper& message(const std::string& msg);
  80. /**
  81. * @brief Add the passed string to the log message.
  82. */
  83. inline log_helper& add_message(const std::string& msg);
  84. /**
  85. * @brief Set the message of the entry using format string.
  86. */
  87. template<typename... T_args>
  88. inline log_helper& format(const std::string& fmt, T_args&&... args);
  89. /**
  90. * @brief Add a message to the entry using format string.
  91. */
  92. template<typename... T_args>
  93. inline log_helper& add_format(const std::string& fmt, T_args&&... args);
  94. private:
  95. /**
  96. * @brief Format a string.
  97. *
  98. * @tparam T_args Arguments of the format string.
  99. *
  100. * @param[in] fmt Format string.
  101. * @param[in] sz Size of the buffer to use for the formatting.
  102. * @param[in] args Arguments of the format.
  103. */
  104. template<typename... T_args>
  105. static inline std::string format(const std::string& fmt, size_t sz, T_args&&... args);
  106. };
  107. }
  108. /**
  109. * @brief Class to create log messages.
  110. */
  111. struct logger
  112. {
  113. public:
  114. friend __impl::log_helper;
  115. private:
  116. std::string _name; //!< name of the logger
  117. private:
  118. /**
  119. * @brief Move constructor.
  120. */
  121. logger(logger&&) = delete;
  122. /**
  123. * @brief Copy constructor.
  124. */
  125. logger(const logger&) = delete;
  126. public:
  127. /**
  128. * @brief Constructor.
  129. */
  130. inline logger(const std::string name);
  131. /**
  132. * @brief Destructor.
  133. */
  134. virtual ~logger() = default;
  135. /**
  136. * @brief Get the name of the logger.
  137. */
  138. inline const std::string& name() const;
  139. /**
  140. * @brief Check if the passed log level is enabled.
  141. *
  142. * @param[in] level Level to check.
  143. *
  144. * @retval true The passed log level is enabled.
  145. * @retval false The passed log level is disabled.
  146. */
  147. virtual bool is_enabled(log_level level) const = 0;
  148. /**
  149. * @brief Write a log entry,
  150. *
  151. * @return Log helper to create log entry.
  152. */
  153. inline __impl::log_helper log();
  154. protected:
  155. /**
  156. * @brief Write a log entry.
  157. *
  158. * @param[in] entry Log entry to write.
  159. */
  160. virtual void write_entry(const log_entry_ptr_s& entry) const = 0;
  161. public:
  162. /**
  163. * @brief Get the instance on an logger with the passed name.
  164. *
  165. * @param[in] name Name of the loggger.
  166. *
  167. * @return Instance of the requested logger.
  168. */
  169. static logger& get(const std::string& name = "");
  170. };
  171. }
  172. #include "logger.inl"