From 74b59961eda931b7a6e369458396b6e6c45846e4 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sat, 23 Mar 2024 15:40:27 +0800 Subject: [PATCH] feat update --- CMakeLists.txt | 1 + include/sled/log/log.h | 58 ++++++++++++++++++++++++++++++++---------- include/sled/status.h | 2 +- src/log/fmt_test.cc | 20 +++++++++++++++ src/status.cc | 36 +++++++++++++------------- src/status_or_test.cc | 1 + src/status_test.cc | 9 +++++++ 7 files changed, 94 insertions(+), 33 deletions(-) create mode 100644 src/status_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index acd469b..c0c6b59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -129,6 +129,7 @@ if(SLED_BUILD_TESTS) src/strings/base64_test.cc src/synchronization/sequence_checker_test.cc src/cleanup_test.cc + src/status_test.cc src/status_or_test.cc src/system/fiber/fiber_test.cc src/system/thread_pool_test.cc diff --git a/include/sled/log/log.h b/include/sled/log/log.h index dd42204..de34163 100644 --- a/include/sled/log/log.h +++ b/include/sled/log/log.h @@ -10,22 +10,52 @@ #include "sled/system/location.h" #include -#include -#include -#include -#include +// clang-format off #include // support fmt base ostream -#include -#include -#include -#include -template::value, int>::type = 0> -auto -format_as(const T &value) -> int -{ - return static_cast(value); -} +// clang-format on +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include + +namespace { +template +class is_streamable { + template + static auto test(int) -> decltype(std::declval() << std::declval(), std::true_type()); + + template + static auto test(...) -> std::false_type; + +public: + static const bool value = decltype(test(0))::value; +}; + +struct OtherType {}; + +}// namespace + +// template::value, int>::type *> +// auto +// format_as(T const &value) -> decltype(fmt::underlying(value)) +// { +// return fmt::underlying(value); +// } + +// template::value, int>::type *> +// auto +// format_as(T const &value) -> std::string +// { +// std::stringstream ss; +// ss << value; +// return ss.str(); +// } namespace sled { enum class LogLevel { diff --git a/include/sled/status.h b/include/sled/status.h index a157e8f..7e28e6a 100644 --- a/include/sled/status.h +++ b/include/sled/status.h @@ -98,7 +98,7 @@ private: std::unique_ptr impl_; }; -std::ostream &operator<<(std::ostream &os, Status const &status); +std::ostream &operator<<(std::ostream &os, sled::Status const &status); }// namespace sled diff --git a/src/log/fmt_test.cc b/src/log/fmt_test.cc index 39d162b..efb2fc1 100644 --- a/src/log/fmt_test.cc +++ b/src/log/fmt_test.cc @@ -9,6 +9,10 @@ TEST(format, enum) kThree = 3, }; + std::stringstream ss; + ss << kOne; + + EXPECT_EQ(ss.str(), "1"); EXPECT_EQ(fmt::format("{}{}{}", kOne, kTwo, kThree), "123"); } @@ -22,3 +26,19 @@ TEST(format, neg_enum) EXPECT_EQ(fmt::format("{}{}{}", kOne, kTwo, kThree), "-1-2-3"); } + +struct Streamable { + int value; +}; + +std::ostream & +operator<<(std::ostream &os, const Streamable &s) +{ + return os << s.value; +} + +TEST(format, streamable) +{ + Streamable s{42}; + EXPECT_EQ(fmt::format("{}", s), "42"); +} diff --git a/src/status.cc b/src/status.cc index 8186fd9..20a759a 100644 --- a/src/status.cc +++ b/src/status.cc @@ -150,10 +150,25 @@ Status::Equals(Status const &a, Status const &b) return (a.ok() && b.ok()) || (a.impl_ && b.impl_ && *a.impl_ == *b.impl_); } -std::ostream & -operator<<(std::ostream &os, const Status &s) +namespace internal { +void +AddMetadata(ErrorInfo &error_info, std::string const &key, std::string value) { - if (s.ok()) return os << StatusCode::kOk; + error_info.metadata_[key] = std::move(value); +} + +void +SetPayload(Status &s, std::string key, std::string payload) +{ + if (s.impl_) s.impl_->payload()[std::move(key)] = std::move(payload); +} + +}// namespace internal + +std::ostream & +operator<<(std::ostream &os, const sled::Status &s) +{ + if (s.ok()) return os << sled::StatusCode::kOk; os << s.code() << ": " << s.message(); auto const &e = s.error_info(); if (e.reason().empty() && e.domain().empty() && e.metadata().empty()) { return os; } @@ -182,19 +197,4 @@ operator<<(std::ostream &os, const Status &s) return os << "}"; } -namespace internal { -void -AddMetadata(ErrorInfo &error_info, std::string const &key, std::string value) -{ - error_info.metadata_[key] = std::move(value); -} - -void -SetPayload(Status &s, std::string key, std::string payload) -{ - if (s.impl_) s.impl_->payload()[std::move(key)] = std::move(payload); -} - -}// namespace internal - }// namespace sled diff --git a/src/status_or_test.cc b/src/status_or_test.cc index e117c1e..9f6cb5c 100644 --- a/src/status_or_test.cc +++ b/src/status_or_test.cc @@ -1,4 +1,5 @@ #include +#include #include TEST(StatusOr, TestStatusOr) diff --git a/src/status_test.cc b/src/status_test.cc new file mode 100644 index 0000000..4062fb5 --- /dev/null +++ b/src/status_test.cc @@ -0,0 +1,9 @@ +#include +#include +#include + +TEST(Status, format) +{ + auto status = sled::Status(sled::StatusCode::kOk, ""); + EXPECT_EQ(fmt::format("{}", status), ""); +}