diff options
| author | Per Larsson <[email protected]> | 2021-09-07 10:26:10 +0200 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2021-09-07 10:26:10 +0200 |
| commit | 20b15d45bd9b34a3f9297c4be1525e25379e5602 (patch) | |
| tree | e7208bd37d364fb9ea413ee7c750a6c6f58e92f7 /zenserver/diag | |
| parent | Support for switching between storing derived data using the legacy DDC endpo... (diff) | |
| download | zen-20b15d45bd9b34a3f9297c4be1525e25379e5602.tar.xz zen-20b15d45bd9b34a3f9297c4be1525e25379e5602.zip | |
Added custom cpr::Response formatter and removed duplicate logging code.
Diffstat (limited to 'zenserver/diag')
| -rw-r--r-- | zenserver/diag/formatters.h | 66 |
1 files changed, 66 insertions, 0 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); + } + } + } +}; |