default_formatter class

pull/2143/head
Your Full Name 4 years ago committed by gabime
parent 8826011c81
commit 8bbf72b661

@ -10,7 +10,15 @@
void bench_formatter(benchmark::State &state, std::string pattern)
{
auto formatter = spdlog::details::make_unique<spdlog::pattern_formatter>(pattern);
std::unique_ptr<spdlog::formatter> formatter;
if(pattern == "%+")
{
formatter = spdlog::details::make_unique<spdlog::default_formatter>();
}
else {
formatter = spdlog::details::make_unique<spdlog::pattern_formatter>(pattern);
}
spdlog::memory_buf_t dest;
std::string logger_name = "logger-name";
const char *text = "Hello. This is some message with length of 80 ";

@ -31,7 +31,7 @@ namespace spdlog {
namespace details {
SPDLOG_INLINE registry::registry()
: formatter_(new pattern_formatter())
: formatter_(new default_formatter())
{
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER

@ -6,6 +6,7 @@
#include <spdlog/common.h>
#include <spdlog/details/log_msg.h>
#include <spdlog/details/os.h>
#include <spdlog/details/fmt_helper.h>
#include <spdlog/formatter.h>
#include <chrono>
@ -119,6 +120,110 @@ private:
void compile_pattern_(const std::string &pattern);
};
// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
class default_formatter final : public formatter
{
public:
default_formatter() = default;
~default_formatter() = default;
std::unique_ptr<formatter> clone() const override
{
return details::make_unique<default_formatter>();
}
void format(const details::log_msg &msg, memory_buf_t &dest) override
{
// 30ns
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::seconds;
// cache the date/time part for the next second.
auto duration = msg.time.time_since_epoch();
auto secs = duration_cast<seconds>(duration);
//[2021-08-23 00:57:12.310] [info] Welcome to spdlog version 1.9.2 !
if (cache_timestamp_ != secs || cached_datetime_.size() == 0)
{
auto tm_time = details::os::localtime(log_clock::to_time_t(msg.time));
cached_datetime_.clear();
cached_datetime_.push_back('[');
details::fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_);
cached_datetime_.push_back('-');
details::fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_);
cached_datetime_.push_back('-');
details::fmt_helper::pad2(tm_time.tm_mday, cached_datetime_);
cached_datetime_.push_back(' ');
details::fmt_helper::pad2(tm_time.tm_hour, cached_datetime_);
cached_datetime_.push_back(':');
details::fmt_helper::pad2(tm_time.tm_min, cached_datetime_);
cached_datetime_.push_back(':');
details::fmt_helper::pad2(tm_time.tm_sec, cached_datetime_);
cached_datetime_.push_back('.');
cache_timestamp_ = secs;
}
// 32ns
dest.append(cached_datetime_.begin(), cached_datetime_.end());
// 36ns
auto millis = details::fmt_helper::time_fraction<milliseconds>(msg.time);
// 40ns
details::fmt_helper::pad3(static_cast<uint32_t>(millis.count()), dest);
// 45ns
dest.push_back(']');
dest.push_back(' ');
// 49ns
// append logger name if exists
if (msg.logger_name.size() > 0)
{
dest.push_back('[');
details::fmt_helper::append_string_view(msg.logger_name, dest);
dest.push_back(']');
dest.push_back(' ');
}
// 56ns
dest.push_back('[');
// wrap the level name with color
msg.color_range_start = dest.size();
// fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
details::fmt_helper::append_string_view(level::to_string_view(msg.level), dest);
msg.color_range_end = dest.size();
dest.push_back(']');
dest.push_back(' ');
// add source location if present
if (!msg.source.empty())
{
dest.push_back('[');
// const char *filename = details::short_filename_formatter<details::null_scoped_padder>::basename(msg.source.filename);
// details::fmt_helper::append_string_view(filename, dest);
dest.push_back(':');
details::fmt_helper::append_int(msg.source.line, dest);
dest.push_back(']');
dest.push_back(' ');
}
details::fmt_helper::append_string_view(msg.payload, dest);
details::fmt_helper::append_string_view(details::os::default_eol, dest);
}
private:
std::chrono::seconds cache_timestamp_{0};
memory_buf_t cached_datetime_;
};
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY

@ -17,7 +17,7 @@ template<typename ConsoleMutex>
SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
: target_file_(target_file)
, mutex_(ConsoleMutex::mutex())
, formatter_(details::make_unique<spdlog::pattern_formatter>())
, formatter_(details::make_unique<spdlog::default_formatter>())
{
set_color_mode(mode);

@ -14,7 +14,7 @@
template<typename Mutex>
SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::base_sink()
: formatter_{details::make_unique<spdlog::pattern_formatter>()}
: formatter_{details::make_unique<spdlog::default_formatter>()}
{}
template<typename Mutex>

Loading…
Cancel
Save