diff options
| author | Stefan Boberg <[email protected]> | 2021-09-07 11:04:47 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-09-07 11:04:47 +0200 |
| commit | e9b5405146d2a25e50d970e6f33a8adb597b01ce (patch) | |
| tree | 121cad36b51ec71884fb2bc9b571b95864e0bc0d | |
| parent | Further xmake file cleanup (diff) | |
| parent | Merge branch 'main' of https://github.com/EpicGames/zen (diff) | |
| download | zen-e9b5405146d2a25e50d970e6f33a8adb597b01ce.tar.xz zen-e9b5405146d2a25e50d970e6f33a8adb597b01ce.zip | |
Merge branch 'main' of https://github.com/EpicGames/zen
| -rw-r--r-- | zenserver/diag/formatters.h | 66 | ||||
| -rw-r--r-- | zenserver/upstream/jupiter.cpp | 50 | ||||
| -rw-r--r-- | zenserver/upstream/zen.cpp | 47 |
3 files changed, 78 insertions, 85 deletions
diff --git a/zenserver/diag/formatters.h b/zenserver/diag/formatters.h new file mode 100644 index 000000000..d4eeeb31c --- /dev/null +++ b/zenserver/diag/formatters.h @@ -0,0 +1,66 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <fmt/format.h> + +#pragma warning(push) +#pragma warning(disable : 4004) +#pragma warning(disable : 4996) +#include <cpr/cpr.h> +#pragma warning(pop) + +template<> +struct fmt::formatter<cpr::Response> +{ + constexpr auto parse(format_parse_context& Ctx) -> decltype(Ctx.begin()) { return Ctx.end(); } + + template<typename FormatContext> + auto format(const cpr::Response& Response, FormatContext& Ctx) -> decltype(Ctx.out()) + { + using namespace std::literals; + + if (Response.status_code == 200) + { + return fmt::format_to(Ctx.out(), + "Url: {}, Status: {}, Bytes: {}/{} (Up/Down), Elapsed: {}s", + Response.url.str(), + Response.status_code, + Response.uploaded_bytes, + Response.downloaded_bytes, + Response.elapsed); + } + else + { + const auto It = Response.header.find("Content-Type"); + const std::string_view ContentType = It != Response.header.end() ? It->second : "<None>"sv; + + const bool IsBinary = ContentType == "application/x-ue-cb"sv || ContentType == "application/x-ue-comp"sv || + ContentType == "application/octet-stream"; + + if (IsBinary) + { + return fmt::format_to(Ctx.out(), + "Url: {}, Status: {}, Bytes: {}/{} (Up/Down), Elapsed: {}s, Reason: '{}'", + Response.url.str(), + Response.status_code, + Response.uploaded_bytes, + Response.downloaded_bytes, + Response.elapsed, + Response.reason); + } + else + { + return fmt::format_to(Ctx.out(), + "Url: {}, Status: {}, Bytes: {}/{} (Up/Down), Elapsed: {}s, Reponse: '{}', Reason: '{}'", + Response.url.str(), + Response.status_code, + Response.uploaded_bytes, + Response.downloaded_bytes, + Response.elapsed, + Response.text, + Response.reason); + } + } + } +}; diff --git a/zenserver/upstream/jupiter.cpp b/zenserver/upstream/jupiter.cpp index d204e8f0b..9a013963a 100644 --- a/zenserver/upstream/jupiter.cpp +++ b/zenserver/upstream/jupiter.cpp @@ -3,6 +3,7 @@ #include "jupiter.h" #include "cache/structuredcachestore.h" +#include "diag/formatters.h" #include "diag/logging.h" #include <fmt/format.h> @@ -52,43 +53,6 @@ namespace detail { cpr::Session Session; }; - static void Log(spdlog::logger& Log, std::string_view Verb, const cpr::Response& Response) - { - std::string_view ContentType = "unknown"sv; - if (auto It = Response.header.find("Content-Type"); It != Response.header.end()) - { - ContentType = It->second; - } - - const uint64_t Bytes = Verb == "GET"sv ? Response.downloaded_bytes : Response.uploaded_bytes; - - const bool IsBinary = - ContentType == "application/x-ue-cb"sv || ContentType == "application/x-ue-comp"sv || ContentType == "application/octet-stream"; - - if (IsBinary) - { - Log.debug("{} '{}', Status: '{}', Elapsed: '{}', Content-Type: '{}' '{}' Bytes, Reason: '{}'", - Verb, - Response.url.str(), - Response.status_code, - Response.elapsed, - ContentType, - Bytes, - Response.reason); - } - else - { - Log.debug("{} '{}', Status: '{}', Elapsed: '{}', Content-Type: '{}': '{}', Reason: '{}'", - Verb, - Response.url.str(), - Response.status_code, - Response.elapsed, - ContentType, - Response.text, - Response.reason); - } - } - } // namespace detail CloudCacheSession::CloudCacheSession(CloudCacheClient* OuterClient) : m_Log(OuterClient->Logger()), m_CacheClient(OuterClient) @@ -117,7 +81,7 @@ CloudCacheSession::GetDerivedData(std::string_view BucketId, std::string_view Ke cpr::Response Response = Session.Get(); - detail::Log(m_Log, "GET"sv, Response); + m_Log.debug("GET {}", Response); if (Response.status_code == 200) { @@ -150,7 +114,7 @@ CloudCacheSession::GetRef(std::string_view BucketId, const IoHash& Key, ZenConte Session.SetOption(cpr::Header{{"Authorization", Auth}, {"Accept", ContentType}}); cpr::Response Response = Session.Get(); - detail::Log(m_Log, "GET"sv, Response); + m_Log.debug("GET {}", Response); if (Response.status_code == 200) { @@ -175,7 +139,7 @@ CloudCacheSession::GetCompressedBlob(const IoHash& Key) Session.SetOption(cpr::Header{{"Authorization", Auth}, {"Accept", "application/x-ue-comp"}}); cpr::Response Response = Session.Get(); - detail::Log(m_Log, "GET"sv, Response); + m_Log.debug("GET {}", Response); if (Response.status_code == 200) { @@ -204,7 +168,7 @@ CloudCacheSession::PutDerivedData(std::string_view BucketId, std::string_view Ke Session.SetBody(cpr::Body{(const char*)DerivedData.Data(), DerivedData.Size()}); cpr::Response Response = Session.Put(); - detail::Log(m_Log, "PUT"sv, Response); + m_Log.debug("PUT {}", Response); return {.Success = Response.status_code == 200}; } @@ -235,7 +199,7 @@ CloudCacheSession::PutRef(std::string_view BucketId, const IoHash& Key, IoBuffer Session.SetBody(cpr::Body{(const char*)Ref.Data(), Ref.Size()}); cpr::Response Response = Session.Put(); - detail::Log(m_Log, "PUT"sv, Response); + m_Log.debug("PUT {}", Response); return {.Success = Response.status_code == 200}; } @@ -256,7 +220,7 @@ CloudCacheSession::PutCompressedBlob(const IoHash& Key, IoBuffer Blob) Session.SetBody(cpr::Body{(const char*)Blob.Data(), Blob.Size()}); cpr::Response Response = Session.Put(); - detail::Log(m_Log, "PUT"sv, Response); + m_Log.debug("PUT {}", Response); return {.Success = Response.status_code == 200}; } diff --git a/zenserver/upstream/zen.cpp b/zenserver/upstream/zen.cpp index 3d4999e5d..7ed4eead0 100644 --- a/zenserver/upstream/zen.cpp +++ b/zenserver/upstream/zen.cpp @@ -8,6 +8,7 @@ #include <zencore/stream.h> #include "cache/structuredcachestore.h" +#include "diag/formatters.h" #include "diag/logging.h" // cpr //////////////////////////////////////////////////////////////////// @@ -323,44 +324,6 @@ namespace detail { cpr::Session Session; }; - static void LogResponse(spdlog::logger& Log, std::string_view Verb, const cpr::Response& Response) - { - using namespace std::literals; - - std::string_view ContentType = "unknown"sv; - if (auto It = Response.header.find("Content-Type"); It != Response.header.end()) - { - ContentType = It->second; - } - - const uint64_t Bytes = Verb == "GET"sv ? Response.downloaded_bytes : Response.uploaded_bytes; - - const bool IsBinary = - ContentType == "application/x-ue-cb"sv || ContentType == "application/x-ue-comp"sv || ContentType == "application/octet-stream"; - - if (IsBinary) - { - Log.debug("{} '{}', Status: '{}', Elapsed: '{}', Content-Type: '{}' '{}' Bytes, Reason: '{}'", - Verb, - Response.url.str(), - Response.status_code, - Response.elapsed, - ContentType, - Bytes, - Response.reason); - } - else - { - Log.debug("{} '{}', Status: '{}', Elapsed: '{}', Content-Type: '{}': '{}', Reason: '{}'", - Verb, - Response.url.str(), - Response.status_code, - Response.elapsed, - ContentType, - Response.text, - Response.reason); - } - } } // namespace detail ////////////////////////////////////////////////////////////////////////// @@ -429,7 +392,7 @@ ZenStructuredCacheSession::GetCacheRecord(std::string_view BucketId, const IoHas Session.SetHeader(cpr::Header{{"Accept", Type == ZenContentType::kCbObject ? "application/x-ue-cb" : "application/octet-stream"}}); cpr::Response Response = Session.Get(); - detail::LogResponse(m_Log, "GET"sv, Response); + m_Log.debug("GET {}", Response); if (Response.status_code == 200) { @@ -451,7 +414,7 @@ ZenStructuredCacheSession::GetCachePayload(std::string_view BucketId, const IoHa Session.SetHeader(cpr::Header{{"Accept", "application/x-ue-comp"}}); cpr::Response Response = Session.Get(); - detail::LogResponse(m_Log, "GET"sv, Response); + m_Log.debug("GET {}", Response); if (Response.status_code == 200) { @@ -475,7 +438,7 @@ ZenStructuredCacheSession::PutCacheRecord(std::string_view BucketId, const IoHas Session.SetBody(cpr::Body{static_cast<const char*>(Value.Data()), Value.Size()}); cpr::Response Response = Session.Put(); - detail::LogResponse(m_Log, "PUT"sv, Response); + m_Log.debug("PUT {}", Response); return {.Success = Response.status_code == 200}; } @@ -493,7 +456,7 @@ ZenStructuredCacheSession::PutCachePayload(std::string_view BucketId, const IoHa Session.SetBody(cpr::Body{static_cast<const char*>(Payload.Data()), Payload.Size()}); cpr::Response Response = Session.Put(); - detail::LogResponse(m_Log, "PUT"sv, Response); + m_Log.debug("PUT {}", Response); return {.Success = Response.status_code == 200}; } |