Changeset 9550 for code/trunk/src/libraries/util/output/LogWriter.cc
- Timestamp:
- Mar 12, 2013, 11:13:03 PM (11 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/testing (added) merged: 9015,9017,9020-9022,9025-9026,9047,9076-9078,9114-9115,9221-9226,9356,9473-9480,9524,9529-9531,9533-9545,9547-9549
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/util/output/LogWriter.cc
r8858 r9550 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. … … 58 61 // get the path for a temporary file, depending on the system 59 62 #ifdef ORXONOX_PLATFORM_WINDOWS 60 this-> path_ = getenv("TEMP");63 this->directory_ = getenv("TEMP"); 61 64 #else 62 this-> path_ = "/tmp";65 this->directory_ = "/tmp"; 63 66 #endif 64 this->bDefaultPath_ = true; 67 68 // send a message to the user so that he can find the file in the case of a crash. 69 OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + this->getPath()); 65 70 66 71 this->openFile(); … … 76 81 77 82 /** 78 @brief Returns the only existing instance of this class.79 */80 /*static*/ LogWriter& LogWriter::getInstance()81 {82 static LogWriter instance;83 return instance;84 }85 86 /**87 83 @brief Opens the log-file in order to write output to it. 88 84 */ 89 85 void LogWriter::openFile() 90 86 { 91 // get the full file-name 92 std::string name = this->path_ + '/' + this->filename_; 93 94 // if we open the log file in the default directory, send a message to the user so that he can find the file in the case of a crash. 95 if (this->bDefaultPath_) 96 OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + name); 87 // archive the old log file 88 this->archive(); 97 89 98 90 // open the file 99 this->file_.open( name.c_str(), std::fstream::out);91 this->file_.open(this->getPath().c_str(), std::fstream::out); 100 92 101 93 // check if it worked and print some output … … 119 111 120 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; 154 } 155 156 /** 121 157 @brief Changes the path of the log-file. Re-writes the log-file by using MemoryWriter. 122 158 */ 123 void LogWriter::setLog Path(const std::string& path)159 void LogWriter::setLogDirectory(const std::string& directory) 124 160 { 125 161 // notify about the change of the log-file (because the old file will no longer be updated) 126 OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this-> path_ + "\nto " + path);162 OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->directory_ + "\nto " + directory); 127 163 128 164 // close the old file, update the path and open the new file 129 165 this->closeFile(); 130 this->path_ = path; 131 this->bDefaultPath_ = false; 166 this->directory_ = directory; 132 167 this->openFile(); 133 168 134 169 // request old output from MemoryWriter 135 MemoryWriter::getInstance().resendOutput(this); 170 if (OutputManager::getInstance().getMemoryWriter()) 171 OutputManager::getInstance().getMemoryWriter()->resendOutput(this); 136 172 } 137 173
Note: See TracChangeset
for help on using the changeset viewer.