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
|
|
|
|
2019-11-07 03:09:30 +08:00
|
|
|
#include <spdlog/common.h>
|
|
|
|
#include <spdlog/details/log_msg.h>
|
|
|
|
#include <spdlog/details/os.h>
|
|
|
|
#include <spdlog/formatter.h>
|
2016-08-26 05:38:08 +08:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <ctime>
|
|
|
|
#include <memory>
|
2019-04-06 04:05:46 +08:00
|
|
|
|
2016-08-26 05:38:08 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2020-03-20 22:09:30 +08:00
|
|
|
#include <unordered_map>
|
2016-08-26 05:38:08 +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.
|
2018-11-09 21:18:45 +08:00
|
|
|
struct padding_info
|
|
|
|
{
|
|
|
|
enum pad_side
|
|
|
|
{
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
center
|
|
|
|
};
|
|
|
|
|
|
|
|
padding_info() = default;
|
2019-11-14 18:28:23 +08:00
|
|
|
padding_info(size_t width, padding_info::pad_side side, bool truncate)
|
2018-11-09 21:18:45 +08:00
|
|
|
: width_(width)
|
|
|
|
, side_(side)
|
2019-11-14 18:28:23 +08:00
|
|
|
, truncate_(truncate)
|
|
|
|
, enabled_(true)
|
2019-05-12 05:22:39 +08:00
|
|
|
{}
|
2018-11-25 00:21:25 +08:00
|
|
|
|
|
|
|
bool enabled() const
|
|
|
|
{
|
2019-11-14 18:28:23 +08:00
|
|
|
return enabled_;
|
2018-11-25 00:21:25 +08:00
|
|
|
}
|
2020-03-21 21:03:41 +08:00
|
|
|
size_t width_ = 0;
|
|
|
|
pad_side side_ = left;
|
2019-11-14 18:28:23 +08:00
|
|
|
bool truncate_ = false;
|
|
|
|
bool enabled_ = false;
|
2018-11-09 21:18:45 +08:00
|
|
|
};
|
|
|
|
|
2020-03-20 22:09:30 +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)
|
|
|
|
: padinfo_(padinfo)
|
2019-05-12 05:22:39 +08:00
|
|
|
{}
|
2018-11-09 21:18:45 +08:00
|
|
|
flag_formatter() = default;
|
2018-02-25 08:25:15 +08:00
|
|
|
virtual ~flag_formatter() = default;
|
2019-08-28 23:46:09 +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
|
|
|
};
|
|
|
|
|
2019-04-27 07:34:50 +08:00
|
|
|
} // namespace details
|
2016-08-26 05:38:08 +08:00
|
|
|
|
2020-03-20 22:09:30 +08:00
|
|
|
class SPDLOG_API custom_flag_formatter : public details::flag_formatter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
|
2020-03-21 21:03:41 +08:00
|
|
|
|
|
|
|
void set_padding_info(details::padding_info padding)
|
|
|
|
{
|
|
|
|
flag_formatter::padinfo_ = padding;
|
|
|
|
}
|
2020-03-20 22:09:30 +08:00
|
|
|
};
|
|
|
|
|
2020-03-10 03:02:16 +08:00
|
|
|
class SPDLOG_API pattern_formatter final : public formatter
|
2016-08-26 05:38:08 +08:00
|
|
|
{
|
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>>;
|
|
|
|
|
|
|
|
explicit pattern_formatter(std::string pattern, pattern_time_type time_type = pattern_time_type::local,
|
2020-04-26 06:58:55 +08:00
|
|
|
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
|
2019-04-27 07:34:50 +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
|
|
|
|
2020-03-20 22:09:30 +08:00
|
|
|
template<typename T, typename... Args>
|
2020-03-21 19:33:04 +08:00
|
|
|
pattern_formatter &add_flag(char flag, const Args &... args)
|
2020-03-20 22:09:30 +08:00
|
|
|
{
|
|
|
|
custom_handlers_[flag] = details::make_unique<T>(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);
|
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_;
|
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
|
|
|
|
2019-05-12 05:22:39 +08:00
|
|
|
std::tm get_time_(const details::log_msg &msg);
|
2019-07-15 06:17:22 +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.
|
2020-03-04 21:59:45 +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
|
|
|
};
|
|
|
|
} // namespace spdlog
|
2019-04-05 21:57:49 +08:00
|
|
|
|
2019-05-12 05:22:39 +08:00
|
|
|
#ifdef SPDLOG_HEADER_ONLY
|
2019-05-11 18:19:53 +08:00
|
|
|
#include "pattern_formatter-inl.h"
|
2019-04-05 21:57:49 +08:00
|
|
|
#endif
|