From bfca1bf206978adf9261c6eee4c423a0b8fc33e7 Mon Sep 17 00:00:00 2001 From: Liu Zhenlong <49094455+Dragonliu2018@users.noreply.github.com> Date: Wed, 1 Nov 2023 16:55:25 +0800 Subject: [PATCH] [Enhancement] Implement format methods for Status (#26133) --- be/src/common/status.h | 14 ++++++++++++++ be/test/common/status_test.cpp | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/be/src/common/status.h b/be/src/common/status.h index 7ca1aacca0..512afa88fe 100644 --- a/be/src/common/status.h +++ b/be/src/common/status.h @@ -612,3 +612,17 @@ using ResultError = unexpected; // clang-format on } // namespace doris + +// specify formatter for Status +template <> +struct fmt::formatter { + template + constexpr auto parse(ParseContext& ctx) { + return ctx.begin(); + } + + template + auto format(doris::Status const& status, FormatContext& ctx) { + return fmt::format_to(ctx.out(), "{}", status.to_string()); + } +}; diff --git a/be/test/common/status_test.cpp b/be/test/common/status_test.cpp index 8f0cdd064a..0371346e3c 100644 --- a/be/test/common/status_test.cpp +++ b/be/test/common/status_test.cpp @@ -73,4 +73,14 @@ TEST_F(StatusTest, Error) { } } +TEST_F(StatusTest /*unused*/, Format /*unused*/) { + // status == ok + Status st_ok = Status::OK(); + EXPECT_TRUE(fmt::format("{}", st_ok).compare(fmt::format("{}", st_ok.to_string())) == 0); + + // status == error + Status st_error = Status::InternalError("123"); + EXPECT_TRUE(fmt::format("{}", st_error).compare(fmt::format("{}", st_error.to_string())) == 0); +} + } // namespace doris