aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/sessions/logtemplate.cpp
blob: b4d8f37e81a07109685231ef576579060296e6f6 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright Epic Games, Inc. All Rights Reserved.

#include "logtemplate.h"

#include <zencore/fmtutils.h>
#include <zencore/guid.h>
#include <zencore/iohash.h>
#include <zencore/string.h>
#include <zencore/uid.h>

ZEN_THIRD_PARTY_INCLUDES_START
#include <fmt/format.h>
ZEN_THIRD_PARTY_INCLUDES_END

namespace zen {
using namespace std::literals;

namespace {

	// Bounded recursion so pathological nesting (e.g. an object that references
	// itself through $format) can't stack-overflow the server. Depth counts
	// every nested template expansion OR value descent.
	constexpr size_t kMaxRecursionDepth = 16;

	void RenderTemplateInto(std::string_view Template, CbObjectView Fields, StringBuilderBase& Out, bool Localized, size_t Depth);
	void RenderValue(CbFieldView Field, StringBuilderBase& Out, bool Localized, size_t Depth);

	//////////////////////////////////////////////////////////////////////////
	//
	// Path resolution: walk a UE field_path — `name` followed by zero or
	// more `.name` / `[N]` segments — starting from the fields root. Returns
	// an empty field on any miss.
	//

	CbFieldView ResolvePath(CbObjectView Root, std::string_view Path)
	{
		CbFieldView Cur;
		bool		Entered = false;  // have we applied at least one segment?

		const auto ApplyName = [&](std::string_view Name) -> bool {
			if (Name.empty())
			{
				return false;
			}
			if (!Entered)
			{
				Cur = Root[Name];
			}
			else
			{
				if (!Cur.IsObject())
				{
					return false;
				}
				Cur = Cur.AsObjectView()[Name];
			}
			Entered = true;
			return Cur.operator bool();
		};

		const auto ApplyIndex = [&](uint64_t Idx) -> bool {
			if (!Entered || !Cur.IsArray())
			{
				return false;
			}
			uint64_t N = 0;
			for (CbFieldView Elem : Cur.AsArrayView().CreateViewIterator())
			{
				if (N == Idx)
				{
					Cur = Elem;
					return Cur.operator bool();
				}
				++N;
			}
			return false;
		};

		size_t i = 0;
		while (i < Path.size())
		{
			const char C = Path[i];
			if (C == '.')
			{
				++i;
				continue;
			}
			if (C == '[')
			{
				const size_t End = Path.find(']', i + 1);
				if (End == std::string_view::npos)
				{
					return {};
				}
				uint64_t Idx = 0;
				for (size_t j = i + 1; j < End; ++j)
				{
					const char D = Path[j];
					if (D < '0' || D > '9')
					{
						return {};
					}
					Idx = Idx * 10 + uint64_t(D - '0');
				}
				if (!ApplyIndex(Idx))
				{
					return {};
				}
				i = End + 1;
				continue;
			}
			// Name segment: run until the next '.' or '['.
			const size_t NameStart = i;
			while (i < Path.size() && Path[i] != '.' && Path[i] != '[')
			{
				++i;
			}
			if (!ApplyName(Path.substr(NameStart, i - NameStart)))
			{
				return {};
			}
		}
		return Entered ? Cur : CbFieldView{};
	}

	//////////////////////////////////////////////////////////////////////////
	//
	// Primitive rendering. Uses natural string forms for each CbField type.
	// Non-string values are emitted without quotes — the caller (the JSON-ish
	// fallback) adds quotes only around string values.
	//

	void RenderPrimitive(CbFieldView Field, StringBuilderBase& Out)
	{
		if (Field.IsString())
		{
			Out << Field.AsString();
			return;
		}
		if (Field.IsBool())
		{
			Out << (Field.AsBool() ? "true"sv : "false"sv);
			return;
		}
		if (Field.IsInteger())
		{
			Out << Field.AsInt64();
			return;
		}
		if (Field.IsFloat())
		{
			// format_to into the builder directly — avoids the std::string
			// fmt::format would otherwise build just to hand to Append.
			fmt::format_to(StringBuilderAppender(Out), "{}", Field.AsDouble());
			return;
		}
		if (Field.IsDateTime())
		{
			Out.Append(Field.AsDateTime().ToIso8601());
			return;
		}
		if (Field.IsObjectId())
		{
			// ToString(char[]) writes the 24-char hex into a caller buffer;
			// the std::string overload would allocate.
			char Buf[Oid::StringLength + 1] = {};
			Field.AsObjectId().ToString(Buf);
			Out << std::string_view(Buf, Oid::StringLength);
			return;
		}
		if (Field.IsHash())
		{
			// Appender overload writes the 40-char hex directly into the
			// builder; the std::string overload would allocate.
			Field.AsHash().ToHexString(Out);
			return;
		}
		if (Field.IsUuid())
		{
			Guid G = Field.AsUuid();
			G.ToString(Out);
			return;
		}
		if (Field.IsNull())
		{
			Out << "null"sv;
			return;
		}
		// Binary / attachment / custom / unknown → emit nothing rather than
		// a stream of garbage bytes.
	}

