Pattern formatter optimizations
This commit is contained in:
		@@ -26,17 +26,13 @@ inline void append_buf(const fmt::memory_buffer &buf, fmt::memory_buffer &dest)
 | 
			
		||||
    dest.append(buf_ptr, buf_ptr + buf.size());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void append_int(int n, fmt::memory_buffer &dest)
 | 
			
		||||
template<typename T>
 | 
			
		||||
inline void append_int(T n, fmt::memory_buffer &dest)
 | 
			
		||||
{
 | 
			
		||||
    fmt::format_int i(n);
 | 
			
		||||
    dest.append(i.data(), i.data() + i.size());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void append_size_t(size_t n, fmt::memory_buffer &dest)
 | 
			
		||||
{
 | 
			
		||||
    fmt::format_int i(n);
 | 
			
		||||
    dest.append(i.data(), i.data() + i.size());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void append_and_pad2(int n, fmt::memory_buffer &dest)
 | 
			
		||||
{
 | 
			
		||||
@@ -65,11 +61,16 @@ inline void append_and_pad3(int n, fmt::memory_buffer &dest)
 | 
			
		||||
    if (n > 9) // 10-99
 | 
			
		||||
    {
 | 
			
		||||
        dest.push_back('0');		
 | 
			
		||||
		dest.push_back('0' + n / 10);
 | 
			
		||||
		dest.push_back('0' + n % 10);		
 | 
			
		||||
		return;
 | 
			
		||||
    }
 | 
			
		||||
    else if (n >= 0)
 | 
			
		||||
    {
 | 
			
		||||
        dest.push_back('0');
 | 
			
		||||
        dest.push_back('0');
 | 
			
		||||
		dest.push_back('0' + n);
 | 
			
		||||
		return;
 | 
			
		||||
    }
 | 
			
		||||
    // negatives (unlikely, but just in case let fmt deal with it)
 | 
			
		||||
    else
 | 
			
		||||
@@ -77,7 +78,7 @@ inline void append_and_pad3(int n, fmt::memory_buffer &dest)
 | 
			
		||||
        fmt::format_to(dest, "{:03}", n);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    append_int(n, dest);
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void append_and_pad6(int n, fmt::memory_buffer &dest)
 | 
			
		||||
 
 | 
			
		||||
@@ -460,13 +460,12 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
 | 
			
		||||
    void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) override
 | 
			
		||||
    {
 | 
			
		||||
#ifndef SPDLOG_NO_DATETIME
 | 
			
		||||
        auto duration = msg.time.time_since_epoch();
 | 
			
		||||
 | 
			
		||||
        // each second cache the header
 | 
			
		||||
        auto duration = msg.time.time_since_epoch();
 | 
			
		||||
        auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
 | 
			
		||||
        if (cached_header_.size() == 0 || cached_seconds_ts_ != seconds)
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            cached_header_ = std::move(fmt::memory_buffer());
 | 
			
		||||
            cached_header_.push_back('[');
 | 
			
		||||
            fmt_helper::append_int(tm_time.tm_year + 1900, cached_header_);
 | 
			
		||||
@@ -491,19 +490,10 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
 | 
			
		||||
        }
 | 
			
		||||
        fmt_helper::append_buf(cached_header_, dest);
 | 
			
		||||
 | 
			
		||||
        //
 | 
			
		||||
        auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
 | 
			
		||||
 | 
			
		||||
        if (cached_millis_.size() == 0 || millis != cached_millis_ts_)
 | 
			
		||||
        {
 | 
			
		||||
            cached_millis_ = std::move(fmt::memory_buffer());
 | 
			
		||||
            fmt_helper::append_and_pad3(static_cast<int>(millis), cached_millis_);
 | 
			
		||||
            cached_millis_.push_back(']');
 | 
			
		||||
            cached_millis_.push_back(' ');
 | 
			
		||||
            cached_millis_ts_ = millis;
 | 
			
		||||
        }
 | 
			
		||||
        fmt_helper::append_buf(cached_millis_, dest);
 | 
			
		||||
 | 
			
		||||
        fmt_helper::append_and_pad3(static_cast<int>(millis), dest);
 | 
			
		||||
        dest.push_back(']');
 | 
			
		||||
        dest.push_back(' ');
 | 
			
		||||
#else // no datetime needed
 | 
			
		||||
        (void)tm_time;
 | 
			
		||||
#endif
 | 
			
		||||
@@ -528,61 +518,59 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    std::chrono::seconds::rep cached_seconds_ts_{0};
 | 
			
		||||
    std::chrono::milliseconds::rep cached_millis_ts_{0};
 | 
			
		||||
    fmt::memory_buffer cached_header_;
 | 
			
		||||
    fmt::memory_buffer cached_millis_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace details
 | 
			
		||||
} // namespace spdlog
 | 
			
		||||
///////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
// pattern_formatter inline impl
 | 
			
		||||
///////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
inline spdlog::pattern_formatter::pattern_formatter(const std::string &pattern, pattern_time_type pattern_time, std::string eol)
 | 
			
		||||
 | 
			
		||||
class pattern_formatter SPDLOG_FINAL : public formatter
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    explicit pattern_formatter(const std::string &pattern, pattern_time_type pattern_time = pattern_time_type::local,
 | 
			
		||||
        std::string eol = spdlog::details::os::default_eol)
 | 
			
		||||
        : eol_(std::move(eol))
 | 
			
		||||
        , pattern_time_(pattern_time)
 | 
			
		||||
{
 | 
			
		||||
    {
 | 
			
		||||
        compile_pattern(pattern);
 | 
			
		||||
}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
inline void spdlog::pattern_formatter::compile_pattern(const std::string &pattern)
 | 
			
		||||
{
 | 
			
		||||
    auto end = pattern.end();
 | 
			
		||||
    std::unique_ptr<details::aggregate_formatter> user_chars;
 | 
			
		||||
    for (auto it = pattern.begin(); it != end; ++it)
 | 
			
		||||
    pattern_formatter(const pattern_formatter &) = default;
 | 
			
		||||
    pattern_formatter &operator=(const pattern_formatter &) = default;
 | 
			
		||||
    void format(const details::log_msg &msg, fmt::memory_buffer &dest) override
 | 
			
		||||
    {
 | 
			
		||||
        if (*it == '%')
 | 
			
		||||
#ifndef SPDLOG_NO_DATETIME
 | 
			
		||||
        auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
 | 
			
		||||
        if (secs != last_log_secs_)
 | 
			
		||||
        {
 | 
			
		||||
            if (user_chars) // append user chars found so far
 | 
			
		||||
            cached_tm_ = get_time(msg);
 | 
			
		||||
            last_log_secs_ = secs;
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
        for (auto &f : formatters_)
 | 
			
		||||
        {
 | 
			
		||||
                formatters_.push_back(std::move(user_chars));
 | 
			
		||||
            f->format(msg, cached_tm_, dest);
 | 
			
		||||
        }
 | 
			
		||||
            // if(
 | 
			
		||||
            if (++it != end)
 | 
			
		||||
        // write eol
 | 
			
		||||
        details::fmt_helper::append_str(eol_, dest);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    const std::string eol_;
 | 
			
		||||
    const pattern_time_type pattern_time_;
 | 
			
		||||
    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)
 | 
			
		||||
    {
 | 
			
		||||
                handle_flag(*it);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
        if (pattern_time_ == pattern_time_type::local)
 | 
			
		||||
        {
 | 
			
		||||
                break;
 | 
			
		||||
            return details::os::localtime(log_clock::to_time_t(msg.time));
 | 
			
		||||
        }
 | 
			
		||||
        return details::os::gmtime(log_clock::to_time_t(msg.time));
 | 
			
		||||
    }
 | 
			
		||||
        else // chars not following the % sign should be displayed as is
 | 
			
		||||
    void handle_flag(char flag)
 | 
			
		||||
    {
 | 
			
		||||
            if (!user_chars)
 | 
			
		||||
            {
 | 
			
		||||
                user_chars = std::unique_ptr<details::aggregate_formatter>(new details::aggregate_formatter());
 | 
			
		||||
            }
 | 
			
		||||
            user_chars->add_ch(*it);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (user_chars) // append raw chars found so far
 | 
			
		||||
    {
 | 
			
		||||
        formatters_.push_back(std::move(user_chars));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
inline void spdlog::pattern_formatter::handle_flag(char flag)
 | 
			
		||||
{
 | 
			
		||||
        switch (flag)
 | 
			
		||||
        {
 | 
			
		||||
            // logger name
 | 
			
		||||
@@ -636,7 +624,6 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
 | 
			
		||||
 | 
			
		||||
        case ('D'):
 | 
			
		||||
        case ('x'):
 | 
			
		||||
 | 
			
		||||
            formatters_.emplace_back(new details::D_formatter());
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
@@ -725,29 +712,43 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
 | 
			
		||||
            formatters_.emplace_back(new details::ch_formatter(flag));
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline std::tm spdlog::pattern_formatter::get_time(const details::log_msg &msg)
 | 
			
		||||
{
 | 
			
		||||
    if (pattern_time_ == pattern_time_type::local)
 | 
			
		||||
    {
 | 
			
		||||
        return details::os::localtime(log_clock::to_time_t(msg.time));
 | 
			
		||||
    }
 | 
			
		||||
    return details::os::gmtime(log_clock::to_time_t(msg.time));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void spdlog::pattern_formatter::format(const details::log_msg &msg, fmt::memory_buffer &dest)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
#ifndef SPDLOG_NO_DATETIME
 | 
			
		||||
    auto tm_time = get_time(msg);
 | 
			
		||||
#else
 | 
			
		||||
    std::tm tm_time;
 | 
			
		||||
#endif
 | 
			
		||||
    for (auto &f : formatters_)
 | 
			
		||||
    void compile_pattern(const std::string &pattern)
 | 
			
		||||
    {
 | 
			
		||||
        f->format(msg, tm_time, dest);
 | 
			
		||||
        auto end = pattern.end();
 | 
			
		||||
        std::unique_ptr<details::aggregate_formatter> user_chars;
 | 
			
		||||
        for (auto it = pattern.begin(); it != end; ++it)
 | 
			
		||||
        {
 | 
			
		||||
            if (*it == '%')
 | 
			
		||||
            {
 | 
			
		||||
                if (user_chars) // append user chars found so far
 | 
			
		||||
                {
 | 
			
		||||
                    formatters_.push_back(std::move(user_chars));
 | 
			
		||||
                }
 | 
			
		||||
    // write eol
 | 
			
		||||
    details::fmt_helper::append_str(eol_, dest);
 | 
			
		||||
}
 | 
			
		||||
                // if(
 | 
			
		||||
                if (++it != end)
 | 
			
		||||
                {
 | 
			
		||||
                    handle_flag(*it);
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else // chars not following the % sign should be displayed as is
 | 
			
		||||
            {
 | 
			
		||||
                if (!user_chars)
 | 
			
		||||
                {
 | 
			
		||||
                    user_chars = std::unique_ptr<details::aggregate_formatter>(new details::aggregate_formatter());
 | 
			
		||||
                }
 | 
			
		||||
                user_chars->add_ch(*it);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (user_chars) // append raw chars found so far
 | 
			
		||||
        {
 | 
			
		||||
            formatters_.push_back(std::move(user_chars));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
} // namespace spdlog
 | 
			
		||||
 
 | 
			
		||||
@@ -20,28 +20,6 @@ public:
 | 
			
		||||
    virtual ~formatter() = default;
 | 
			
		||||
    virtual void format(const details::log_msg &msg, fmt::memory_buffer &dest) = 0;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
namespace details {
 | 
			
		||||
class flag_formatter;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class pattern_formatter SPDLOG_FINAL : public formatter
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    explicit pattern_formatter(const std::string &pattern, pattern_time_type pattern_time = pattern_time_type::local,
 | 
			
		||||
        std::string eol = spdlog::details::os::default_eol);
 | 
			
		||||
    pattern_formatter(const pattern_formatter &) = default;
 | 
			
		||||
    pattern_formatter &operator=(const pattern_formatter &) = default;
 | 
			
		||||
    void format(const details::log_msg &msg, fmt::memory_buffer &dest) override;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    const std::string eol_;
 | 
			
		||||
    const pattern_time_type pattern_time_;
 | 
			
		||||
    std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
 | 
			
		||||
    std::tm get_time(const details::log_msg &msg);
 | 
			
		||||
    void handle_flag(char flag);
 | 
			
		||||
    void compile_pattern(const std::string &pattern);
 | 
			
		||||
};
 | 
			
		||||
} // namespace spdlog
 | 
			
		||||
 | 
			
		||||
#include "details/pattern_formatter_impl.h"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user