aboutsummaryrefslogtreecommitdiff
path: root/zenserver/diag/formatters.h
blob: d55765612aee5cbf18a6e1102504e50e31b4ac36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

ZEN_THIRD_PARTY_INCLUDES_START
#include <fmt/format.h>
#include <cpr/cpr.h>
ZEN_THIRD_PARTY_INCLUDES_END

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);
			}
		}
	}
};