	//////////////////////////////////////////////////////////////////////////
	//
	// JSON-ish fallback for bare objects / nested arrays. Compact single-line
	// with quoted string keys and string values, raw other types. Intended
	// for debug display — not strictly RFC-8259 JSON.
	//

	void AppendJsonishString(std::string_view S, StringBuilderBase& Out)
	{
		Out << '"';
		for (char C : S)
		{
			switch (C)
			{
				case '"':
					Out << "\\\""sv;
					break;
				case '\\':
					Out << "\\\\"sv;
					break;
				case '\n':
					Out << "\\n"sv;
					break;
				case '\r':
					Out << "\\r"sv;
					break;
				case '\t':
					Out << "\\t"sv;
					break;
				default:
					Out << C;
					break;
			}
		}
		Out << '"';
	}

	void AppendJsonishValue(CbFieldView Field, StringBuilderBase& Out, bool Localized, size_t Depth)
	{
		if (Field.IsString())
		{
			AppendJsonishString(Field.AsString(), Out);
			return;
		}
		// Non-string leaves and nested objects/arrays go through RenderValue
		// so object short-circuits ($text / $format / ...) still apply.
		RenderValue(Field, Out, Localized, Depth);
	}

	//////////////////////////////////////////////////////////////////////////
	//
	// Value rendering (the decision tree from the plan).
	//

	void RenderValue(CbFieldView Field, StringBuilderBase& Out, bool Localized, size_t Depth)
	{
		if (Depth >= kMaxRecursionDepth)
		{
			Out << "…"sv;
			return;
		}
		if (Field.IsObject())
		{
			CbObjectView Obj = Field.AsObjectView();

			if (CbFieldView Text = Obj["$text"sv]; Text.IsString())
			{
				Out << Text.AsString();
				return;
			}
			if (CbFieldView Format = Obj["$format"sv]; Format.IsString())
			{
				RenderTemplateInto(Format.AsString(), Obj, Out, /*Localized=*/false, Depth + 1);
				return;
			}
			if (CbFieldView LocFormat = Obj["$locformat"sv]; LocFormat.IsString())
			{
				RenderTemplateInto(LocFormat.AsString(), Obj, Out, /*Localized=*/true, Depth + 1);
				return;
			}

			// Bare object — JSON-ish fallback.
			Out << '{';
			bool First = true;
			for (CbFieldView Entry : Obj.CreateViewIterator())
			{
				if (!First)
				{
					Out << ", "sv;
				}
				First = false;
				AppendJsonishString(Entry.GetName(), Out);
				Out << ": "sv;
				AppendJsonishValue(Entry, Out, Localized, Depth + 1);
			}
			Out << '}';
			return;
		}
		if (Field.IsArray())
		{
			Out << '[';
			bool First = true;
			for (CbFieldView Elem : Field.AsArrayView().CreateViewIterator())
			{
				if (!First)
				{
					Out << ", "sv;
				}
				First = false;
				AppendJsonishValue(Elem, Out, Localized, Depth + 1);
			}
			Out << ']';
			return;
		}
		RenderPrimitive(Field, Out);
	}

	//////////////////////////////////////////////////////////////////////////
	//
	// Template tokenizer + renderer.
	//

	void RenderTemplateInto(std::string_view Template, CbObjectView Fields, StringBuilderBase& Out, bool Localized, size_t Depth)
	{
		if (Depth >= kMaxRecursionDepth)
		{
			Out << "…"sv;
			return;
		}

		size_t i = 0;
		while (i < Template.size())
		{
			const char C = Template[i];

			// Localized escape: ` followed by {, }, or ` → literal.
			if (Localized && C == '`' && i + 1 < Template.size())
			{
				const char Next = Template[i + 1];
				if (Next == '{' || Next == '}' || Next == '`')
				{
					Out << Next;
					i += 2;
					continue;
				}
			}

			// Non-localized escape: {{ or }} → literal { or }.
			if (!Localized && C == '{' && i + 1 < Template.size() && Template[i + 1] == '{')
			{
				Out << '{';
				i += 2;
				continue;
			}
			if (!Localized && C == '}' && i + 1 < Template.size() && Template[i + 1] == '}')
			{
				Out << '}';
				i += 2;
				continue;
			}

			if (C == '{')
			{
				// Placeholder: scan until matching '}'.
				const size_t End = Template.find('}', i + 1);
				if (End == std::string_view::npos)
				{
					// Unterminated placeholder — emit the rest literally so we
					// don't silently drop data. UE would have asserted at emit.
					Out << Template.substr(i);
					return;
				}
				const std::string_view Path		= Template.substr(i + 1, End - i - 1);
				const CbFieldView	   Resolved = ResolvePath(Fields, Path);
				if (Resolved)
				{
					RenderValue(Resolved, Out, Localized, Depth + 1);
				}
				// Missing placeholder: emit nothing. (UE asserts at emit time,
				// so in well-formed input this never fires.)
				i = End + 1;
				continue;
			}

			Out << C;
			++i;
		}
	}

}  // namespace

void
RenderLogTemplate(std::string_view Template, CbObjectView Fields, StringBuilderBase& Out, bool Localized)
{
	RenderTemplateInto(Template, Fields, Out, Localized, 0);
}

}  // namespace zen