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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/log/bufferedlogger.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
LOG_SETUP("bufferedlogskiptest");
std::string readFile(const std::string& file) {
std::ostringstream ost;
std::ifstream is(file.c_str());
std::string line;
while (std::getline(is, line)) {
std::string::size_type pos = line.find('\t');
if (pos == std::string::npos) continue;
std::string::size_type pos2 = line.find('\t', pos + 1);
if (pos2 == std::string::npos) continue;
std::string result = line.substr(0, pos)
+ "\tlocalhost"
+ line.substr(pos2);
// Ignore debug entries. (Log adds some itself with timestamp we
// can't control)
if (result.find("\tdebug\t") == std::string::npos) {
ost << result << "\n";
}
}
return ost.str();
}
void testSkipBufferOnDebug(const std::string& file, uint64_t & timer)
{
std::cerr << "testSkipBufferOnDebug ...\n";
LOGBM(info, "Starting up, using logfile %s", file.c_str());
timer = 200 * 1000000;
for (uint32_t i=0; i<10; ++i) {
LOGBP(info, "Message");
timer += 1;
LOGBM(info, "Message");
timer += 1;
LOGBT(info, "Message", "Message");
timer += 1;
}
std::string result(readFile(file));
std::string expected(readFile("bufferedlogskiptest.skipped.log"));
if (result != expected) {
std::cerr << "Failed "
<< "testSkipBufferOnDebug\n";
[[maybe_unused]] int system_result =
system(("diff -u " + file
+ " bufferedlogskiptest.skipped.log").c_str());
exit(EXIT_FAILURE);
}
unlink(file.c_str());
}
void reset(uint64_t & timer) {
timer = 0;
ns_log::BufferedLogger::logger.setMaxCacheSize(10);
ns_log::BufferedLogger::logger.setMaxEntryAge(300);
ns_log::BufferedLogger::logger.setCountFactor(5);
}
int
main(int argc, char **argv)
{
if (argc != 2) {
std::cerr << "bufferedlogskiptest must be called with one argument\n";
exit(EXIT_FAILURE);
}
ns_log::Logger::fakePid = true;
uint64_t timer;
logger.setTimer(std::unique_ptr<ns_log::Timer>(new ns_log::TestTimer(timer)));
ns_log::BufferedLogger::logger.setTimer(std::unique_ptr<ns_log::Timer>(new ns_log::TestTimer(timer)));
reset(timer);
testSkipBufferOnDebug(argv[1], timer);
exit(EXIT_SUCCESS);
}
|