spdlog/include/spdlog/pattern_formatter.h

109 lines
3.5 KiB
C
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2016-08-26 05:38:08 +08:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
2019-05-12 01:06:17 +08:00
2016-08-26 05:38:08 +08:00
#include <chrono>
#include <ctime>
#include <memory>
#include <string>
2020-03-20 22:09:30 +08:00
#include <unordered_map>
2023-09-25 07:35:55 +08:00
#include <vector>
2016-08-26 05:38:08 +08:00
#include "./common.h"
#include "./details/log_msg.h"
#include "./details/os.h"
#include "./formatter.h"
2023-09-29 05:20:26 +08:00
2018-03-17 18:47:46 +08:00
namespace spdlog {
namespace details {
2018-06-12 23:48:22 +08:00
2018-11-11 00:03:11 +08:00
// padding information.
2023-09-25 07:35:55 +08:00
struct padding_info {
enum class pad_side { left, right, center };
2018-11-09 21:18:45 +08:00
padding_info() = default;
2019-11-14 18:28:23 +08:00
padding_info(size_t width, padding_info::pad_side side, bool truncate)
2023-09-25 21:19:10 +08:00
: width_(width),
side_(side),
truncate_(truncate),
enabled_(true) {}
2023-09-25 07:35:55 +08:00
bool enabled() const { return enabled_; }
size_t width_ = 0;
2020-09-28 00:08:24 +08:00
pad_side side_ = pad_side::left;
2019-11-14 18:28:23 +08:00
bool truncate_ = false;
bool enabled_ = false;
2018-11-09 21:18:45 +08:00
};
2023-09-25 07:35:55 +08:00
class SPDLOG_API flag_formatter {
2016-08-26 05:38:08 +08:00
public:
2018-11-09 21:18:45 +08:00
explicit flag_formatter(padding_info padinfo)
2023-09-25 07:35:55 +08:00
: padinfo_(padinfo) {}
2018-11-09 21:18:45 +08:00
flag_formatter() = default;
2018-02-25 08:25:15 +08:00
virtual ~flag_formatter() = default;
2024-01-13 15:37:32 +08:00
virtual void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0;
2018-11-09 21:18:45 +08:00
protected:
padding_info padinfo_;
2016-08-26 05:38:08 +08:00
};
2023-09-25 21:40:05 +08:00
} // namespace details
2016-08-26 05:38:08 +08:00
2023-09-25 07:35:55 +08:00
class SPDLOG_API custom_flag_formatter : public details::flag_formatter {
2020-03-20 22:09:30 +08:00
public:
virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
2024-01-13 15:37:32 +08:00
void set_padding_info(const details::padding_info &padding) { flag_formatter::padinfo_ = padding; }
2020-03-20 22:09:30 +08:00
};
2023-09-25 07:35:55 +08:00
class SPDLOG_API pattern_formatter final : public formatter {
2018-06-26 06:13:02 +08:00
public:
2020-03-20 22:09:30 +08:00
using custom_flags = std::unordered_map<char, std::unique_ptr<custom_flag_formatter>>;
2023-09-25 07:35:55 +08:00
explicit pattern_formatter(std::string pattern,
pattern_time_type time_type = pattern_time_type::local,
std::string eol = spdlog::details::os::default_eol,
custom_flags custom_user_flags = custom_flags());
2019-04-27 07:34:50 +08:00
2019-04-06 04:05:46 +08:00
// use default pattern is not given
2023-09-25 07:35:55 +08:00
explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local,
std::string eol = spdlog::details::os::default_eol);
2018-07-23 05:13:52 +08:00
pattern_formatter(const pattern_formatter &other) = delete;
pattern_formatter &operator=(const pattern_formatter &other) = delete;
2019-04-06 04:05:46 +08:00
std::unique_ptr<formatter> clone() const override;
void format(const details::log_msg &msg, memory_buf_t &dest) override;
2016-08-26 05:38:08 +08:00
2023-09-25 07:35:55 +08:00
template <typename T, typename... Args>
pattern_formatter &add_flag(char flag, Args &&...args) {
custom_handlers_[flag] = std::make_unique<T>(std::forward<Args>(args)...);
return *this;
2020-03-20 22:09:30 +08:00
}
void set_pattern(std::string pattern);
void need_localtime(bool need = true);
2020-03-20 22:09:30 +08:00
2018-06-26 06:13:02 +08:00
private:
2018-07-23 05:13:52 +08:00
std::string pattern_;
2018-07-23 02:52:46 +08:00
std::string eol_;
pattern_time_type pattern_time_type_;
bool need_localtime_;
2018-06-26 07:00:33 +08:00
std::tm cached_tm_;
2018-06-26 06:13:02 +08:00
std::chrono::seconds last_log_secs_;
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
2020-03-20 22:09:30 +08:00
custom_flags custom_handlers_;
2024-11-30 18:45:14 +08:00
std::tm get_time_(const details::log_msg &msg) const;
2023-09-25 07:35:55 +08:00
template <typename Padder>
2019-04-06 04:05:46 +08:00
void handle_flag_(char flag, details::padding_info padding);
2019-04-27 07:34:50 +08:00
2018-11-09 21:18:45 +08:00
// Extract given pad spec (e.g. %8X)
// Advance the given it pass the end of the padding spec found (if any)
// Return padding.
2024-01-13 15:37:32 +08:00
static details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
2019-04-27 07:34:50 +08:00
2019-04-06 04:05:46 +08:00
void compile_pattern_(const std::string &pattern);
2018-06-26 06:13:02 +08:00
};
2023-09-25 21:40:05 +08:00
} // namespace spdlog