mirror of https://github.com/gabime/spdlog.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
4.8 KiB
C++
168 lines
4.8 KiB
C++
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
#pragma once
|
|
|
|
#include <spdlog/common.h>
|
|
#include <spdlog/details/log_msg.h>
|
|
#include <spdlog/details/os.h>
|
|
#include <spdlog/formatter.h>
|
|
|
|
#include <chrono>
|
|
#include <ctime>
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <unordered_map>
|
|
|
|
namespace spdlog {
|
|
namespace details {
|
|
|
|
// padding information.
|
|
struct padding_info
|
|
{
|
|
enum class pad_side
|
|
{
|
|
left,
|
|
right,
|
|
center
|
|
};
|
|
|
|
padding_info() = default;
|
|
padding_info(size_t width, padding_info::pad_side side, bool truncate)
|
|
: width_(width)
|
|
, side_(side)
|
|
, truncate_(truncate)
|
|
, enabled_(true)
|
|
{}
|
|
|
|
bool enabled() const
|
|
{
|
|
return enabled_;
|
|
}
|
|
size_t width_ = 0;
|
|
pad_side side_ = pad_side::left;
|
|
bool truncate_ = false;
|
|
bool enabled_ = false;
|
|
};
|
|
|
|
#if !defined(_WIN32) && defined(SPDLOG_EXTENDED_STLYING)
|
|
|
|
class SPDLOG_API flag_formatter
|
|
{
|
|
public:
|
|
explicit flag_formatter(padding_info padinfo)
|
|
: padinfo_(padinfo)
|
|
{}
|
|
explicit flag_formatter(padding_info padinfo, styling_info style_info)
|
|
: padinfo_(padinfo)
|
|
, styleinfo_(style_info)
|
|
{}
|
|
flag_formatter() = default;
|
|
virtual ~flag_formatter() = default;
|
|
virtual void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) = 0;
|
|
|
|
protected:
|
|
padding_info padinfo_;
|
|
styling_info styleinfo_;
|
|
};
|
|
|
|
static const details::style_strings style_type_table
|
|
{
|
|
"null_style",
|
|
"reset", "bold", "dark", "underline", "blink", "reverse",
|
|
"fg_black", "fg_red", "fg_green", "fg_yellow", "fg_blue", "fg_magenta", "fg_cyan", "fg_white", "fg_default",
|
|
"bg_black", "bg_red", "bg_green", "bg_yellow", "bg_blue", "bg_magenta", "bg_cyan", "bg_white", "bg_default"
|
|
};
|
|
|
|
#else
|
|
|
|
class SPDLOG_API 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, memory_buf_t &dest) = 0;
|
|
|
|
protected:
|
|
padding_info padinfo_;
|
|
};
|
|
|
|
#endif
|
|
|
|
} // namespace details
|
|
|
|
class SPDLOG_API custom_flag_formatter : public details::flag_formatter
|
|
{
|
|
public:
|
|
virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
|
|
|
|
void set_padding_info(const details::padding_info &padding)
|
|
{
|
|
flag_formatter::padinfo_ = padding;
|
|
}
|
|
};
|
|
|
|
class SPDLOG_API pattern_formatter final : public formatter
|
|
{
|
|
public:
|
|
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,
|
|
std::string eol = spdlog::details::os::default_eol, custom_flags custom_user_flags = custom_flags());
|
|
|
|
// use default pattern is not given
|
|
explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
|
|
|
|
pattern_formatter(const pattern_formatter &other) = delete;
|
|
pattern_formatter &operator=(const pattern_formatter &other) = delete;
|
|
|
|
std::unique_ptr<formatter> clone() const override;
|
|
void format(const details::log_msg &msg, memory_buf_t &dest) override;
|
|
|
|
template<typename T, typename... Args>
|
|
pattern_formatter &add_flag(char flag, Args &&...args)
|
|
{
|
|
custom_handlers_[flag] = details::make_unique<T>(std::forward<Args>(args)...);
|
|
return *this;
|
|
}
|
|
void set_pattern(std::string pattern);
|
|
void need_localtime(bool need = true);
|
|
|
|
private:
|
|
std::string pattern_;
|
|
std::string eol_;
|
|
pattern_time_type pattern_time_type_;
|
|
bool need_localtime_;
|
|
std::tm cached_tm_;
|
|
std::chrono::seconds last_log_secs_;
|
|
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
|
|
custom_flags custom_handlers_;
|
|
|
|
std::tm get_time_(const details::log_msg &msg);
|
|
template<typename Padder>
|
|
void handle_flag_(char flag, details::padding_info padding, details::styling_info styling);
|
|
|
|
// Extract given pad spec (e.g. %8X)
|
|
// Advance the given it pass the end of the padding spec found (if any)
|
|
// Return padding.
|
|
static details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
|
|
|
|
#if !defined(_WIN32) && defined(SPDLOG_EXTENDED_STLYING)
|
|
// Extract given style spec (e.g. %{style}^X, %{style;style}^X, etc...)
|
|
// Advance the given it pass the end of the style spec found (if any)
|
|
// Return style.
|
|
static details::styling_info handle_stylespec_(std::string::const_iterator &it, std::string::const_iterator end);
|
|
#endif
|
|
void compile_pattern_(const std::string &pattern);
|
|
};
|
|
} // namespace spdlog
|
|
|
|
#ifdef SPDLOG_HEADER_ONLY
|
|
# include "pattern_formatter-inl.h"
|
|
#endif
|