forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLogger.cpp
More file actions
245 lines (194 loc) · 9.69 KB
/
Copy pathFileLogger.cpp
File metadata and controls
245 lines (194 loc) · 9.69 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include <AppInstallerFileLogger.h>
#include <AppInstallerStrings.h>
using namespace AppInstaller::Logging;
using namespace AppInstaller::Utility;
using namespace TestCommon;
std::string GetHeaderString()
{
return "TIME [CHAN] Header Message";
}
std::string GetLargeString()
{
return "[===|Clearly defined start to large string|===]\r\n"
"While this string does not need to be particularly unique, it is still good if it is not easily duplicated by any other random set of data.\r\n"
"It should also end in a character that is not used in any other way within these tests, so please don't include that character when writing tests.\r\n"
"That character is &";
}
namespace
{
#define WINGET_DEFINE_STRING_ENUM(_enum_,_value_) constexpr std::string_view _enum_##_##_value_ = #_value_##sv
WINGET_DEFINE_STRING_ENUM(TagState, Unset);
WINGET_DEFINE_STRING_ENUM(TagState, SetAtStart);
WINGET_DEFINE_STRING_ENUM(TagState, SetAfterLogging);
WINGET_DEFINE_STRING_ENUM(MaximumSizeState, Zero);
WINGET_DEFINE_STRING_ENUM(MaximumSizeState, SmallerThanLargeString);
WINGET_DEFINE_STRING_ENUM(MaximumSizeState, EqualToLargeString);
WINGET_DEFINE_STRING_ENUM(MaximumSizeState, SlightlyLargerThanLargeString);
WINGET_DEFINE_STRING_ENUM(MaximumSizeState, MuchLargerThanLargeString);
constexpr std::string_view WrapIndicator = "--- log file has wrapped ---"sv;
// The amount of extra size that is allowed (total indicator size + newline + newlines from test strings)
constexpr size_t ExtraAllowedSize = 72;
constexpr size_t NewLineCharacterCount = 2;
constexpr size_t SmallDifferenceSize = 10;
constexpr AppInstaller::Logging::Channel DefaultChannel = AppInstaller::Logging::Channel::Core;
constexpr AppInstaller::Logging::Level DefaultLevel = AppInstaller::Logging::Level::Info;
void ValidateFileContents(const std::filesystem::path& file, const std::vector<std::string_view>& expectedContents, size_t maximumSize)
{
std::ifstream fileStream{ file, std::ios::binary };
auto fileContents = ReadEntireStream(fileStream);
std::string_view fileContentsView = fileContents;
std::string fileContentsCopy = fileContents;
FindAndReplace(fileContentsCopy, "\r", "\\r");
FindAndReplace(fileContentsCopy, "\n", "\\n");
INFO("File contents:\n" << fileContentsCopy);
if (maximumSize)
{
REQUIRE(maximumSize + ExtraAllowedSize >= fileContents.size());
}
size_t currentPosition = 0;
for (std::string_view expectedContent : expectedContents)
{
REQUIRE(currentPosition < fileContents.size());
if (expectedContent == WrapIndicator)
{
auto endLinePosition = fileContentsView.find('\n', currentPosition);
REQUIRE(endLinePosition != -1);
REQUIRE(endLinePosition >= expectedContent.size() + NewLineCharacterCount);
auto actualContent = fileContentsView.substr(endLinePosition + 1 - expectedContent.size() - NewLineCharacterCount, expectedContent.size());
REQUIRE(expectedContent == actualContent);
currentPosition = endLinePosition + 1;
}
else
{
auto actualContent = fileContentsView.substr(currentPosition, expectedContent.size());
REQUIRE(expectedContent == actualContent);
currentPosition += expectedContent.size() + NewLineCharacterCount;
}
}
}
void FileLogger_MaximumSize_Test(std::string_view tagState, std::string_view sizeState)
{
auto headerString = GetHeaderString();
auto largeString = GetLargeString();
// Determine maximum size
size_t maximumSize = 0;
if (sizeState == MaximumSizeState_SmallerThanLargeString)
{
maximumSize = largeString.size() - SmallDifferenceSize;
}
else if (sizeState == MaximumSizeState_EqualToLargeString)
{
maximumSize = largeString.size();
}
else if (sizeState == MaximumSizeState_SlightlyLargerThanLargeString)
{
maximumSize = largeString.size() + SmallDifferenceSize;
}
else if (sizeState == MaximumSizeState_MuchLargerThanLargeString)
{
maximumSize = largeString.size() * 2;
}
INFO("Tag State: " << tagState << ", Size State: " << sizeState << "[" << maximumSize << "]");
TempFile tempFile{ "FileLogger_MaximumSize", ".log" };
FileLogger logger{ tempFile };
INFO("File: " << tempFile.GetPath().u8string());
logger.SetMaximumSize(wil::safe_cast<std::ofstream::off_type>(maximumSize));
// Set tag and log strings
size_t tagPosition = 0;
if (tagState == TagState_SetAtStart)
{
logger.SetTag(Tag::HeadersComplete);
}
logger.WriteDirect(DefaultChannel, DefaultLevel, headerString);
if (tagState == TagState_SetAfterLogging)
{
logger.SetTag(Tag::HeadersComplete);
tagPosition = headerString.size() + NewLineCharacterCount;
}
// Due to text output in the logger, log with \n only
std::string largeStringWithoutCarriageReturn = largeString;
FindAndReplace(largeStringWithoutCarriageReturn, "\r\n", "\n");
logger.WriteDirect(DefaultChannel, DefaultLevel, largeStringWithoutCarriageReturn);
// Calculate current state
size_t maximumAvailableSpace = std::numeric_limits<size_t>::max();
size_t currentAvailableSpace = std::numeric_limits<size_t>::max();
if (maximumSize)
{
maximumAvailableSpace = maximumSize - tagPosition;
currentAvailableSpace = maximumSize - headerString.size() - NewLineCharacterCount;
}
bool shouldWrap = largeString.size() > currentAvailableSpace;
INFO("Maximum Available: " << maximumAvailableSpace << ", Current Available: " << currentAvailableSpace << ", ShouldWrap: " << shouldWrap);
std::vector<std::string_view> expectedFileContents;
if (tagPosition || !shouldWrap)
{
expectedFileContents.push_back(headerString);
}
if (shouldWrap)
{
expectedFileContents.push_back(WrapIndicator);
}
std::string_view largeStringView = largeString;
expectedFileContents.push_back(largeStringView.substr(0, std::min(largeString.size(), maximumAvailableSpace)));
ValidateFileContents(tempFile, expectedFileContents, maximumSize);
// Log again
INFO("Second time logging large string");
logger.WriteDirect(DefaultChannel, DefaultLevel, largeStringWithoutCarriageReturn);
// The maximum size is twice the large log, so anything with a limit will wrap
shouldWrap = maximumSize != 0;
expectedFileContents.clear();
if (tagPosition || !shouldWrap)
{
expectedFileContents.push_back(headerString);
}
if (shouldWrap)
{
expectedFileContents.push_back(WrapIndicator);
}
else
{
expectedFileContents.push_back(largeStringView);
}
expectedFileContents.push_back(largeStringView.substr(0, std::min(largeString.size(), maximumAvailableSpace)));
ValidateFileContents(tempFile, expectedFileContents, maximumSize);
}
}
TEST_CASE("FileLogger_MaximumSize", "[logging]")
{
auto tagState = GENERATE(TagState_Unset, TagState_SetAtStart, TagState_SetAfterLogging);
auto sizeState = GENERATE(MaximumSizeState_Zero, MaximumSizeState_SmallerThanLargeString, MaximumSizeState_EqualToLargeString, MaximumSizeState_SlightlyLargerThanLargeString, MaximumSizeState_MuchLargerThanLargeString);
FileLogger_MaximumSize_Test(tagState, sizeState);
}
TEST_CASE("FileLogger_MaximumSize_ManyWraps", "[logging]")
{
TempFile tempFile{ "FileLogger_ManyWraps", ".log" };
FileLogger logger{ tempFile };
INFO("File: " << tempFile.GetPath().u8string());
size_t maximumSize = 1000;
logger.SetMaximumSize(static_cast<std::ofstream::off_type>(maximumSize));
std::string header = GetHeaderString();
header += " !Now with more header!";
std::string largeString = "[*=INIT=*]Now we just need another few dozen characters, which shouldn't be that hard to get. Wow, made it already.";
std::string_view largeStringView = largeString;
size_t initSize = 10;
logger.WriteDirect(DefaultChannel, DefaultLevel, header);
logger.SetTag(Tag::HeadersComplete);
// Use the default seed value as we want arbitrary but reproducible results
std::default_random_engine randomEngine;
std::uniform_int_distribution<> sizeDistribution(static_cast<int>(initSize), 100);
// We should expect ~500 wraps on average
for (size_t i = 0; i < 9999; ++i)
{
logger.WriteDirect(DefaultChannel, DefaultLevel, largeStringView.substr(0, sizeDistribution(randomEngine)));
}
// We want the header to be preserved, followed by the wrap indicator, and at a minimum we should see the first few characters in the log string
std::vector<std::string_view> expectedFileContents;
expectedFileContents.push_back(header);
expectedFileContents.push_back(WrapIndicator);
expectedFileContents.push_back(largeStringView.substr(0, initSize));
ValidateFileContents(tempFile, expectedFileContents, maximumSize);
}