| [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 |
|---|
| [9538] | 60 | this->directory_ = getenv("TEMP"); |
|---|
| [8774] | 61 | #else |
|---|
| [9538] | 62 | this->directory_ = "/tmp"; |
|---|
| [8774] | 63 | #endif |
|---|
| 64 | |
|---|
| [9538] | 65 | // send a message to the user so that he can find the file in the case of a crash. |
|---|
| 66 | OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + this->getPath()); |
|---|
| 67 | |
|---|
| [8774] | 68 | this->openFile(); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| [8848] | 71 | /** |
|---|
| 72 | @brief Destructor, closes the log-file. |
|---|
| 73 | */ |
|---|
| [8774] | 74 | LogWriter::~LogWriter() |
|---|
| 75 | { |
|---|
| 76 | this->closeFile(); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| [8848] | 79 | /** |
|---|
| 80 | @brief Opens the log-file in order to write output to it. |
|---|
| 81 | */ |
|---|
| [8774] | 82 | void LogWriter::openFile() |
|---|
| 83 | { |
|---|
| [8848] | 84 | // open the file |
|---|
| [9538] | 85 | this->file_.open(this->getPath().c_str(), std::fstream::out); |
|---|
| [8774] | 86 | |
|---|
| [8848] | 87 | // check if it worked and print some output |
|---|
| [8774] | 88 | if (this->file_.is_open()) |
|---|
| [8795] | 89 | this->printLine("Log file opened", level::none); |
|---|
| [8774] | 90 | else |
|---|
| [8831] | 91 | OutputManager::getInstance().pushMessage(level::user_warning, context::undefined(), "Failed to open log file. File logging disabled."); |
|---|
| [8774] | 92 | } |
|---|
| 93 | |
|---|
| [8848] | 94 | /** |
|---|
| 95 | @brief Closes the log-file. |
|---|
| 96 | */ |
|---|
| [8774] | 97 | void LogWriter::closeFile() |
|---|
| 98 | { |
|---|
| 99 | if (this->file_.is_open()) |
|---|
| 100 | { |
|---|
| [8795] | 101 | this->printLine("Log file closed", level::none); |
|---|
| [8774] | 102 | this->file_.close(); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| [8848] | 106 | /** |
|---|
| 107 | @brief Changes the path of the log-file. Re-writes the log-file by using MemoryWriter. |
|---|
| 108 | */ |
|---|
| [9538] | 109 | void LogWriter::setLogDirectory(const std::string& directory) |
|---|
| [8774] | 110 | { |
|---|
| [8848] | 111 | // notify about the change of the log-file (because the old file will no longer be updated) |
|---|
| [9538] | 112 | OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->directory_ + "\nto " + directory); |
|---|
| [8774] | 113 | |
|---|
| [8848] | 114 | // close the old file, update the path and open the new file |
|---|
| [8774] | 115 | this->closeFile(); |
|---|
| [9538] | 116 | this->directory_ = directory; |
|---|
| [8774] | 117 | this->openFile(); |
|---|
| 118 | |
|---|
| [8848] | 119 | // request old output from MemoryWriter |
|---|
| [9540] | 120 | if (&OutputManager::getInstance().getMemoryWriter()) |
|---|
| 121 | OutputManager::getInstance().getMemoryWriter().resendOutput(this); |
|---|
| [8774] | 122 | } |
|---|
| 123 | |
|---|
| [8848] | 124 | /** |
|---|
| 125 | @brief Inherited function from BaseWriter, writers output together with a timestamp to the log-file. |
|---|
| 126 | */ |
|---|
| [8795] | 127 | void LogWriter::printLine(const std::string& line, OutputLevel) |
|---|
| [8774] | 128 | { |
|---|
| [8794] | 129 | if (!this->file_.is_open()) |
|---|
| 130 | return; |
|---|
| 131 | |
|---|
| [8848] | 132 | // get the current time |
|---|
| [8774] | 133 | time_t rawtime; |
|---|
| 134 | struct tm* timeinfo; |
|---|
| 135 | time(&rawtime); |
|---|
| 136 | timeinfo = localtime(&rawtime); |
|---|
| 137 | |
|---|
| [8848] | 138 | // print timestamp and output line to the log file |
|---|
| [8774] | 139 | this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' << |
|---|
| 140 | (timeinfo->tm_min < 10 ? "0" : "") << timeinfo->tm_min << ':' << |
|---|
| 141 | (timeinfo->tm_sec < 10 ? "0" : "") << timeinfo->tm_sec << ' ' << line << std::endl; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|