pattern_formatter.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <unordered_map>
  14. namespace spdlog {
  15. namespace details {
  16. // padding information.
  17. struct padding_info
  18. {
  19. enum class pad_side
  20. {
  21. left,
  22. right,
  23. center
  24. };
  25. padding_info() = default;
  26. padding_info(size_t width, padding_info::pad_side side, bool truncate)
  27. : width_(width)
  28. , side_(side)
  29. , truncate_(truncate)
  30. , enabled_(true)
  31. {}
  32. bool enabled() const
  33. {
  34. return enabled_;
  35. }
  36. size_t width_ = 0;
  37. pad_side side_ = pad_side::left;
  38. bool truncate_ = false;
  39. bool enabled_ = false;
  40. };
  41. class SPDLOG_API flag_formatter
  42. {
  43. public:
  44. explicit flag_formatter(padding_info padinfo)
  45. : padinfo_(padinfo)
  46. {}
  47. flag_formatter() = default;
  48. virtual ~flag_formatter() = default;
  49. virtual void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0;
  50. protected:
  51. padding_info padinfo_;
  52. };
  53. } // namespace details
  54. class SPDLOG_API custom_flag_formatter : public details::flag_formatter
  55. {
  56. public:
  57. virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
  58. void set_padding_info(const details::padding_info &padding)
  59. {
  60. flag_formatter::padinfo_ = padding;
  61. }
  62. };
  63. class SPDLOG_API pattern_formatter final : public formatter
  64. {
  65. public:
  66. using custom_flags = std::unordered_map<char, std::unique_ptr<custom_flag_formatter>>;
  67. explicit pattern_formatter(std::string pattern, pattern_time_type time_type = pattern_time_type::local,
  68. std::string eol = spdlog::details::os::default_eol, custom_flags custom_user_flags = custom_flags());
  69. // use default pattern is not given
  70. explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
  71. pattern_formatter(const pattern_formatter &other) = delete;
  72. pattern_formatter &operator=(const pattern_formatter &other) = delete;
  73. std::unique_ptr<formatter> clone() const override;
  74. void format(const details::log_msg &msg, memory_buf_t &dest) override;
  75. template<typename T, typename... Args>
  76. pattern_formatter &add_flag(char flag, Args &&...args)
  77. {
  78. custom_handlers_[flag] = details::make_unique<T>(std::forward<Args>(args)...);
  79. return *this;
  80. }
  81. void set_pattern(std::string pattern);
  82. void need_localtime(bool need = true);
  83. private:
  84. std::string pattern_;
  85. std::string eol_;
  86. pattern_time_type pattern_time_type_;
  87. bool need_localtime_;
  88. std::tm cached_tm_;
  89. std::chrono::seconds last_log_secs_;
  90. std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
  91. custom_flags custom_handlers_;
  92. std::tm get_time_(const details::log_msg &msg);
  93. template<typename Padder>
  94. void handle_flag_(char flag, details::padding_info padding);
  95. // Extract given pad spec (e.g. %8X)
  96. // Advance the given it pass the end of the padding spec found (if any)
  97. // Return padding.
  98. static details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
  99. void compile_pattern_(const std::string &pattern);
  100. };
  101. } // namespace spdlog
  102. #ifdef SPDLOG_HEADER_ONLY
  103. # include "pattern_formatter-inl.h"
  104. #endif