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