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