| [8774] | 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 |  | 
|---|
| [8848] | 29 | /** | 
|---|
 | 30 |     @file | 
|---|
 | 31 |     @brief Implementation of the LogWriter singleton. | 
|---|
 | 32 | */ | 
|---|
 | 33 |  | 
|---|
| [8774] | 34 | #include "LogWriter.h" | 
|---|
 | 35 |  | 
|---|
 | 36 | #include <ctime> | 
|---|
| [8833] | 37 | #include <cstdlib> | 
|---|
| [8774] | 38 |  | 
|---|
 | 39 | #include "OutputManager.h" | 
|---|
 | 40 | #include "MemoryWriter.h" | 
|---|
 | 41 |  | 
|---|
 | 42 | namespace orxonox | 
|---|
 | 43 | { | 
|---|
| [8848] | 44 |     /** | 
|---|
 | 45 |         @brief Constructor, initializes the desired output levels and the name and path of the log-file, and opens the log-file. | 
|---|
 | 46 |  | 
|---|
 | 47 |         By default, LogWriter receives all output up to level::internal_info. | 
|---|
 | 48 |         The log-file has a default name which usually doesn't change. The path | 
|---|
 | 49 |         is initialized with a temporary directory, depending on the system, | 
|---|
 | 50 |         and can be changed later. | 
|---|
 | 51 |     */ | 
|---|
| [8799] | 52 |     LogWriter::LogWriter() : BaseWriter("Log") | 
|---|
| [8774] | 53 |     { | 
|---|
 | 54 |         this->setLevelMax(level::internal_info); | 
|---|
 | 55 |  | 
|---|
| [8799] | 56 |         this->filename_ = "orxonox.log"; | 
|---|
| [8774] | 57 |  | 
|---|
| [8848] | 58 |         // get the path for a temporary file, depending on the system | 
|---|
| [8774] | 59 | #ifdef ORXONOX_PLATFORM_WINDOWS | 
|---|
 | 60 |         this->path_ = getenv("TEMP"); | 
|---|
 | 61 | #else | 
|---|
 | 62 |         this->path_ = "/tmp"; | 
|---|
 | 63 | #endif | 
|---|
 | 64 |         this->bDefaultPath_ = true; | 
|---|
 | 65 |  | 
|---|
 | 66 |         this->openFile(); | 
|---|
 | 67 |     } | 
|---|
 | 68 |  | 
|---|
| [8848] | 69 |     /** | 
|---|
 | 70 |         @brief Destructor, closes the log-file. | 
|---|
 | 71 |     */ | 
|---|
| [8774] | 72 |     LogWriter::~LogWriter() | 
|---|
 | 73 |     { | 
|---|
 | 74 |         this->closeFile(); | 
|---|
 | 75 |     } | 
|---|
 | 76 |  | 
|---|
| [8848] | 77 |     /** | 
|---|
 | 78 |         @brief Returns the only existing instance of this class. | 
|---|
 | 79 |     */ | 
|---|
| [8774] | 80 |     /*static*/ LogWriter& LogWriter::getInstance() | 
|---|
 | 81 |     { | 
|---|
 | 82 |         static LogWriter instance; | 
|---|
 | 83 |         return instance; | 
|---|
 | 84 |     } | 
|---|
 | 85 |  | 
|---|
| [8848] | 86 |     /** | 
|---|
 | 87 |         @brief Opens the log-file in order to write output to it. | 
|---|
 | 88 |     */ | 
|---|
| [8774] | 89 |     void LogWriter::openFile() | 
|---|
 | 90 |     { | 
|---|
| [8848] | 91 |         // get the full file-name | 
|---|
| [8774] | 92 |         std::string name = this->path_ + '/' + this->filename_; | 
|---|
 | 93 |  | 
|---|
| [8848] | 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. | 
|---|
| [8774] | 95 |         if (this->bDefaultPath_) | 
|---|
| [8831] | 96 |             OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + name); | 
|---|
| [8774] | 97 |  | 
|---|
| [8848] | 98 |         // open the file | 
|---|
| [8774] | 99 |         this->file_.open(name.c_str(), std::fstream::out); | 
|---|
 | 100 |  | 
|---|
| [8848] | 101 |         // check if it worked and print some output | 
|---|
| [8774] | 102 |         if (this->file_.is_open()) | 
|---|
| [8795] | 103 |             this->printLine("Log file opened", level::none); | 
|---|
| [8774] | 104 |         else | 
|---|
| [8831] | 105 |             OutputManager::getInstance().pushMessage(level::user_warning, context::undefined(), "Failed to open log file. File logging disabled."); | 
|---|
| [8774] | 106 |     } | 
|---|
 | 107 |  | 
