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
|
|
|
|
2024-01-12 23:33:23 +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_; }
|
2020-03-21 21:03:41 +08:00
|
|
|
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;
|
2020-03-21 21:03:41 +08:00
|
|
|
|
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-11-11 07:18:57 +08:00
|
|
|
|
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;
|
2019-08-28 23:46:09 +08:00
|
|
|
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) {
|
2023-09-01 23:22:40 +08:00
|
|
|
custom_handlers_[flag] = std::make_unique<T>(std::forward<Args>(args)...);
|
2020-03-21 19:33:04 +08:00
|
|
|
return *this;
|
2020-03-20 22:09:30 +08:00
|
|
|
}
|
2020-03-21 21:03:41 +08:00
|
|
|
void set_pattern(std::string pattern);
|
2022-05-07 20:39:10 +08:00
|
|
|
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_;
|
2018-07-07 21:15:17 +08:00
|
|
|
pattern_time_type pattern_time_type_;
|
2022-01-16 02:35:27 +08:00
|
|
|
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_;
|
2018-07-07 21:15:17 +08:00
|
|
|
|
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
|