You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.6KB

  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include "spdlog/common.h"
  5. #include "spdlog/details/log_msg.h"
  6. #include "spdlog/details/os.h"
  7. #include "spdlog/formatter.h"
  8. #include <chrono>
  9. #include <ctime>
  10. #include <memory>
  11. #include <string>
  12. #include <vector>
  13. namespace spdlog {
  14. namespace details {
  15. // padding information.
  16. struct padding_info
  17. {
  18. enum pad_side
  19. {
  20. left,
  21. right,
  22. center
  23. };
  24. padding_info() = default;
  25. padding_info(size_t width, padding_info::pad_side side)
  26. : width_(width)
  27. , side_(side)
  28. {}
  29. bool enabled() const
  30. {
  31. return width_ != 0;
  32. }
  33. const size_t width_ = 0;
  34. const pad_side side_ = left;
  35. };
  36. class flag_formatter
  37. {
  38. public:
  39. explicit flag_formatter(padding_info padinfo)
  40. : padinfo_(padinfo)
  41. {}
  42. flag_formatter() = default;
  43. virtual ~flag_formatter() = default;
  44. virtual void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0;
  45. protected:
  46. padding_info padinfo_;
  47. };
  48. } // namespace details
  49. class pattern_formatter final : public formatter
  50. {
  51. public:
  52. explicit pattern_formatter(
  53. std::string pattern, pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
  54. // use default pattern is not given
  55. explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
  56. pattern_formatter(const pattern_formatter &other) = delete;
  57. pattern_formatter &operator=(const pattern_formatter &other) = delete;
  58. std::unique_ptr<formatter> clone() const override;
  59. void format(const details::log_msg &msg, memory_buf_t &dest) override;
  60. private:
  61. std::string pattern_;
  62. std::string eol_;
  63. pattern_time_type pattern_time_type_;
  64. std::tm cached_tm_;
  65. std::chrono::seconds last_log_secs_;
  66. std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
  67. std::tm get_time_(const details::log_msg &msg);
  68. template<typename Padder>
  69. void handle_flag_(char flag, details::padding_info padding);
  70. // Extract given pad spec (e.g. %8X)
  71. // Advance the given it pass the end of the padding spec found (if any)
  72. // Return padding.
  73. details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
  74. void compile_pattern_(const std::string &pattern);
  75. };
  76. } // namespace spdlog
  77. #ifdef SPDLOG_HEADER_ONLY
  78. #include "pattern_formatter-inl.h"
  79. #endif