| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Fabian 'x3n' Landau | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      Reto Grieder | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 | @file | 
|---|
| 31 | @brief | 
|---|
| 32 |     Definition of classes related to output (logging). | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 | #include "OutputHandler.h" | 
|---|
| 36 |  | 
|---|
| 37 | #include <algorithm> | 
|---|
| 38 | #include <ctime> | 
|---|
| 39 | #include <cstdlib> | 
|---|
| 40 | #include <fstream> | 
|---|
| 41 | #include <iostream> | 
|---|
| 42 | #include <sstream> | 
|---|
| 43 |  | 
|---|
| 44 | #include "Debug.h" | 
|---|
| 45 |  | 
|---|
| 46 | namespace orxonox | 
|---|
| 47 | { | 
|---|
| 48 |     //! How the log file shall be named on the filesystem | 
|---|
| 49 |     const std::string logFileBaseName_g = "orxonox.log"; | 
|---|
| 50 |  | 
|---|
| 51 |     ///////////////////////// | 
|---|
| 52 |     ///// LogFileWriter ///// | 
|---|
| 53 |     ///////////////////////// | 
|---|
| 54 |     /** | 
|---|
| 55 |     @brief | 
|---|
| 56 |         Writes the output to the log file. | 
|---|
| 57 |     @note | 
|---|
| 58 |         As long as the correct log path is not yet known (for pre main code), the | 
|---|
| 59 |         LogFileWriter will write to a temporary file in /temp (Unix) or %TEMP% (Windows). | 
|---|
| 60 |         As soon as you set the correct path setLogPath the content of the temporary file | 
|---|
| 61 |         is read and put into the new file as well. | 
|---|
| 62 |     */ | 
|---|
| 63 |     class LogFileWriter : public OutputListener | 
|---|
| 64 |     { | 
|---|
| 65 |     public: | 
|---|
| 66 |         /** | 
|---|
| 67 |         @brief | 
|---|
| 68 |             Gets temporary log path and starts the log file | 
|---|
| 69 |         */ | 
|---|
| 70 |         LogFileWriter() | 
|---|
| 71 |             : OutputListener(OutputHandler::logFileOutputListenerName_s) | 
|---|
| 72 |         { | 
|---|
| 73 |             // Get path for a temporary file | 
|---|
| 74 | #ifdef ORXONOX_PLATFORM_WINDOWS | 
|---|
| 75 |             char* pTempDir = getenv("TEMP"); | 
|---|
| 76 |             this->logFilename_ = std::string(pTempDir) + '/' + logFileBaseName_g; | 
|---|
| 77 | #else | 
|---|
| 78 |             this->logFilename_ = std::string("/tmp/") + logFileBaseName_g; | 
|---|
| 79 | #endif | 
|---|
| 80 |  | 
|---|
| 81 |             // Get current time | 
|---|
| 82 |             time_t rawtime; | 
|---|
| 83 |             struct tm* timeinfo; | 
|---|
| 84 |             time(&rawtime); | 
|---|
| 85 |             timeinfo = localtime(&rawtime); | 
|---|
| 86 |  | 
|---|
| 87 |             this->logFile_.open(this->logFilename_.c_str(), std::fstream::out); | 
|---|
| 88 |             this->logFile_ << "Started log on " << asctime(timeinfo) << std::endl; | 
|---|
| 89 |             this->logFile_.flush(); | 
|---|
| 90 |  | 
|---|
| 91 |             this->outputStream_ = &this->logFile_; | 
|---|
| 92 |         } | 
|---|
| 93 |  | 
|---|
| 94 |         //! Closes the log file | 
|---|
| 95 |         ~LogFileWriter() | 
|---|
| 96 |         { | 
|---|
| 97 |             this->logFile_ << "Closed log" << std::endl; | 
|---|
| 98 |             this->logFile_.close(); | 
|---|
| 99 |         } | 
|---|
| 100 |  | 
|---|
| 101 |         //! Changes the log path | 
|---|
| 102 |         void setLogPath(const std::string& path) | 
|---|
| 103 |         { | 
|---|
| 104 |             this->logFile_.close(); | 
|---|
| 105 |             // Read old file into a buffer | 
|---|
| 106 |             std::ifstream old(this->logFilename_.c_str()); | 
|---|
| 107 |             this->logFilename_ = path + logFileBaseName_g; | 
|---|
| 108 |             // Open the new file and feed it the content of the old one | 
|---|
| 109 |             this->logFile_.open(this->logFilename_.c_str(), std::fstream::out); | 
|---|
| 110 |             this->logFile_ << old.rdbuf(); | 
|---|
| 111 |             this->logFile_.flush(); | 
|---|
| 112 |             old.close(); | 
|---|
| 113 |         } | 
|---|
| 114 |  | 
|---|
| 115 |     private: | 
|---|
| 116 |         std::ofstream logFile_;     //!< File handle for the log file | 
|---|
| 117 |         std::string   logFilename_; //!< Filename of the log file | 
|---|
| 118 |     }; | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 |     ///////////////////////// | 
|---|
| 122 |     ///// ConsoleWriter ///// | 
|---|
| 123 |     ///////////////////////// | 
|---|
| 124 |     /** | 
|---|
| 125 |     @brief | 
|---|
| 126 |         Writes the output to std::cout. | 
|---|
| 127 |     @note | 
|---|
| 128 |         This listener will usually be disable once an actual shell with console is instantiated. | 
|---|
| 129 |     */ | 
|---|
| 130 |     class ConsoleWriter : public OutputListener | 
|---|
| 131 |     { | 
|---|
| 132 |     public: | 
|---|
| 133 |         //! Only assigns the output stream with std::cout | 
|---|
| 134 |         ConsoleWriter() | 
|---|
| 135 |             : OutputListener("consoleLog") | 
|---|
| 136 |         { | 
|---|
| 137 |             this->outputStream_ = &std::cout; | 
|---|
| 138 |         } | 
|---|
| 139 |     }; | 
|---|
| 140 |  | 
|---|
| 141 |  | 
|---|
| 142 |     /////////////////////////// | 
|---|
| 143 |     ///// MemoryLogWriter ///// | 
|---|
| 144 |     /////////////////////////// | 
|---|
| 145 |     /** | 
|---|
| 146 |     @brief | 
|---|
| 147 |         OutputListener that writes all the output piece by piece to an array | 
|---|
| 148 |         associated with the corresponding output level. | 
|---|
| 149 |     @note | 
|---|
| 150 |         Only output below or equal to the current soft debug level is written | 
|---|
| 151 |         to minimise huge arrays for the normal run. | 
|---|
| 152 |     */ | 
|---|
| 153 |     class MemoryLogWriter : public OutputListener | 
|---|
| 154 |     { | 
|---|
| 155 |     public: | 
|---|
| 156 |         friend class OutputHandler; | 
|---|
| 157 |  | 
|---|
| 158 |         /** | 
|---|
| 159 |         @brief | 
|---|
| 160 |             Sets the right soft debug level and registers itself | 
|---|
| 161 |         */ | 
|---|
| 162 |         MemoryLogWriter() | 
|---|
| 163 |             : OutputListener("memoryLog") | 
|---|
| 164 |         { | 
|---|
| 165 |             this->outputStream_ = &this->buffer_; | 
|---|
| 166 |         } | 
|---|
| 167 |  | 
|---|
| 168 |         //! Pushed the just written output to the internal array | 
|---|
| 169 |         void outputChanged(int level) | 
|---|
| 170 |         { | 
|---|
| 171 |             if (!this->buffer_.str().empty()) | 
|---|
| 172 |             { | 
|---|
| 173 |                 // Read ostringstream and store it | 
|---|
| 174 |                 this->output_.push_back(std::make_pair(level, this->buffer_.str())); | 
|---|
| 175 |                 // Clear content and flags | 
|---|
| 176 |                 this->buffer_.str(std::string()); | 
|---|
| 177 |             } | 
|---|
| 178 |             this->buffer_.clear(); | 
|---|
| 179 |         } | 
|---|
| 180 |  | 
|---|
| 181 |     private: | 
|---|
| 182 |         std::ostringstream                        buffer_; //!< Stream object used to process the output | 
|---|
| 183 |         std::vector<std::pair<int, std::string> > output_; //!< Vector containing ALL output | 
|---|
| 184 |     }; | 
|---|
| 185 |  | 
|---|
| 186 |  | 
|---|
| 187 |     ///////////////////////// | 
|---|
| 188 |     ///// OutputHandler ///// | 
|---|
| 189 |     ///////////////////////// | 
|---|
| 190 |     const std::string OutputHandler::logFileOutputListenerName_s = "logFile"; | 
|---|
| 191 |           int         OutputHandler::softDebugLevel_s = hardDebugLevel; | 
|---|
| 192 |  | 
|---|
| 193 |     //! Creates the LogFileWriter and the MemoryLogWriter | 
|---|
| 194 |     OutputHandler::OutputHandler() | 
|---|
| 195 |         : outputLevel_(OutputLevel::Verbose) | 
|---|
| 196 |     { | 
|---|
| 197 | #ifdef ORXONOX_RELEASE | 
|---|
| 198 |         const OutputLevel::Value defaultLevelConsole = OutputLevel::Error; | 
|---|
| 199 |         const OutputLevel::Value defaultLevelLogFile = OutputLevel::Info; | 
|---|
| 200 | #else | 
|---|
| 201 |         const OutputLevel::Value defaultLevelConsole = OutputLevel::Info; | 
|---|
| 202 |         const OutputLevel::Value defaultLevelLogFile = OutputLevel::Debug; | 
|---|
| 203 | #endif | 
|---|
| 204 |  | 
|---|
| 205 |         this->logFile_ = new LogFileWriter(); | 
|---|
| 206 |         // Use default level until we get the configValue from the Core | 
|---|
| 207 |         this->logFile_->softDebugLevel_ = defaultLevelLogFile; | 
|---|
| 208 |         this->registerOutputListener(this->logFile_); | 
|---|
| 209 |  | 
|---|
| 210 |         this->consoleWriter_ = new ConsoleWriter(); | 
|---|
| 211 |         this->consoleWriter_->softDebugLevel_ = defaultLevelConsole; | 
|---|
| 212 |         this->registerOutputListener(this->consoleWriter_); | 
|---|
| 213 |  | 
|---|
| 214 |         this->output_ = new MemoryLogWriter(); | 
|---|
| 215 |         // We capture as much input as the listener with the highest level | 
|---|
| 216 |         this->output_->softDebugLevel_ = getSoftDebugLevel(); | 
|---|
| 217 |         this->registerOutputListener(this->output_); | 
|---|
| 218 |     } | 
|---|
| 219 |  | 
|---|
| 220 |     //! Destroys the LogFileWriter and the MemoryLogWriter | 
|---|
| 221 |     OutputHandler::~OutputHandler() | 
|---|
| 222 |     { | 
|---|
| 223 |         delete this->logFile_; | 
|---|
| 224 |         delete this->consoleWriter_; | 
|---|
| 225 |         delete this->output_; | 
|---|
| 226 |     } | 
|---|
| 227 |  | 
|---|
| 228 |     OutputHandler& OutputHandler::getInstance() | 
|---|
| 229 |     { | 
|---|
| 230 |         static OutputHandler orxout; | 
|---|
| 231 |         return orxout; | 
|---|
| 232 |     } | 
|---|
| 233 |  | 
|---|
| 234 |     void OutputHandler::registerOutputListener(OutputListener* listener) | 
|---|
| 235 |     { | 
|---|
| 236 |         for (std::list<OutputListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
| 237 |         { | 
|---|
| 238 |             if ((*it)->name_ == listener->name_) | 
|---|
| 239 |             { | 
|---|
| 240 |                 COUT(2) << "OutputHandler, Warning: Trying to register two listeners with the same name!" << std::endl; | 
|---|
| 241 |                 return; | 
|---|
| 242 |             } | 
|---|
| 243 |         } | 
|---|
| 244 |         this->listeners_.push_back(listener); | 
|---|
| 245 |         // Update global soft debug level | 
|---|
| 246 |         this->setSoftDebugLevel(listener->getOutputListenerName(), listener->getSoftDebugLevel()); | 
|---|
| 247 |     } | 
|---|
| 248 |  | 
|---|
| 249 |     void OutputHandler::unregisterOutputListener(OutputListener* listener) | 
|---|
| 250 |     { | 
|---|
| 251 |         this->listeners_.remove(listener); | 
|---|
| 252 |     } | 
|---|
| 253 |  | 
|---|
| 254 |     void OutputHandler::setLogPath(const std::string& path) | 
|---|
| 255 |     { | 
|---|
| 256 |         this->logFile_->setLogPath(path); | 
|---|
| 257 |     } | 
|---|
| 258 |  | 
|---|
| 259 |     void OutputHandler::disableCout() | 
|---|
| 260 |     { | 
|---|
| 261 |         this->unregisterOutputListener(this->consoleWriter_); | 
|---|
| 262 |     } | 
|---|
| 263 |  | 
|---|
| 264 |     void OutputHandler::enableCout() | 
|---|
| 265 |     { | 
|---|
| 266 |         this->registerOutputListener(this->consoleWriter_); | 
|---|
| 267 |     } | 
|---|
| 268 |  | 
|---|
| 269 |     OutputHandler::OutputVectorIterator OutputHandler::getOutputVectorBegin() const | 
|---|
| 270 |     { | 
|---|
| 271 |         return this->output_->output_.begin(); | 
|---|
| 272 |     } | 
|---|
| 273 |  | 
|---|
| 274 |     OutputHandler::OutputVectorIterator OutputHandler::getOutputVectorEnd() const | 
|---|
| 275 |     { | 
|---|
| 276 |         return this->output_->output_.end(); | 
|---|
| 277 |     } | 
|---|
| 278 |  | 
|---|
| 279 |     int OutputHandler::getSoftDebugLevel(const std::string& name) const | 
|---|
| 280 |     { | 
|---|
| 281 |         for (std::list<OutputListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
| 282 |         { | 
|---|
| 283 |             if ((*it)->name_ == name) | 
|---|
| 284 |                 return (*it)->softDebugLevel_; | 
|---|
| 285 |         } | 
|---|
| 286 |         return -1; | 
|---|
| 287 |     } | 
|---|
| 288 |  | 
|---|
| 289 |     void OutputHandler::setSoftDebugLevel(const std::string& name, int level) | 
|---|
| 290 |     { | 
|---|
| 291 |         int globalSoftDebugLevel = -1; | 
|---|
| 292 |         for (std::list<OutputListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
| 293 |         { | 
|---|
| 294 |             if ((*it)->name_ == name) | 
|---|
| 295 |                 (*it)->softDebugLevel_ = level; | 
|---|
| 296 |             if ((*it)->softDebugLevel_ > globalSoftDebugLevel) | 
|---|
| 297 |                 globalSoftDebugLevel = (*it)->softDebugLevel_; | 
|---|
| 298 |         } | 
|---|
| 299 |         // Update global soft debug level | 
|---|
| 300 |         OutputHandler::softDebugLevel_s = globalSoftDebugLevel; | 
|---|
| 301 |     } | 
|---|
| 302 | } | 
|---|