Merge branch 'master' of https://code.uocat.com/tqcq/sled
This commit is contained in:
commit
4ee0adf822
@ -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
|
||||
|
@ -10,22 +10,52 @@
|
||||
|
||||
#include "sled/system/location.h"
|
||||
#include <assert.h>
|
||||
#include <fmt/chrono.h>
|
||||
#include <fmt/color.h>
|
||||
#include <fmt/compile.h>
|
||||
#include <fmt/format.h>
|
||||
// clang-format off
|
||||
#include <fmt/ostream.h>// support fmt base ostream
|
||||
#include <fmt/printf.h>
|
||||
#include <fmt/ranges.h>
|
||||
#include <fmt/std.h>
|
||||
#include <fmt/xchar.h>
|
||||
|
||||
template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
|
||||
auto
|
||||
format_as(const T &value) -> int
|
||||
{
|
||||
return static_cast<int>(value);
|
||||
}
|
||||
// clang-format on
|
||||
// #include <fmt/chrono.h>
|
||||
// #include <fmt/color.h>
|
||||
// #include <fmt/compile.h>
|
||||
// #include <fmt/format.h>
|
||||
// #include <fmt/printf.h>
|
||||
// #include <fmt/ranges.h>
|
||||
// #include <fmt/std.h>
|
||||
// #include <fmt/xchar.h>
|
||||
// #include <sstream>
|
||||
|
||||
namespace {
|
||||
template<typename S, typename T>
|
||||
class is_streamable {
|
||||
template<typename SS, typename TT>
|
||||
static auto test(int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
|
||||
|
||||
template<typename, typename>
|
||||
static auto test(...) -> std::false_type;
|
||||
|
||||
public:
|
||||
static const bool value = decltype(test<S, T>(0))::value;
|
||||
};
|
||||
|
||||
struct OtherType {};
|
||||
|
||||
}// namespace
|
||||
|
||||
// template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type *>
|
||||
// auto
|
||||
// format_as(T const &value) -> decltype(fmt::underlying(value))
|
||||
// {
|
||||
// return fmt::underlying(value);
|
||||
// }
|
||||
|
||||
// template<typename T, typename std::enable_if<is_streamable<std::ostream, T>::value, int>::type *>
|
||||
// auto
|
||||
// format_as(T const &value) -> std::string
|
||||
// {
|
||||
// std::stringstream ss;
|
||||
// ss << value;
|
||||
// return ss.str();
|
||||
// }
|
||||
|
||||
namespace sled {
|
||||
enum class LogLevel {
|
||||
|
@ -98,7 +98,7 @@ private:
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, Status const &status);
|
||||
std::ostream &operator<<(std::ostream &os, sled::Status const &status);
|
||||
|
||||
}// namespace sled
|
||||
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <sled/log/log.h>
|
||||
#include <sled/status_or.h>
|
||||
|
||||
TEST(StatusOr, TestStatusOr)
|
||||
|
9
src/status_test.cc
Normal file
9
src/status_test.cc
Normal file
@ -0,0 +1,9 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <sled/log/log.h>
|
||||
#include <sled/status.h>
|
||||
|
||||
TEST(Status, format)
|
||||
{
|
||||
auto status = sled::Status(sled::StatusCode::kOk, "");
|
||||
EXPECT_EQ(fmt::format("{}", status), "");
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user