[684] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[684] | 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 | |
---|
[871] | 29 | /** |
---|
| 30 | @file OutputHandler.cc |
---|
| 31 | @brief Implementation of the OutputHandler class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[1062] | 34 | #include "OutputHandler.h" |
---|
[1052] | 35 | #include "CoreSettings.h" |
---|
| 36 | #include "ConsoleCommand.h" |
---|
[684] | 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
[1341] | 40 | SetConsoleCommandShortcutGeneric(log, createExecutor(createFunctor(&OutputHandler::log), "log" )); |
---|
| 41 | SetConsoleCommandShortcutGeneric(error, createExecutor(createFunctor(&OutputHandler::error), "error" )); |
---|
| 42 | SetConsoleCommandShortcutGeneric(warning, createExecutor(createFunctor(&OutputHandler::warning), "warning")); |
---|
| 43 | SetConsoleCommandShortcutGeneric(info, createExecutor(createFunctor(&OutputHandler::info), "info" )); |
---|
| 44 | SetConsoleCommandShortcutGeneric(debug, createExecutor(createFunctor(&OutputHandler::debug), "debug" )); |
---|
[1052] | 45 | |
---|
[684] | 46 | /** |
---|
| 47 | @brief Constructor: Opens the logfile and writes the first line. |
---|
| 48 | @param logfilename The name of the logfile |
---|
| 49 | */ |
---|
[715] | 50 | OutputHandler::OutputHandler(const std::string& logfilename) |
---|
[684] | 51 | { |
---|
| 52 | this->logfilename_ = logfilename; |
---|
| 53 | this->logfile_.open(this->logfilename_.c_str(), std::fstream::out); |
---|
[1052] | 54 | this->logfile_ << "Started log at yyyy/mm/dd hh:mm:ss" << std::endl; |
---|
[684] | 55 | this->logfile_.flush(); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | @brief Destructor: Writes the last line to the logfile and closes it. |
---|
| 60 | */ |
---|
| 61 | OutputHandler::~OutputHandler() |
---|
| 62 | { |
---|
| 63 | this->logfile_ << "Closed log" << std::endl; |
---|
| 64 | this->logfile_.close(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
[871] | 68 | @brief Returns a reference to the only existing instance of the OutputHandler class. |
---|
| 69 | @return The instance |
---|
[684] | 70 | */ |
---|
| 71 | OutputHandler& OutputHandler::getOutStream() |
---|
| 72 | { |
---|
| 73 | static OutputHandler orxout("orxonox.log"); |
---|
| 74 | return orxout; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
[699] | 78 | @brief Returns the soft debug level for a given output device. |
---|
| 79 | @param device The output device |
---|
| 80 | @return The debug level |
---|
| 81 | */ |
---|
| 82 | int OutputHandler::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
| 83 | { |
---|
[1052] | 84 | return CoreSettings::getSoftDebugLevel(device); |
---|
[699] | 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
[684] | 88 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
| 89 | @param sb The streambuffer that should be shown in the console |
---|
| 90 | @return A reference to the OutputHandler itself |
---|
| 91 | */ |
---|
| 92 | OutputHandler& OutputHandler::operator<<(std::streambuf* sb) |
---|
| 93 | { |
---|
[699] | 94 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 95 | std::cout << sb; |
---|
| 96 | |
---|
| 97 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 98 | { |
---|
| 99 | this->logfile_ << sb; |
---|
| 100 | this->logfile_.flush(); |
---|
| 101 | } |
---|
| 102 | |
---|
[1313] | 103 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
| 104 | Shell::getInstance().getOutputBuffer() << sb; |
---|
| 105 | |
---|
[684] | 106 | return *this; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
| 111 | @param manipulator A function, manipulating the outstream. |
---|
| 112 | @return A reference to the OutputHandler itself |
---|
| 113 | */ |
---|
| 114 | OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&)) |
---|
| 115 | { |
---|
[699] | 116 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 117 | manipulator(std::cout); |
---|
| 118 | |
---|
| 119 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 120 | { |
---|
| 121 | manipulator(this->logfile_); |
---|
| 122 | this->logfile_.flush(); |
---|
| 123 | } |
---|
| 124 | |
---|
[1313] | 125 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
[1322] | 126 | Shell::getInstance().getOutputBuffer() << manipulator; |
---|
[1313] | 127 | |
---|
[684] | 128 | return *this; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
| 133 | @param manipulator A function, manipulating the outstream. |
---|
| 134 | @return A reference to the OutputHandler itself |
---|
| 135 | */ |
---|
| 136 | OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&)) |
---|
| 137 | { |
---|
[699] | 138 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 139 | manipulator(std::cout); |
---|
| 140 | |
---|
| 141 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 142 | { |
---|
| 143 | manipulator(this->logfile_); |
---|
| 144 | this->logfile_.flush(); |
---|
| 145 | } |
---|
| 146 | |
---|
[1313] | 147 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
[1322] | 148 | Shell::getInstance().getOutputBuffer() << manipulator; |
---|
[1313] | 149 | |
---|
[684] | 150 | return *this; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
| 155 | @param manipulator A function, manipulating the outstream. |
---|
| 156 | @return A reference to the OutputHandler itself |
---|
| 157 | */ |
---|
| 158 | OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&)) |
---|
| 159 | { |
---|
[699] | 160 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 161 | manipulator(std::cout); |
---|
| 162 | |
---|
| 163 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 164 | { |
---|
| 165 | manipulator(this->logfile_); |
---|
| 166 | this->logfile_.flush(); |
---|
| 167 | } |
---|
| 168 | |
---|
[1313] | 169 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
[1322] | 170 | Shell::getInstance().getOutputBuffer() << manipulator; |
---|
[1313] | 171 | |
---|
[684] | 172 | return *this; |
---|
| 173 | } |
---|
[699] | 174 | } |
---|