File indexing completed on 2025-12-16 10:32:30
0001
0002
0003
0004 #pragma once
0005
0006 #include <spdlog/common.h>
0007 #include <spdlog/details/log_msg.h>
0008 #include <spdlog/details/os.h>
0009 #include <spdlog/formatter.h>
0010
0011 #include <chrono>
0012 #include <ctime>
0013 #include <memory>
0014
0015 #include <string>
0016 #include <unordered_map>
0017 #include <vector>
0018
0019 namespace spdlog {
0020 namespace details {
0021
0022
0023 struct padding_info {
0024 enum class pad_side { left, right, center };
0025
0026 padding_info() = default;
0027 padding_info(size_t width, padding_info::pad_side side, bool truncate)
0028 : width_(width),
0029 side_(side),
0030 truncate_(truncate),
0031 enabled_(true) {}
0032
0033 bool enabled() const { return enabled_; }
0034 size_t width_ = 0;
0035 pad_side side_ = pad_side::left;
0036 bool truncate_ = false;
0037 bool enabled_ = false;
0038 };
0039
0040 class SPDLOG_API flag_formatter {
0041 public:
0042 explicit flag_formatter(padding_info padinfo)
0043 : padinfo_(padinfo) {}
0044 flag_formatter() = default;
0045 virtual ~flag_formatter() = default;
0046 virtual void format(const details::log_msg &msg,
0047 const std::tm &tm_time,
0048 memory_buf_t &dest) = 0;
0049
0050 protected:
0051 padding_info padinfo_;
0052 };
0053
0054 }
0055
0056 class SPDLOG_API custom_flag_formatter : public details::flag_formatter {
0057 public:
0058 virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
0059
0060 void set_padding_info(const details::padding_info &padding) {
0061 flag_formatter::padinfo_ = padding;
0062 }
0063 };
0064
0065 class SPDLOG_API pattern_formatter final : public formatter {
0066 public:
0067 using custom_flags = std::unordered_map<char, std::unique_ptr<custom_flag_formatter>>;
0068
0069 explicit pattern_formatter(std::string pattern,
0070 pattern_time_type time_type = pattern_time_type::local,
0071 std::string eol = spdlog::details::os::default_eol,
0072 custom_flags custom_user_flags = custom_flags());
0073
0074
0075 explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local,
0076 std::string eol = spdlog::details::os::default_eol);
0077
0078 pattern_formatter(const pattern_formatter &other) = delete;
0079 pattern_formatter &operator=(const pattern_formatter &other) = delete;
0080
0081 std::unique_ptr<formatter> clone() const override;
0082 void format(const details::log_msg &msg, memory_buf_t &dest) override;
0083
0084 template <typename T, typename... Args>
0085 pattern_formatter &add_flag(char flag, Args &&...args) {
0086 custom_handlers_[flag] = details::make_unique<T>(std::forward<Args>(args)...);
0087 return *this;
0088 }
0089 void set_pattern(std::string pattern);
0090 void need_localtime(bool need = true);
0091
0092 private:
0093 std::string pattern_;
0094 std::string eol_;
0095 pattern_time_type pattern_time_type_;
0096 bool need_localtime_;
0097 std::tm cached_tm_;
0098 std::chrono::seconds last_log_secs_;
0099 std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
0100 custom_flags custom_handlers_;
0101
0102 std::tm get_time_(const details::log_msg &msg);
0103 template <typename Padder>
0104 void handle_flag_(char flag, details::padding_info padding);
0105
0106
0107
0108
0109 static details::padding_info handle_padspec_(std::string::const_iterator &it,
0110 std::string::const_iterator end);
0111
0112 void compile_pattern_(const std::string &pattern);
0113 };
0114 }
0115
0116 #ifdef SPDLOG_HEADER_ONLY
0117 #include "pattern_formatter-inl.h"
0118 #endif