[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 Opens the log-file in order to write output to it. |
---|
| 79 | */ |
---|
[8774] | 80 | void LogWriter::openFile() |
---|
| 81 | { |
---|
[8848] | 82 | // get the full file-name |
---|
[8774] | 83 | std::string name = this->path_ + '/' + this->filename_; |
---|
| 84 | |
---|
[8848] | 85 | // 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] | 86 | if (this->bDefaultPath_) |
---|
[8831] | 87 | OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + name); |
---|
[8774] | 88 | |
---|
[8848] | 89 | // open the file |
---|
[8774] | 90 | this->file_.open(name.c_str(), std::fstream::out); |
---|
| 91 | |
---|
[8848] | 92 | // check if it worked and print some output |
---|
[8774] | 93 | if (this->file_.is_open()) |
---|
[8795] | 94 | this->printLine("Log file opened", level::none); |
---|
[8774] | 95 | else |
---|
[8831] | 96 | OutputManager::getInstance().pushMessage(level::user_warning, context::undefined(), "Failed to open log file. File logging disabled."); |
---|
[8774] | 97 | } |
---|
| 98 | |
---|
[8848] | 99 | /** |
---|
| 100 | @brief Closes the log-file. |
---|
| 101 | */ |
---|
[8774] | 102 | void LogWriter::closeFile() |
---|
| 103 | { |
---|
| 104 | if (this->file_.is_open()) |
---|
| 105 | { |
---|
[8795] | 106 | this->printLine("Log file closed", level::none); |
---|
[8774] | 107 | this->file_.close(); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
[8848] | 111 | /** |
---|
| 112 | @brief Changes the path of the log-file. Re-writes the log-file by using MemoryWriter. |
---|
| 113 | */ |
---|
[8774] | 114 | void LogWriter::setLogPath(const std::string& path) |
---|
| 115 | { |
---|
[8848] | 116 | // notify about the change of the log-file (because the old file will no longer be updated) |
---|
[8831] | 117 | OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->path_ + "\nto " + path); |
---|
[8774] | 118 | |
---|
[8848] | 119 | // close the old file, update the path and open the new file |
---|
[8774] | 120 | this->closeFile(); |
---|
| 121 | this->path_ = path; |
---|
| 122 | this->bDefaultPath_ = false; |
---|
| 123 | this->openFile(); |
---|
| 124 | |
---|
[8848] | 125 | // request old output from MemoryWriter |
---|
[9536] | 126 | OutputManager::getInstance().getMemoryWriter().resendOutput(this); |
---|
[8774] | 127 | } |
---|
| 128 | |
---|
[8848] | 129 | /** |
---|
| 130 | @brief Inherited function from BaseWriter, writers output together with a timestamp to the log-file. |
---|
| 131 | */ |
---|
[8795] | 132 | void LogWriter::printLine(const std::string& line, OutputLevel) |
---|
[8774] | 133 | { |
---|
[8794] | 134 | if (!this->file_.is_open()) |
---|
| 135 | return; |
---|
| 136 | |
---|
[8848] | 137 | // get the current time |
---|
[8774] | 138 | time_t rawtime; |
---|
| 139 | struct tm* timeinfo; |
---|
| 140 | time(&rawtime); |
---|
| 141 | timeinfo = localtime(&rawtime); |
---|
| 142 | |
---|
[8848] | 143 | // print timestamp and output line to the log file |
---|
[8774] | 144 | this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' << |
---|
| 145 | (timeinfo->tm_min < 10 ? "0" : "") << timeinfo->tm_min << ':' << |
---|
| 146 | (timeinfo->tm_sec < 10 ? "0" : "") << timeinfo->tm_sec << ' ' << line << std::endl; |
---|
| 147 | } |
---|
| 148 | } |
---|