[1505] | 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 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
[2171] | 30 | @file |
---|
[1505] | 31 | @brief Implementation of the OutputHandler class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "OutputHandler.h" |
---|
[2087] | 35 | #include <time.h> |
---|
[1505] | 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
| 39 | /** |
---|
| 40 | @brief Constructor: Opens the logfile and writes the first line. |
---|
| 41 | @param logfilename The name of the logfile |
---|
| 42 | */ |
---|
| 43 | OutputHandler::OutputHandler(const std::string& logfilename) |
---|
| 44 | { |
---|
[1586] | 45 | this->outputBuffer_ = &this->fallbackBuffer_; |
---|
| 46 | this->softDebugLevel_[0] = this->softDebugLevel_[1] = this->softDebugLevel_[2] = this->softDebugLevel_[3] = 2; |
---|
[1505] | 47 | this->logfilename_ = logfilename; |
---|
| 48 | this->logfile_.open(this->logfilename_.c_str(), std::fstream::out); |
---|
[2087] | 49 | |
---|
| 50 | time_t rawtime; |
---|
| 51 | struct tm* timeinfo; |
---|
| 52 | time(&rawtime); |
---|
| 53 | timeinfo = localtime(&rawtime); |
---|
| 54 | |
---|
| 55 | this->logfile_ << "Started log at " << asctime(timeinfo) << std::endl; |
---|
[1505] | 56 | this->logfile_.flush(); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | @brief Destructor: Writes the last line to the logfile and closes it. |
---|
| 61 | */ |
---|
| 62 | OutputHandler::~OutputHandler() |
---|
| 63 | { |
---|
| 64 | this->logfile_ << "Closed log" << std::endl; |
---|
| 65 | this->logfile_.close(); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | @brief Returns a reference to the only existing instance of the OutputHandler class. |
---|
| 70 | @return The instance |
---|
| 71 | */ |
---|
| 72 | OutputHandler& OutputHandler::getOutStream() |
---|
| 73 | { |
---|
| 74 | static OutputHandler orxout("orxonox.log"); |
---|
| 75 | return orxout; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
[1586] | 79 | @brief Sets the soft debug level for a given output device. |
---|
| 80 | @param device The output device |
---|
| 81 | @param level The debug level |
---|
| 82 | */ |
---|
| 83 | void OutputHandler::setSoftDebugLevel(OutputHandler::OutputDevice device, int level) |
---|
| 84 | { |
---|
| 85 | OutputHandler::getOutStream().softDebugLevel_[(unsigned int)device] = level; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
[1505] | 89 | @brief Returns the soft debug level for a given output device. |
---|
| 90 | @param device The output device |
---|
| 91 | @return The debug level |
---|
| 92 | */ |
---|
| 93 | int OutputHandler::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
| 94 | { |
---|
[1586] | 95 | return OutputHandler::getOutStream().softDebugLevel_[(unsigned int)device]; |
---|
[1505] | 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
[1586] | 99 | @brief Sets the OutputBuffer, representing the third output stream. |
---|
| 100 | @param buffer The OutputBuffer |
---|
[1505] | 101 | */ |
---|
[1586] | 102 | void OutputHandler::setOutputBuffer(OutputBuffer& buffer) |
---|
[1505] | 103 | { |
---|
[1586] | 104 | buffer.getStream() >> this->outputBuffer_->getStream().rdbuf(); |
---|
| 105 | this->outputBuffer_ = &buffer; |
---|
[1505] | 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
| 110 | @param sb The streambuffer that should be shown in the console |
---|
| 111 | @return A reference to the OutputHandler itself |
---|
| 112 | */ |
---|
| 113 | OutputHandler& OutputHandler::operator<<(std::streambuf* sb) |
---|
| 114 | { |
---|
| 115 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 116 | std::cout << sb; |
---|
| 117 | |
---|
| 118 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 119 | { |
---|
| 120 | this->logfile_ << sb; |
---|
| 121 | this->logfile_.flush(); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
[1586] | 125 | (*this->outputBuffer_) << sb; |
---|
[1505] | 126 | |
---|
| 127 | return *this; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
[1791] | 131 | @brief Overloaded << operator, redirects the output to the console, the logfile and the ingame shell. |
---|
[1505] | 132 | @param manipulator A function, manipulating the outstream. |
---|
| 133 | @return A reference to the OutputHandler itself |
---|
| 134 | */ |
---|
| 135 | OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&)) |
---|
| 136 | { |
---|
| 137 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 138 | manipulator(std::cout); |
---|
| 139 | |
---|
| 140 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 141 | { |
---|
| 142 | manipulator(this->logfile_); |
---|
| 143 | this->logfile_.flush(); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
[1586] | 147 | (*this->outputBuffer_) << manipulator; |
---|
[1505] | 148 | |
---|
| 149 | return *this; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
[1791] | 153 | @brief Overloaded << operator, redirects the output to the console, the logfile and the ingame shell. |
---|
[1505] | 154 | @param manipulator A function, manipulating the outstream. |
---|
| 155 | @return A reference to the OutputHandler itself |
---|
| 156 | */ |
---|
| 157 | OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&)) |
---|
| 158 | { |
---|
| 159 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 160 | manipulator(std::cout); |
---|
| 161 | |
---|
| 162 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 163 | { |
---|
| 164 | manipulator(this->logfile_); |
---|
| 165 | this->logfile_.flush(); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
[1586] | 169 | (*this->outputBuffer_) << manipulator; |
---|
[1505] | 170 | |
---|
| 171 | return *this; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
[1791] | 175 | @brief Overloaded << operator, redirects the output to the console, the logfile and the ingame shell. |
---|
[1505] | 176 | @param manipulator A function, manipulating the outstream. |
---|
| 177 | @return A reference to the OutputHandler itself |
---|
| 178 | */ |
---|
| 179 | OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&)) |
---|
| 180 | { |
---|
| 181 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 182 | manipulator(std::cout); |
---|
| 183 | |
---|
| 184 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 185 | { |
---|
| 186 | manipulator(this->logfile_); |
---|
| 187 | this->logfile_.flush(); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
[1586] | 191 | (*this->outputBuffer_) << manipulator; |
---|
[1505] | 192 | |
---|
| 193 | return *this; |
---|
| 194 | } |
---|
| 195 | } |
---|