Agora  1.2.0
Agora project
pattern_formatter.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
6 #include <spdlog/common.h>
8 #include <spdlog/details/os.h>
9 #include <spdlog/formatter.h>
10 
11 #include <chrono>
12 #include <ctime>
13 #include <memory>
14 
15 #include <string>
16 #include <vector>
17 #include <unordered_map>
18 
19 namespace spdlog {
20 namespace details {
21 
22 // padding information.
24 {
25  enum class pad_side
26  {
27  left,
28  right,
29  center
30  };
31 
32  padding_info() = default;
33  padding_info(size_t width, padding_info::pad_side side, bool truncate)
34  : width_(width)
35  , side_(side)
36  , truncate_(truncate)
37  , enabled_(true)
38  {}
39 
40  bool enabled() const
41  {
42  return enabled_;
43  }
44  size_t width_ = 0;
46  bool truncate_ = false;
47  bool enabled_ = false;
48 };
49 
51 {
52 public:
53  explicit flag_formatter(padding_info padinfo)
54  : padinfo_(padinfo)
55  {}
56  flag_formatter() = default;
57  virtual ~flag_formatter() = default;
58  virtual void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0;
59 
60 protected:
62 };
63 
64 } // namespace details
65 
67 {
68 public:
69  virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
70 
72  {
73  flag_formatter::padinfo_ = padding;
74  }
75 };
76 
78 {
79 public:
80  using custom_flags = std::unordered_map<char, std::unique_ptr<custom_flag_formatter>>;
81 
82  explicit pattern_formatter(std::string pattern, pattern_time_type time_type = pattern_time_type::local,
83  std::string eol = spdlog::details::os::default_eol, custom_flags custom_user_flags = custom_flags());
84 
85  // use default pattern is not given
87 
88  pattern_formatter(const pattern_formatter &other) = delete;
89  pattern_formatter &operator=(const pattern_formatter &other) = delete;
90 
91  std::unique_ptr<formatter> clone() const override;
92  void format(const details::log_msg &msg, memory_buf_t &dest) override;
93 
94  template<typename T, typename... Args>
95  pattern_formatter &add_flag(char flag, Args &&... args)
96  {
97  custom_handlers_[flag] = details::make_unique<T>(std::forward<Args>(args)...);
98  return *this;
99  }
100  void set_pattern(std::string pattern);
101 
102 private:
103  std::string pattern_;
104  std::string eol_;
107  std::tm cached_tm_;
108  std::chrono::seconds last_log_secs_;
109  std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
111 
112  std::tm get_time_(const details::log_msg &msg);
113  template<typename Padder>
114  void handle_flag_(char flag, details::padding_info padding);
115 
116  // Extract given pad spec (e.g. %8X)
117  // Advance the given it pass the end of the padding spec found (if any)
118  // Return padding.
119  static details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
120 
121  void compile_pattern_(const std::string &pattern);
122 };
123 } // namespace spdlog
124 
125 #ifdef SPDLOG_HEADER_ONLY
126 # include "pattern_formatter-inl.h"
127 #endif
spdlog::formatter
Definition: formatter.h:11
spdlog::details::flag_formatter::flag_formatter
flag_formatter(padding_info padinfo)
Definition: pattern_formatter.h:53
spdlog::pattern_formatter::need_localtime_
bool need_localtime_
Definition: pattern_formatter.h:106
spdlog::pattern_formatter::custom_handlers_
custom_flags custom_handlers_
Definition: pattern_formatter.h:110
spdlog::details::padding_info
Definition: pattern_formatter.h:23
spdlog::details::padding_info::pad_side::right
@ right
spdlog::details::flag_formatter
Definition: pattern_formatter.h:50
pattern_formatter-inl.h
spdlog::details::padding_info::pad_side::center
@ center
spdlog::details::padding_info::padding_info
padding_info(size_t width, padding_info::pad_side side, bool truncate)
Definition: pattern_formatter.h:33
spdlog::set_pattern
SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type)
Definition: spdlog-inl.h:30
spdlog::pattern_formatter::pattern_
std::string pattern_
Definition: pattern_formatter.h:103
spdlog::pattern_formatter::custom_flags
std::unordered_map< char, std::unique_ptr< custom_flag_formatter > > custom_flags
Definition: pattern_formatter.h:80
spdlog::details::padding_info::truncate_
bool truncate_
Definition: pattern_formatter.h:46
spdlog::details::flag_formatter::padinfo_
padding_info padinfo_
Definition: pattern_formatter.h:61
spdlog::details::padding_info::pad_side
pad_side
Definition: pattern_formatter.h:25
spdlog::pattern_formatter
Definition: pattern_formatter.h:77
T
T
Definition: simulate_performance.m:4
fmt::v8::basic_memory_buffer
Definition: format.h:677
spdlog::details::padding_info::enabled_
bool enabled_
Definition: pattern_formatter.h:47
spdlog::custom_flag_formatter
Definition: pattern_formatter.h:66
spdlog
Definition: async.h:25
spdlog::details::padding_info::side_
pad_side side_
Definition: pattern_formatter.h:45
spdlog::pattern_formatter::add_flag
pattern_formatter & add_flag(char flag, Args &&... args)
Definition: pattern_formatter.h:95
os.h
spdlog::details::padding_info::padding_info
padding_info()=default
spdlog::pattern_formatter::pattern_time_type_
pattern_time_type pattern_time_type_
Definition: pattern_formatter.h:105
spdlog::pattern_formatter::last_log_secs_
std::chrono::seconds last_log_secs_
Definition: pattern_formatter.h:108
spdlog::pattern_formatter::formatters_
std::vector< std::unique_ptr< details::flag_formatter > > formatters_
Definition: pattern_formatter.h:109
common.h
spdlog::custom_flag_formatter::set_padding_info
void set_padding_info(const details::padding_info &padding)
Definition: pattern_formatter.h:71
spdlog::pattern_time_type::local
@ local
spdlog::details::os::default_eol
static const SPDLOG_CONSTEXPR char * default_eol
Definition: os.h:32
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::pattern_time_type
pattern_time_type
Definition: common.h:267
spdlog::pattern_formatter::cached_tm_
std::tm cached_tm_
Definition: pattern_formatter.h:107
spdlog::details::padding_info::pad_side::left
@ left
spdlog::details::padding_info::enabled
bool enabled() const
Definition: pattern_formatter.h:40
spdlog::pattern_formatter::eol_
std::string eol_
Definition: pattern_formatter.h:104
formatter.h
spdlog::details::padding_info::width_
size_t width_
Definition: pattern_formatter.h:44
SPDLOG_API
#define SPDLOG_API
Definition: common.h:40
log_msg.h
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail2::end
end_tag end(T &&...)
utils::format
std::string format(const T &value)
Definition: utils.h:15