|
|
|
@ -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
|
|
|
|
|