From 6c8f8bac1fe2f63d31ff4f34c3cde6cf2745f691 Mon Sep 17 00:00:00 2001 From: Ichiro Date: Fri, 24 Dec 2021 21:45:36 +0900 Subject: [PATCH] Add toString for `drogon::ReqResult` (#1133) * Add to_string and to_string_view for drogon::ReqResult --- lib/inc/drogon/HttpTypes.h | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/inc/drogon/HttpTypes.h b/lib/inc/drogon/HttpTypes.h index bdc19690..d1305fee 100644 --- a/lib/inc/drogon/HttpTypes.h +++ b/lib/inc/drogon/HttpTypes.h @@ -15,6 +15,8 @@ #include #include #include +#include +#include namespace drogon { @@ -169,26 +171,43 @@ enum class WebSocketMessageType Close, Unknown }; -inline std::ostream &operator<<(std::ostream &out, drogon::ReqResult result) + +inline string_view to_string_view(drogon::ReqResult result) { switch (result) { case ReqResult::Ok: - return out << "OK"; + return "OK"; case ReqResult::BadResponse: - return out << "Bad response from server"; + return "Bad response from server"; case ReqResult::NetworkFailure: - return out << "Network failure"; + return "Network failure"; case ReqResult::BadServerAddress: - return out << "Bad server address"; + return "Bad server address"; case ReqResult::Timeout: - return out << "Timeout"; + return "Timeout"; case ReqResult::HandshakeError: - return out << "Handshake error"; + return "Handshake error"; case ReqResult::InvalidCertificate: - return out << "Invalid certificate"; + return "Invalid certificate"; default: - return out << "Unknown error"; + return "Unknown error"; } } + +inline std::string to_string(drogon::ReqResult result) +{ + return to_string_view(result).data(); +} + +inline std::ostream &operator<<(std::ostream &out, drogon::ReqResult result) +{ + return out << to_string_view(result); +} + +inline trantor::LogStream &operator<<(trantor::LogStream &out, + drogon::ReqResult result) +{ + return out << to_string_view(result); +} } // namespace drogon