aboutsummaryrefslogtreecommitdiff
path: root/zenserver/diag/formatters.h
blob: 759df58d39a8d67dfedaffb49fb403a556e876e2 (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
64
65
66
67
68
69
70
71
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/compactbinary.h>
#include <zencore/compactbinaryvalidation.h>
#include <zencore/iobuffer.h>
#include <zencore/string.h>

ZEN_THIRD_PARTY_INCLUDES_START
#include <cpr/cpr.h>
#include <fmt/format.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 || Response.status_code == 201)
		{
			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;

			if (ContentType == "application/x-ue-cb"sv)
			{
				zen::IoBuffer					  Body(zen::IoBuffer::Wrap, Response.text.data(), Response.text.size());
				zen::CbObjectView				  Obj(Body.Data());
				zen::ExtendableStringBuilder<256> Sb;
				std::string_view				  Json = Obj.ToJson(Sb).ToView();

				return fmt::format_to(Ctx.out(),
									  "Url: {}, Status: {}, Bytes: {}/{} (Up/Down), Elapsed: {}s, Response: '{}', Reason: '{}'",
									  Response.url.str(),
									  Response.status_code,
									  Response.uploaded_bytes,
									  Response.downloaded_bytes,
									  Response.elapsed,
									  Json,
									  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);
			}
		}
	}
};