1 | #include <gtest/gtest.h> |
---|
2 | #include "util/Output.h" |
---|
3 | #include "util/output/LogWriter.h" |
---|
4 | #include "util/Convert.h" |
---|
5 | #include "util/output/OutputManager.h" |
---|
6 | |
---|
7 | namespace orxonox |
---|
8 | { |
---|
9 | namespace |
---|
10 | { |
---|
11 | class MockLogWriter : public LogWriter |
---|
12 | { |
---|
13 | public: |
---|
14 | virtual void printLine(const std::string& line, OutputLevel level) override |
---|
15 | { this->LogWriter::printLine(line, level); } |
---|
16 | }; |
---|
17 | |
---|
18 | // Fixture |
---|
19 | class LogWriterTest : public ::testing::Test |
---|
20 | { |
---|
21 | public: |
---|
22 | virtual void SetUp() override |
---|
23 | { |
---|
24 | // reset output manager |
---|
25 | OutputManager::Testing::getInstancePointer().reset(new OutputManager()); |
---|
26 | } |
---|
27 | |
---|
28 | virtual void TearDown() override |
---|
29 | { |
---|
30 | } |
---|
31 | }; |
---|
32 | } |
---|
33 | |
---|
34 | // test constructor opens file |
---|
35 | TEST_F(LogWriterTest, ConstructorOpensFile) |
---|
36 | { |
---|
37 | LogWriter logWriter; |
---|
38 | EXPECT_TRUE(logWriter.getFile().is_open()); |
---|
39 | } |
---|
40 | |
---|
41 | // setLogDirectory changes file, opens correct file |
---|
42 | bool fileExists(const std::string& path) |
---|
43 | { |
---|
44 | std::ifstream stream(path.c_str()); |
---|
45 | bool exists = stream.is_open() && stream.good(); |
---|
46 | stream.close(); |
---|
47 | return exists; |
---|
48 | } |
---|
49 | |
---|
50 | std::string getLineWhichContains(const std::string& path, const std::string& message) |
---|
51 | { |
---|
52 | std::ifstream file(path.c_str()); |
---|
53 | EXPECT_TRUE(file.is_open()); |
---|
54 | EXPECT_TRUE(file.good()); |
---|
55 | EXPECT_FALSE(file.eof()); |
---|
56 | |
---|
57 | while (file.is_open() && file.good() && !file.eof()) |
---|
58 | { |
---|
59 | std::string line; |
---|
60 | std::getline(file, line); |
---|
61 | |
---|
62 | // see if we find the output and return |
---|
63 | if (line.find(message) == (line.size() - message.size())) |
---|
64 | return line; |
---|
65 | } |
---|
66 | |
---|
67 | return std::string(); |
---|
68 | } |
---|
69 | |
---|
70 | bool fileContains(const std::string& path, const std::string& message) |
---|
71 | { |
---|
72 | return !getLineWhichContains(path, message).empty(); |
---|
73 | } |
---|
74 | |
---|
75 | bool deleteFile(const std::string& path) |
---|
76 | { |
---|
77 | bool result = std::remove(path.c_str()) == 0; |
---|
78 | EXPECT_FALSE(fileExists(path)); |
---|
79 | return result; |
---|
80 | } |
---|
81 | |
---|
82 | TEST_F(LogWriterTest, SetLogDirectoryOpensNewFile) |
---|
83 | { |
---|
84 | std::string path = "./orxonox.log"; |
---|
85 | |
---|
86 | // cleanup before test |
---|
87 | deleteFile(path); |
---|
88 | |
---|
89 | { |
---|
90 | LogWriter logWriter; |
---|
91 | EXPECT_FALSE(fileExists(path)); |
---|
92 | logWriter.setLogDirectory("."); |
---|
93 | EXPECT_TRUE(fileExists(path)); |
---|
94 | } |
---|
95 | |
---|
96 | // cleanup after test |
---|
97 | EXPECT_TRUE(deleteFile(path)); |
---|
98 | } |
---|
99 | |
---|
100 | // prints output to logfile |
---|
101 | TEST_F(LogWriterTest, PrintsOutputToLogfile) |
---|
102 | { |
---|
103 | std::string path; |
---|
104 | |
---|
105 | { |
---|
106 | // write lines to log file |
---|
107 | std::vector<std::string> lines; |
---|
108 | lines.push_back("mytestoutput"); |
---|
109 | |
---|
110 | LogWriter logWriter; |
---|
111 | logWriter.setLogDirectory("."); |
---|
112 | logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines); |
---|
113 | |
---|
114 | path = logWriter.getPath(); |
---|
115 | } |
---|
116 | |
---|
117 | EXPECT_TRUE(fileContains(path, "mytestoutput")); |
---|
118 | } |
---|
119 | |
---|
120 | // prints time to logfile |
---|
121 | TEST_F(LogWriterTest, PrintsTimestampToLogfile) |
---|
122 | { |
---|
123 | std::string path; |
---|
124 | |
---|
125 | { |
---|
126 | // write lines to log file |
---|
127 | std::vector<std::string> lines; |
---|
128 | lines.push_back("myothertestoutput"); |
---|
129 | |
---|
130 | LogWriter logWriter; |
---|
131 | logWriter.setLogDirectory("."); |
---|
132 | logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines); |
---|
133 | |
---|
134 | path = logWriter.getPath(); |
---|
135 | } |
---|
136 | |
---|
137 | std::string line = getLineWhichContains(path, "myothertestoutput"); |
---|
138 | EXPECT_FALSE(line.empty()); |
---|
139 | EXPECT_TRUE(isdigit(line[0])); |
---|
140 | EXPECT_TRUE(isdigit(line[1])); |
---|
141 | EXPECT_EQ(':', line[2]); |
---|
142 | EXPECT_TRUE(isdigit(line[3])); |
---|
143 | EXPECT_TRUE(isdigit(line[4])); |
---|
144 | EXPECT_EQ(':', line[5]); |
---|
145 | EXPECT_TRUE(isdigit(line[6])); |
---|
146 | EXPECT_TRUE(isdigit(line[7])); |
---|
147 | EXPECT_EQ(' ', line[8]); |
---|
148 | } |
---|
149 | |
---|
150 | void deleteAllLogFiles() |
---|
151 | { |
---|
152 | std::string path; |
---|
153 | { |
---|
154 | LogWriter writer; |
---|
155 | path = writer.getPath(); |
---|
156 | } |
---|
157 | |
---|
158 | deleteFile(path); |
---|
159 | deleteFile(path + ".1"); |
---|
160 | deleteFile(path + ".2"); |
---|
161 | deleteFile(path + ".3"); |
---|
162 | deleteFile(path + ".4"); |
---|
163 | deleteFile(path + ".5"); |
---|
164 | deleteFile(path + ".6"); |
---|
165 | deleteFile(path + ".7"); |
---|
166 | deleteFile(path + ".8"); |
---|
167 | deleteFile(path + ".9"); |
---|
168 | } |
---|
169 | |
---|
170 | TEST_F(LogWriterTest, ArchivesOldLogFile) |
---|
171 | { |
---|
172 | deleteAllLogFiles(); |
---|
173 | |
---|
174 | std::string path; |
---|
175 | |
---|
176 | { |
---|
177 | MockLogWriter writer; |
---|
178 | path = writer.getPath(); |
---|
179 | writer.printLine("test1", level::message); |
---|
180 | } |
---|
181 | |
---|
182 | EXPECT_TRUE(fileExists(path)); |
---|
183 | EXPECT_TRUE(fileContains(path, "test1")); |
---|
184 | EXPECT_FALSE(fileExists(path + ".1")); |
---|
185 | EXPECT_FALSE(fileExists(path + ".2")); |
---|
186 | EXPECT_FALSE(fileExists(path + ".3")); |
---|
187 | |
---|
188 | { |
---|
189 | MockLogWriter writer; |
---|
190 | writer.printLine("test2", level::message); |
---|
191 | } |
---|
192 | |
---|
193 | EXPECT_TRUE(fileExists(path)); |
---|
194 | EXPECT_TRUE(fileContains(path, "test2")); |
---|
195 | EXPECT_TRUE(fileExists(path + ".1")); |
---|
196 | EXPECT_TRUE(fileContains(path + ".1", "test1")); |
---|
197 | EXPECT_FALSE(fileExists(path + ".2")); |
---|
198 | EXPECT_FALSE(fileExists(path + ".3")); |
---|
199 | |
---|
200 | { |
---|
201 | MockLogWriter writer; |
---|
202 | writer.printLine("test3", level::message); |
---|
203 | } |
---|
204 | |
---|
205 | EXPECT_TRUE(fileExists(path)); |
---|
206 | EXPECT_TRUE(fileContains(path, "test3")); |
---|
207 | EXPECT_TRUE(fileExists(path + ".1")); |
---|
208 | EXPECT_TRUE(fileContains(path + ".1", "test2")); |
---|
209 | EXPECT_TRUE(fileExists(path + ".2")); |
---|
210 | EXPECT_TRUE(fileContains(path + ".2", "test1")); |
---|
211 | EXPECT_FALSE(fileExists(path + ".3")); |
---|
212 | } |
---|
213 | |
---|
214 | TEST_F(LogWriterTest, ArchivesNineLogFiles) |
---|
215 | { |
---|
216 | deleteAllLogFiles(); |
---|
217 | |
---|
218 | std::string path; |
---|
219 | for (int i = 0; i < 20; ++i) |
---|
220 | { |
---|
221 | MockLogWriter writer; |
---|
222 | path = writer.getPath(); |
---|
223 | writer.printLine("test" + multi_cast<std::string>(i), level::message); |
---|
224 | } |
---|
225 | |
---|
226 | EXPECT_TRUE(fileContains(path, "test19")); |
---|
227 | EXPECT_TRUE(fileContains(path + ".1", "test18")); |
---|
228 | EXPECT_TRUE(fileContains(path + ".2", "test17")); |
---|
229 | EXPECT_TRUE(fileContains(path + ".3", "test16")); |
---|
230 | EXPECT_TRUE(fileContains(path + ".4", "test15")); |
---|
231 | EXPECT_TRUE(fileContains(path + ".5", "test14")); |
---|
232 | EXPECT_TRUE(fileContains(path + ".6", "test13")); |
---|
233 | EXPECT_TRUE(fileContains(path + ".7", "test12")); |
---|
234 | EXPECT_TRUE(fileContains(path + ".8", "test11")); |
---|
235 | EXPECT_TRUE(fileContains(path + ".9", "test10")); |
---|
236 | EXPECT_FALSE(fileExists(path + ".10")); |
---|
237 | EXPECT_FALSE(fileExists(path + ".11")); |
---|
238 | } |
---|
239 | } |
---|