|---|
| [8848] | 108 |     /** | 
|---|
 | 109 |         @brief Closes the log-file. | 
|---|
 | 110 |     */ | 
|---|
| [8774] | 111 |     void LogWriter::closeFile() | 
|---|
 | 112 |     { | 
|---|
 | 113 |         if (this->file_.is_open()) | 
|---|
 | 114 |         { | 
|---|
| [8795] | 115 |             this->printLine("Log file closed", level::none); | 
|---|
| [8774] | 116 |             this->file_.close(); | 
|---|
 | 117 |         } | 
|---|
 | 118 |     } | 
|---|
 | 119 |  | 
|---|
| [8848] | 120 |     /** | 
|---|
 | 121 |         @brief Changes the path of the log-file. Re-writes the log-file by using MemoryWriter. | 
|---|
 | 122 |     */ | 
|---|
| [8774] | 123 |     void LogWriter::setLogPath(const std::string& path) | 
|---|
 | 124 |     { | 
|---|
| [8848] | 125 |         // notify about the change of the log-file (because the old file will no longer be updated) | 
|---|
| [8831] | 126 |         OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->path_ + "\nto " + path); | 
|---|
| [8774] | 127 |  | 
|---|
| [8848] | 128 |         // close the old file, update the path and open the new file | 
|---|
| [8774] | 129 |         this->closeFile(); | 
|---|
 | 130 |         this->path_ = path; | 
|---|
 | 131 |         this->bDefaultPath_ = false; | 
|---|
 | 132 |         this->openFile(); | 
|---|
 | 133 |  | 
|---|
| [8848] | 134 |         // request old output from MemoryWriter | 
|---|
| [8774] | 135 |         MemoryWriter::getInstance().resendOutput(this); | 
|---|
 | 136 |     } | 
|---|
 | 137 |  | 
|---|
| [8848] | 138 |     /** | 
|---|
 | 139 |         @brief Inherited function from BaseWriter, writers output together with a timestamp to the log-file. | 
|---|
 | 140 |     */ | 
|---|
| [8795] | 141 |     void LogWriter::printLine(const std::string& line, OutputLevel) | 
|---|
| [8774] | 142 |     { | 
|---|
| [8794] | 143 |         if (!this->file_.is_open()) | 
|---|
 | 144 |             return; | 
|---|
 | 145 |  | 
|---|
| [8848] | 146 |         // get the current time | 
|---|
| [8774] | 147 |         time_t rawtime; | 
|---|
 | 148 |         struct tm* timeinfo; | 
|---|
 | 149 |         time(&rawtime); | 
|---|
 | 150 |         timeinfo = localtime(&rawtime); | 
|---|
 | 151 |  | 
|---|
| [8848] | 152 |         // print timestamp and output line to the log file | 
|---|
| [8774] | 153 |         this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' << | 
|---|
 | 154 |                        (timeinfo->tm_min  < 10 ? "0" : "") << timeinfo->tm_min  << ':' << | 
|---|
 | 155 |                        (timeinfo->tm_sec  < 10 ? "0" : "") << timeinfo->tm_sec  << ' ' << line << std::endl; | 
|---|
 | 156 |     } | 
|---|
 | 157 | } | 
|---|