You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/include/spdlog/details/pattern_formatter.h

100 lines
2.6 KiB

// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
9 years ago
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
7 years ago
#include "spdlog/common.h"
#include "spdlog/details/log_msg.h"
#include "spdlog/details/os.h"
#include "spdlog/formatter.h"
9 years ago
#include <chrono>
#include <ctime>
#include <memory>
7 years ago
9 years ago
#include <string>
#include <vector>
8 years ago
namespace spdlog {
namespace details {
// padding information.
struct padding_info
{
enum pad_side
{
left,
right,
center
};
padding_info() = default;
padding_info(size_t width, padding_info::pad_side side)
: width_(width)
, side_(side)
{}
bool enabled() const
{
return width_ != 0;
}
const size_t width_ = 0;
const pad_side side_ = left;
};
9 years ago
class flag_formatter
{
public:
explicit flag_formatter(padding_info padinfo)
: padinfo_(padinfo)
{}
flag_formatter() = default;
virtual ~flag_formatter() = default;
virtual void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) = 0;
protected:
padding_info padinfo_;
9 years ago
};
7 years ago
} // namespace details
9 years ago
class pattern_formatter final : public formatter
9 years ago
{
public:
7 years ago
explicit pattern_formatter(
7 years ago
std::string pattern, pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
7 years ago
// use default pattern is not given
7 years ago
explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
7 years ago
pattern_formatter(const pattern_formatter &other) = delete;
pattern_formatter &operator=(const pattern_formatter &other) = delete;
7 years ago
std::unique_ptr<formatter> clone() const override;
7 years ago
void format(const details::log_msg &msg, fmt::memory_buffer &dest) override;
9 years ago
private:
7 years ago
std::string pattern_;
std::string eol_;
pattern_time_type pattern_time_type_;
std::tm cached_tm_;
std::chrono::seconds last_log_secs_;
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
std::tm get_time_(const details::log_msg &msg);
template<typename Padder>
7 years ago
void handle_flag_(char flag, details::padding_info padding);
7 years ago
// Extract given pad spec (e.g. %8X)
// Advance the given it pass the end of the padding spec found (if any)
// Return padding.
7 years ago
details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
7 years ago
7 years ago
void compile_pattern_(const std::string &pattern);
};
} // namespace spdlog
7 years ago
#ifdef SPDLOG_HEADER_ONLY
#include "pattern_formatter-inl.h"
7 years ago
#endif