From 9cf86f592df18384d16ebf1e59b3ded9444358de Mon Sep 17 00:00:00 2001 From: Bailey Chittle Date: Thu, 22 Dec 2022 13:48:53 -0500 Subject: [PATCH] fixed bugs with default formatting --- example/example.cpp | 4 +- include/spdlog/details/attr_composer.h | 133 ------------------------- include/spdlog/pattern_formatter-inl.h | 21 ++-- 3 files changed, 8 insertions(+), 150 deletions(-) delete mode 100644 include/spdlog/details/attr_composer.h diff --git a/example/example.cpp b/example/example.cpp index 4df2b4cd..6a9b1bac 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -394,14 +394,14 @@ void replace_default_logger_example() } void attribute_example() { - // spdlog::default_logger_raw()->log(spdlog::level::warn, "EXPERIMENTAL: log with attributes", {"attribute_key", "attribute value"}); + spdlog::default_logger_raw()->log(spdlog::level::warn, "EXPERIMENTAL: log with attributes", {{"attribute_key", "attribute value"}}); // logfmt structured logging using attributes // auto logfmt_logger = spdlog::basic_logger_mt("logfmt_logger", "logs/mylog.txt"); auto logfmt_logger = spdlog::default_logger_raw(); - std::string logfmt_pattern = "time=%Y-%m-%dT%H:%M:%S.%f%z, name=%n, level=%^%l%$, process=%P, thread=%t, message=\"%v\""; + std::string logfmt_pattern = "time=%Y-%m-%dT%H:%M:%S.%f%z, name=%n, level=%^%l%$, process=%P, thread=%t, message=\"%v\", "; logfmt_logger->set_pattern(logfmt_pattern); logfmt_logger->log(spdlog::level::info, "logfmt structured logging", {{"key1", "value1"}, {"key2", "value2"}}); diff --git a/include/spdlog/details/attr_composer.h b/include/spdlog/details/attr_composer.h deleted file mode 100644 index fff26b12..00000000 --- a/include/spdlog/details/attr_composer.h +++ /dev/null @@ -1,133 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace spdlog { -namespace detail { - -template -requires std::integral -auto to_string(T const& v, T const& base) -> std::string -{ - // make sure we have enough to cover: negative base 2 - static constexpr std::size_t nbytes = sizeof(T) * 8 + 1 * std::is_signed_v(T); - std::array buf; - auto [p, ec] = std::to_chars(buf.data(), buf.data() + buf.size(), v); - return std::string(buf.data(), std::size_t(p - buf.data())); -} - -inline void scramble(std::string& dst, std::string_view s) -{ - if (s.empty()) - return; - - auto start = s.data(); - auto const end = s.data() + s.size(); - auto cursor = start; - - dst.reserve(dst.size() + s.size()); - - auto replace = [&](std::string_view with) { - dst.append(start, size_t(cursor - start)); - ++cursor; - start = cursor; - dst.append(with); - }; - - while (cursor != end) { - auto c = static_cast(*cursor); - - switch (c) { - case '\b': - replace("\\b"); - break; - case '\f': - replace("\\f"); - break; - case '\n': - replace("\\n"); - break; - case '\r': - replace("\\r"); - break; - case '\t': - replace("\\t"); - break; - case '\\': - replace("\\\\"); - break; - case '"': - replace("\\\""); - break; - default: - if (c <= '\x0f') { - char buf[] = "\\u0000"; - buf[5] += c; - if (c >= '\x0a') - buf[5] += 'a' - ':'; - replace(buf); - } - else if (c <= '\x1f' || c == 0x7f) { - char buf[] = "\\u0010"; - buf[5] += c - 16; - if (c >= '\x1a') - buf[5] += 'a' - ':'; - replace(buf); - } - else - ++cursor; - } - } - if (cursor != start) - dst.append({start, size_t(cursor - start)}); -} - -inline auto compose_prefix(std::string_view k, std::size_t value_bytes) -> std::string -{ - auto dst = std::string{}; - dst.reserve(6 + k.size() + value_bytes); - dst += ','; - dst += '"'; - dst += k; - dst += '"'; - dst += ':'; - dst += '"'; -} - -auto compose(std::string_view k, std::string_view const& v) -> std::string -{ - auto dst = compose_prefix(k, v.size()); - scramble(dst, std::string_view{v}); - dst += '"'; - return dst; -} -inline auto compose(std::string_view k, bool v) -> std::string -{ - auto dst = compose_prefix(k, 4 + !v); - dst += v ? "true\"" : "false\""; - return dst; -} - -template - requires(std::integral || std::floating_point) -auto compose(std::string_view k, T const& v) -> std::string -{ - auto dst = compose_prefix(k, 1); - dst += std::to_string(v); - dst += '"'; - return dst; -} - -template - requires std::integral -auto compose(std::string_view k, T const& v, int base) -> std::string -{ - auto dst = compose_prefix(k, 1); - dst += to_string(v, base); - dst += '"'; - return dst; -} -} -} \ No newline at end of file diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 0fbd6d86..b2edcb93 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1012,13 +1012,7 @@ public: fmt_helper::append_string_view(msg.payload, dest); if (msg.attributes.size() > 0) { - fmt_helper::append_string_view(" | ", dest); - for (const details::attr& a : msg.attributes) { - fmt_helper::append_string_view(a.key, dest); - fmt_helper::append_string_view("=\"", dest); - fmt_helper::append_string_view(a.value, dest); - fmt_helper::append_string_view("\", ", dest); - } + fmt_helper::append_string_view(" | ", dest); // separate message from attributes } } @@ -1087,14 +1081,11 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory f->format(msg, cached_tm_, dest); } // TODO: make separate function, and add a custom format to attributes (for now just using logfmt) - if (msg.attributes.size() > 0) { - details::fmt_helper::append_string_view(", ", dest); - for (const details::attr& a : msg.attributes) { - details::fmt_helper::append_string_view(a.key, dest); - details::fmt_helper::append_string_view("=\"", dest); - details::fmt_helper::append_string_view(a.value, dest); - details::fmt_helper::append_string_view("\", ", dest); - } + for (const details::attr& a : msg.attributes) { + details::fmt_helper::append_string_view(a.key, dest); + details::fmt_helper::append_string_view("=\"", dest); + details::fmt_helper::append_string_view(a.value, dest); + details::fmt_helper::append_string_view("\", ", dest); } // write eol