spdlog/include/spdlog/details/pattern_formatter.h

99 lines
2.5 KiB
C
Raw Normal View History

2019-05-11 13:06:17 -04:00
// Copyright(c) 2015-present Gabi Melman & spdlog contributors.
2016-08-25 17:38:08 -04:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
2019-05-11 13:06:17 -04:00
2019-04-05 16:05:46 -04:00
#include "spdlog/common.h"
2018-04-28 18:43:42 -04:00
#include "spdlog/details/log_msg.h"
#include "spdlog/details/os.h"
#include "spdlog/formatter.h"
2016-08-25 17:38:08 -04:00
#include <chrono>
#include <ctime>
#include <memory>
2019-04-05 16:05:46 -04:00
2016-08-25 17:38:08 -04:00
#include <string>
#include <vector>
2018-03-17 06:47:46 -04:00
namespace spdlog {
namespace details {
2018-06-12 11:48:22 -04:00
2018-11-10 11:03:11 -05:00
// padding information.
2018-11-09 08:18:45 -05:00
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;
}
2018-11-09 08:18:45 -05:00
const size_t width_ = 0;
const pad_side side_ = left;
};
2016-08-25 17:38:08 -04:00
class flag_formatter
{
public:
2018-11-09 08:18:45 -05:00
explicit flag_formatter(padding_info padinfo)
: padinfo_(padinfo)
{}
2018-11-09 08:18:45 -05:00
flag_formatter() = default;
2018-02-24 19:25:15 -05:00
virtual ~flag_formatter() = default;
2018-06-23 18:32:39 -04:00
virtual void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) = 0;
2018-11-09 08:18:45 -05:00
protected:
padding_info padinfo_;
2016-08-25 17:38:08 -04:00
};
2019-04-26 19:34:50 -04:00
} // namespace details
2016-08-25 17:38:08 -04:00
2018-09-26 07:33:37 -04:00
class pattern_formatter final : public formatter
2016-08-25 17:38:08 -04:00
{
2018-06-25 18:13:02 -04:00
public:
2018-07-22 17:13:52 -04:00
explicit pattern_formatter(
2019-04-26 19:34:50 -04:00
std::string pattern, pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
2019-04-05 16:05:46 -04:00
// use default pattern is not given
2019-04-26 19:34:50 -04:00
explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
2018-07-22 17:13:52 -04:00
pattern_formatter(const pattern_formatter &other) = delete;
pattern_formatter &operator=(const pattern_formatter &other) = delete;
2019-04-05 16:05:46 -04:00
std::unique_ptr<formatter> clone() const override;
2019-04-26 19:34:50 -04:00
void format(const details::log_msg &msg, fmt::memory_buffer &dest) override;
2016-08-25 17:38:08 -04:00
2018-06-25 18:13:02 -04:00
private:
2018-07-22 17:13:52 -04:00
std::string pattern_;
2018-07-22 14:52:46 -04:00
std::string eol_;
pattern_time_type pattern_time_type_;
2018-06-25 19:00:33 -04:00
std::tm cached_tm_;
2018-06-25 18:13:02 -04:00
std::chrono::seconds last_log_secs_;
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
std::tm get_time_(const details::log_msg &msg);
2019-04-05 16:05:46 -04:00
void handle_flag_(char flag, details::padding_info padding);
2019-04-26 19:34:50 -04:00
2018-11-09 08:18:45 -05:00
// Extract given pad spec (e.g. %8X)
// Advance the given it pass the end of the padding spec found (if any)
// Return padding.
2019-04-05 16:05:46 -04:00
details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
2019-04-26 19:34:50 -04:00
2019-04-05 16:05:46 -04:00
void compile_pattern_(const std::string &pattern);
2018-06-25 18:13:02 -04:00
};
} // namespace spdlog
2019-04-05 09:57:49 -04:00
#ifdef SPDLOG_HEADER_ONLY
2019-05-11 06:19:53 -04:00
#include "pattern_formatter-inl.h"
2019-04-05 09:57:49 -04:00
#endif