From eaa11e940a958549300dc75d9fa72db78a4c8ef8 Mon Sep 17 00:00:00 2001 From: eudoxos Date: Tue, 12 Nov 2019 07:23:09 +0100 Subject: [PATCH] Preliminary attempt at right_clip alignment --- example/example.cpp | 5 ++-- .../spdlog/details/pattern_formatter-inl.h | 26 ++++++++++++++++--- include/spdlog/details/pattern_formatter.h | 3 ++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 0bc838bd..6e3068af 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -36,8 +36,9 @@ int main(int, char *[]) spdlog::debug("This message should be displayed.."); // Customize msg format for all loggers - spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v"); - spdlog::info("This an info message with custom format"); + spdlog::set_pattern("[%H:%M:%S %z] %>4B [%^%>3l%$] [thread %t] %>30v"); + spdlog::debug("This an info message with custom format"); + spdlog::info("Nicely aligned with the previous message"); spdlog::set_pattern("%+"); // back to default format spdlog::set_level(spdlog::level::info); diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 8bee8a51..e7e5f936 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -42,7 +42,14 @@ public: if (padinfo_.width_ <= wrapped_size) { - total_pad_ = 0; + if (padinfo_.side_ == padding_info::clip_right) + { + total_pad_ = (long)padinfo_.width_ - (long)wrapped_size; + } + else + { + total_pad_ = 0; + } return; } @@ -70,16 +77,23 @@ public: } private: - void pad_it(size_t count) + void pad_it(long count) { // count = std::min(count, spaces_.size()); assert(count <= spaces_.size()); - fmt_helper::append_string_view(string_view_t(spaces_.data(), count), dest_); + if ( count >= 0 ) + { + fmt_helper::append_string_view(string_view_t(spaces_.data(), count), dest_); + } + else + { + dest_.resize(dest_.size()+count); + } } const padding_info &padinfo_; memory_buf_t &dest_; - size_t total_pad_; + long total_pad_; string_view_t spaces_{" ", 64}; }; @@ -1233,6 +1247,10 @@ SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::stri side = padding_info::center; ++it; break; + case '>': + side = padding_info::clip_right; + ++it; + break; default: side = details::padding_info::left; break; diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index b03d3394..41bc7d34 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -25,7 +25,8 @@ struct padding_info { left, right, - center + center, + clip_right, }; padding_info() = default;