Repository navigation
Expand file tree
/
Copy pathgtest_log.cpp
More file actions
259 lines (210 loc) · 8.25 KB
/
Copy pathgtest_log.cpp
File metadata and controls
259 lines (210 loc) · 8.25 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <string>
#include <vector>
#include <base/sleep.h>
#include <Common/logger_useful.h>
#include <Common/thread_local_rng.h>
#include <gtest/gtest.h>
#include <Poco/Logger.h>
#include <Poco/AutoPtr.h>
#include <Poco/FileChannel.h>
#include <Poco/NullChannel.h>
#include <Poco/StreamChannel.h>
#include <Poco/TemporaryFile.h>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <thread>
TEST(Logger, Log)
{
Poco::Logger::root().setLevel("none");
Poco::Logger::root().setChannel(Poco::AutoPtr<Poco::NullChannel>(new Poco::NullChannel()));
LoggerPtr log = getLogger("Log");
/// This test checks that we don't pass this string to fmtlib, because it is the only argument.
EXPECT_NO_THROW(LOG_INFO(log, fmt::runtime("Hello {} World")));
}
TEST(Logger, TestLog)
{
{ /// Test logs visible for test level
std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
auto my_channel = Poco::AutoPtr<Poco::StreamChannel>(new Poco::StreamChannel(oss));
auto log = createLogger("TestLogger", my_channel.get());
log->setLevel("test");
LOG_TEST(log, "Hello World");
EXPECT_EQ(oss.str(), "Hello World\n");
}
{ /// Test logs invisible for other levels
for (const auto & level : {"trace", "debug", "information", "warning", "error", "fatal"})
{
std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
auto my_channel = Poco::AutoPtr<Poco::StreamChannel>(new Poco::StreamChannel(oss));
auto log = createLogger(std::string{level} + "_Logger", my_channel.get());
log->setLevel(level);
LOG_TEST(log, "Hello World");
EXPECT_EQ(oss.str(), "");
}
}
}
static size_t global_counter = 0;
static std::string getLogMessage()
{
++global_counter;
return "test1 " + std::to_string(thread_local_rng());
}
static size_t getLogMessageParam()
{
++global_counter;
return thread_local_rng();
}
static PreformattedMessage getPreformatted()
{
++global_counter;
return PreformattedMessage::create("test3 {}", thread_local_rng());
}
TEST(Logger, SideEffects)
{
std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
auto my_channel = Poco::AutoPtr<Poco::StreamChannel>(new Poco::StreamChannel(oss));
auto log = createLogger("Logger", my_channel.get());
log->setLevel("trace");
/// Ensure that parameters are evaluated only once
global_counter = 0;
LOG_TRACE(log, fmt::runtime(getLogMessage()));
EXPECT_EQ(global_counter, 1);
LOG_TRACE(log, "test2 {}", getLogMessageParam());
EXPECT_EQ(global_counter, 2);
LOG_TRACE(log, getPreformatted());
EXPECT_EQ(global_counter, 3);
auto var = PreformattedMessage::create("test4 {}", thread_local_rng());
LOG_TRACE(log, var);
EXPECT_EQ(var.text.starts_with("test4 "), true);
EXPECT_EQ(var.format_string, "test4 {}");
}
TEST(Logger, SharedRawLogger)
{
{
std::ostringstream stream; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
auto stream_channel = Poco::AutoPtr<Poco::StreamChannel>(new Poco::StreamChannel(stream));
auto shared_logger = getLogger("Logger_1");
shared_logger->setChannel(stream_channel.get());
shared_logger->setLevel("trace");
LOG_TRACE(shared_logger, "SharedLogger1Log1");
LOG_TRACE(getRawLogger("Logger_1"), "RawLogger1Log");
LOG_TRACE(shared_logger, "SharedLogger1Log2");
auto actual = stream.str();
EXPECT_EQ(actual, "SharedLogger1Log1\nRawLogger1Log\nSharedLogger1Log2\n");
}
{
std::ostringstream stream; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
auto stream_channel = Poco::AutoPtr<Poco::StreamChannel>(new Poco::StreamChannel(stream));
auto * raw_logger = getRawLogger("Logger_2");
raw_logger->setChannel(stream_channel.get());
raw_logger->setLevel("trace");
LOG_TRACE(getLogger("Logger_2"), "SharedLogger2Log1");
LOG_TRACE(raw_logger, "RawLogger2Log");
LOG_TRACE(getLogger("Logger_2"), "SharedLogger2Log2");
auto actual = stream.str();
EXPECT_EQ(actual, "SharedLogger2Log1\nRawLogger2Log\nSharedLogger2Log2\n");
}
}
TEST(Logger, SharedLoggersThreadSafety)
{
static size_t threads_count = std::thread::hardware_concurrency();
static constexpr size_t loggers_count = 10;
static constexpr size_t logger_get_count = 1000;
Poco::Logger::root();
std::vector<std::string> names;
Poco::Logger::names(names);
size_t loggers_size_before = names.size();
std::vector<std::thread> threads;
threads.reserve(threads_count);
for (size_t thread_index = 0; thread_index < threads_count; ++thread_index)
{
threads.emplace_back([]()
{
for (size_t logger_index = 0; logger_index < loggers_count; ++logger_index)
{
for (size_t iteration = 0; iteration < logger_get_count; ++iteration)
{
getLogger("Logger_" + std::to_string(logger_index));
}
}
});
}
for (auto & thread : threads)
thread.join();
Poco::Logger::names(names);
size_t loggers_size_after = names.size();
EXPECT_EQ(loggers_size_before, loggers_size_after);
}
TEST(Logger, ExceptionsPropagatedFromArguments)
{
std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
auto my_channel = Poco::AutoPtr<Poco::StreamChannel>(new Poco::StreamChannel(oss));
auto log = createLogger("Logger", my_channel.get());
log->setLevel("trace");
std::optional<int> empty_optional;
EXPECT_THROW(LOG_TRACE(log, "my value is {}", empty_optional.value()), std::bad_optional_access);
}
class AlwaysFailingChannel : public Poco::StreamChannel
{
public:
explicit AlwaysFailingChannel(std::ostream & str)
: Poco::StreamChannel(str)
{
}
void log(const Poco::Message &) override { throw Poco::Exception("Exception from AlwaysFailingChannel"); }
};
TEST(Logger, ExceptionsFromPocoLoggerAreNotPropagated)
{
std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
auto my_channel = Poco::AutoPtr<Poco::StreamChannel>(new AlwaysFailingChannel(oss));
auto log = createLogger("Logger", my_channel.get());
log->setLevel("trace");
EXPECT_NO_THROW(LOG_TRACE(log, "my value is {}", 42));
EXPECT_EQ(oss.str(), "");
}
static std::vector<std::string> getLogFileNames(const std::string & log_dir)
{
std::vector<std::string> log_file_names;
for (const auto & entry : std::filesystem::directory_iterator(log_dir))
log_file_names.emplace_back(entry.path().filename());
std::sort(log_file_names.begin(), log_file_names.end());
return log_file_names;
}
static std::string readLogFile(const std::string & path, bool skip_comments = true)
{
std::string file_contents;
std::ifstream input(path);
while (input)
{
std::string line;
std::getline(input, line, '\n');
if (!line.empty() && (!skip_comments || !line.starts_with("#")))
file_contents.append(line).append("\n");
}
return file_contents;
}
TEST(Logger, Rotation)
{
Poco::TemporaryFile temp_dir;
temp_dir.createDirectories();
auto my_channel = Poco::AutoPtr<Poco::FileChannel>(new Poco::FileChannel());
std::filesystem::path log_dir = std::filesystem::path{temp_dir.path()};
my_channel->setProperty(Poco::FileChannel::PROP_PATH, log_dir / "logger.log");
my_channel->setProperty(Poco::FileChannel::PROP_ROTATION, "200, 100 milliseconds");
auto log = createLogger("Logger", my_channel.get());
log->setLevel("trace");
LOG_INFO(log, "A");
LOG_INFO(log, "B");
LOG_INFO(log, "{}", std::string(201, 'C')); /// This should cause a rotation.
LOG_INFO(log, "D");
LOG_INFO(log, "E");
sleepForMilliseconds(101); /// This should cause a rotation.
LOG_INFO(log, "F");
/// We expect three log files at this point: "logger.log.1", "logger.log.0", "logger.log".
std::vector<std::string> expected_log_file_names{"logger.log", "logger.log.0", "logger.log.1"};
EXPECT_EQ(getLogFileNames(log_dir), expected_log_file_names);
EXPECT_EQ(readLogFile(log_dir / "logger.log.1"), "A\nB\n" + std::string(201, 'C') + "\n");
EXPECT_EQ(readLogFile(log_dir / "logger.log.0"), "D\nE\n");
EXPECT_EQ(readLogFile(log_dir / "logger.log"), "F\n");
}