Changeset 9544
- Timestamp:
- Mar 11, 2013, 11:13:21 PM (12 years ago)
- Location:
- code/branches/testing
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/testing/src/libraries/util/output/LogWriter.cc
r9540 r9544 39 39 #include "OutputManager.h" 40 40 #include "MemoryWriter.h" 41 #include "util/Convert.h" 41 42 42 43 namespace orxonox 43 44 { 45 static const int MAX_ARCHIVED_FILES = 9; 46 44 47 /** 45 48 @brief Constructor, initializes the desired output levels and the name and path of the log-file, and opens the log-file. … … 82 85 void LogWriter::openFile() 83 86 { 87 // archive the old log file 88 this->archive(); 89 84 90 // open the file 85 91 this->file_.open(this->getPath().c_str(), std::fstream::out); … … 102 108 this->file_.close(); 103 109 } 110 } 111 112 /** 113 * @brief Archives old copies of the log file by adding increasing numbers to the filename. 114 */ 115 void LogWriter::archive(int index) 116 { 117 std::string oldPath = this->getArchivedPath(index); 118 119 // see if the file already exists, otherwise return 120 std::ifstream stream(oldPath.c_str()); 121 bool exists = stream.is_open(); 122 stream.close(); 123 124 if (!exists) 125 return; 126 127 if (index < MAX_ARCHIVED_FILES) 128 { 129 // increment the index and archive the file with the next higher index 130 this->archive(++index); 131 132 // create the new path based on the incremented index 133 std::string newPath = this->getArchivedPath(index); 134 135 // move the file 136 std::rename(oldPath.c_str(), newPath.c_str()); 137 } 138 else 139 { 140 // delete the file 141 std::remove(oldPath.c_str()); 142 } 143 } 144 145 /** 146 * @brief Returns the path for archived copies of the logfile (based on the archive index) 147 */ 148 std::string LogWriter::getArchivedPath(int index) const 149 { 150 std::string path = this->getPath(); 151 if (index > 0) 152 path += '.' + multi_cast<std::string>(index); 153 return path; 104 154 } 105 155 -
code/branches/testing/src/libraries/util/output/LogWriter.h
r9542 r9544 76 76 void closeFile(); 77 77 78 void archive(int index = 0); 79 std::string getArchivedPath(int index) const; 80 78 81 std::string filename_; ///< The name of the log-file (without directory) 79 82 std::string directory_; ///< The directory where the log-file resided (without file-name) -
code/branches/testing/test/util/output/LogWriterTest.cc
r9538 r9544 2 2 #include "util/Output.h" 3 3 #include "util/output/LogWriter.h" 4 #include "util/Convert.h" 4 5 5 6 namespace orxonox 6 7 { 8 namespace 9 { 10 class MockLogWriter : public LogWriter 11 { 12 public: 13 virtual void printLine(const std::string& line, OutputLevel level) 14 { this->LogWriter::printLine(line, level); } 15 }; 16 } 17 7 18 // test constructor opens file 8 19 TEST(LogWriterTest, ConstructorOpensFile) … … 15 26 bool fileExists(const std::string& path) 16 27 { 17 std::ifstream stream(path.c_str() , std::fstream::in);18 bool exists = stream.is_open() ;28 std::ifstream stream(path.c_str()); 29 bool exists = stream.is_open() && stream.good(); 19 30 stream.close(); 20 31 return exists; 21 32 } 22 33 23 bool fileSuccessfullyDeleted(const std::string& path) 24 { 25 return std::remove(path.c_str()) == 0; 34 std::string getLineWhichContains(const std::string& path, const std::string& message) 35 { 36 std::ifstream file(path.c_str()); 37 EXPECT_TRUE(file.is_open()); 38 EXPECT_TRUE(file.good()); 39 EXPECT_FALSE(file.eof()); 40 41 while (file.is_open() && file.good() && !file.eof()) 42 { 43 std::string line; 44 std::getline(file, line); 45 46 // see if we find the output and return 47 if (line.find(message) == (line.size() - message.size())) 48 return line; 49 } 50 51 return std::string(); 52 } 53 54 bool fileContains(const std::string& path, const std::string& message) 55 { 56 return !getLineWhichContains(path, message).empty(); 57 } 58 59 bool deleteFile(const std::string& path) 60 { 61 bool result = std::remove(path.c_str()) == 0; 62 EXPECT_FALSE(fileExists(path)); 63 return result; 26 64 } 27 65 … … 31 69 32 70 // cleanup before test 33 fileSuccessfullyDeleted(path);71 deleteFile(path); 34 72 35 73 { … … 41 79 42 80 // cleanup after test 43 EXPECT_TRUE( fileSuccessfullyDeleted(path));81 EXPECT_TRUE(deleteFile(path)); 44 82 } 45 83 … … 61 99 } 62 100 63 { 64 std::ifstream file(path.c_str(), std::fstream::in); 65 ASSERT_TRUE(file.is_open()); 66 ASSERT_TRUE(file.good()); 67 ASSERT_FALSE(file.eof()); 68 69 while (!file.eof()) 70 { 71 std::string line; 72 std::getline(file, line); 73 74 // see if we find the output and return 75 if (line.find("mytestoutput") != std::string::npos) 76 return; 77 } 78 79 // output not found - failure! 80 FAIL(); 81 } 101 EXPECT_TRUE(fileContains(path, "mytestoutput")); 82 102 } 83 103 … … 99 119 } 100 120 101 { 102 std::ifstream file(path.c_str(), std::fstream::in); 103 ASSERT_TRUE(file.is_open()); 104 ASSERT_TRUE(file.good()); 105 ASSERT_FALSE(file.eof()); 106 107 while (!file.eof()) 108 { 109 std::string line; 110 std::getline(file, line); 111 112 // see if we find the output and return 113 if (line.find("myothertestoutput") != std::string::npos) 114 { 115 EXPECT_TRUE(std::isdigit(line[0])); 116 EXPECT_TRUE(std::isdigit(line[1])); 117 EXPECT_EQ(':', line[2]); 118 EXPECT_TRUE(std::isdigit(line[3])); 119 EXPECT_TRUE(std::isdigit(line[4])); 120 EXPECT_EQ(':', line[5]); 121 EXPECT_TRUE(std::isdigit(line[6])); 122 EXPECT_TRUE(std::isdigit(line[7])); 123 EXPECT_EQ(' ', line[8]); 124 return; 125 } 126 } 127 128 // output not found - failure! 129 FAIL(); 130 } 121 std::string line = getLineWhichContains(path, "myothertestoutput"); 122 EXPECT_FALSE(line.empty()); 123 EXPECT_TRUE(std::isdigit(line[0])); 124 EXPECT_TRUE(std::isdigit(line[1])); 125 EXPECT_EQ(':', line[2]); 126 EXPECT_TRUE(std::isdigit(line[3])); 127 EXPECT_TRUE(std::isdigit(line[4])); 128 EXPECT_EQ(':', line[5]); 129 EXPECT_TRUE(std::isdigit(line[6])); 130 EXPECT_TRUE(std::isdigit(line[7])); 131 EXPECT_EQ(' ', line[8]); 132 } 133 134 void deleteAllLogFiles() 135 { 136 std::string path; 137 { 138 LogWriter writer; 139 path = writer.getPath(); 140 } 141 142 deleteFile(path); 143 deleteFile(path + ".1"); 144 deleteFile(path + ".2"); 145 deleteFile(path + ".3"); 146 deleteFile(path + ".4"); 147 deleteFile(path + ".5"); 148 deleteFile(path + ".6"); 149 deleteFile(path + ".7"); 150 deleteFile(path + ".8"); 151 deleteFile(path + ".9"); 152 } 153 154 TEST(LogWriterTest, ArchivesOldLogFile) 155 { 156 deleteAllLogFiles(); 157 158 std::string path; 159 160 { 161 MockLogWriter writer; 162 path = writer.getPath(); 163 writer.printLine("test1", level::message); 164 } 165 166 EXPECT_TRUE(fileExists(path)); 167 EXPECT_TRUE(fileContains(path, "test1")); 168 EXPECT_FALSE(fileExists(path + ".1")); 169 EXPECT_FALSE(fileExists(path + ".2")); 170 EXPECT_FALSE(fileExists(path + ".3")); 171 172 { 173 MockLogWriter writer; 174 writer.printLine("test2", level::message); 175 } 176 177 EXPECT_TRUE(fileExists(path)); 178 EXPECT_TRUE(fileContains(path, "test2")); 179 EXPECT_TRUE(fileExists(path + ".1")); 180 EXPECT_TRUE(fileContains(path + ".1", "test1")); 181 EXPECT_FALSE(fileExists(path + ".2")); 182 EXPECT_FALSE(fileExists(path + ".3")); 183 184 { 185 MockLogWriter writer; 186 writer.printLine("test3", level::message); 187 } 188 189 EXPECT_TRUE(fileExists(path)); 190 EXPECT_TRUE(fileContains(path, "test3")); 191 EXPECT_TRUE(fileExists(path + ".1")); 192 EXPECT_TRUE(fileContains(path + ".1", "test2")); 193 EXPECT_TRUE(fileExists(path + ".2")); 194 EXPECT_TRUE(fileContains(path + ".2", "test1")); 195 EXPECT_FALSE(fileExists(path + ".3")); 196 } 197 198 TEST(LogWriterTest, ArchivesNineLogFiles) 199 { 200 deleteAllLogFiles(); 201 202 std::string path; 203 for (int i = 0; i < 20; ++i) 204 { 205 MockLogWriter writer; 206 path = writer.getPath(); 207 writer.printLine("test" + multi_cast<std::string>(i), level::message); 208 } 209 210 EXPECT_TRUE(fileContains(path, "test19")); 211 EXPECT_TRUE(fileContains(path + ".1", "test18")); 212 EXPECT_TRUE(fileContains(path + ".2", "test17")); 213 EXPECT_TRUE(fileContains(path + ".3", "test16")); 214 EXPECT_TRUE(fileContains(path + ".4", "test15")); 215 EXPECT_TRUE(fileContains(path + ".5", "test14")); 216 EXPECT_TRUE(fileContains(path + ".6", "test13")); 217 EXPECT_TRUE(fileContains(path + ".7", "test12")); 218 EXPECT_TRUE(fileContains(path + ".8", "test11")); 219 EXPECT_TRUE(fileContains(path + ".9", "test10")); 220 EXPECT_FALSE(fileExists(path + ".10")); 221 EXPECT_FALSE(fileExists(path + ".11")); 131 222 } 132 223 }
Note: See TracChangeset
for help on using the changeset viewer.