[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 | /** |
---|
| 30 | @file OutputHandler.cc |
---|
| 31 | @brief Implementation of the OutputHandler class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "OutputHandler.h" |
---|
| 35 | #include "CoreSettings.h" |
---|
| 36 | #include "ConsoleCommand.h" |
---|
| 37 | #include "Shell.h" |
---|
| 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | SetConsoleCommandShortcutGeneric(log, createConsoleCommand(createFunctor(&OutputHandler::log), "log" )); |
---|
| 42 | SetConsoleCommandShortcutGeneric(error, createConsoleCommand(createFunctor(&OutputHandler::error), "error" )); |
---|
| 43 | SetConsoleCommandShortcutGeneric(warning, createConsoleCommand(createFunctor(&OutputHandler::warning), "warning")); |
---|
| 44 | SetConsoleCommandShortcutGeneric(info, createConsoleCommand(createFunctor(&OutputHandler::info), "info" )); |
---|
| 45 | SetConsoleCommandShortcutGeneric(debug, createConsoleCommand(createFunctor(&OutputHandler::debug), "debug" )); |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | @brief Constructor: Opens the logfile and writes the first line. |
---|
| 49 | @param logfilename The name of the logfile |
---|
| 50 | */ |
---|
| 51 | OutputHandler::OutputHandler(const std::string& logfilename) |
---|
| 52 | { |
---|
| 53 | this->logfilename_ = logfilename; |
---|
| 54 | this->logfile_.open(this->logfilename_.c_str(), std::fstream::out); |
---|
| 55 | this->logfile_ << "Started log at yyyy/mm/dd hh:mm:ss" << std::endl; |
---|
| 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 | /** |
---|
| 79 | @brief Returns the soft debug level for a given output device. |
---|
| 80 | @param device The output device |
---|
| 81 | @return The debug level |
---|
| 82 | */ |
---|
| 83 | int OutputHandler::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
| 84 | { |
---|
| 85 | return CoreSettings::getSoftDebugLevel(device); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | @brief Returns the Shell's OutputBuffer. This is mere placed here to avoid |
---|
| 90 | recompiling the entire project when Shell.h changes. |
---|
| 91 | @return The OutputBuffer of the Shell |
---|
| 92 | */ |
---|
| 93 | OutputBuffer& OutputHandler::getShellOutputBuffer() |
---|
| 94 | { |
---|
| 95 | return Shell::getInstance().getOutputBuffer(); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
| 100 | @param sb The streambuffer that should be shown in the console |
---|
| 101 | @return A reference to the OutputHandler itself |
---|
| 102 | */ |
---|
| 103 | OutputHandler& OutputHandler::operator<<(std::streambuf* sb) |
---|
| 104 | { |
---|
| 105 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 106 | std::cout << sb; |
---|
| 107 | |
---|
| 108 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 109 | { |
---|
| 110 | this->logfile_ << sb; |
---|
| 111 | this->logfile_.flush(); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
| 115 | Shell::getInstance().getOutputBuffer() << sb; |
---|
| 116 | |
---|
| 117 | return *this; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
| 122 | @param manipulator A function, manipulating the outstream. |
---|
| 123 | @return A reference to the OutputHandler itself |
---|
| 124 | */ |
---|
| 125 | OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&)) |
---|
| 126 | { |
---|
| 127 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 128 | manipulator(std::cout); |
---|
| 129 | |
---|
| 130 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 131 | { |
---|
| 132 | manipulator(this->logfile_); |
---|
| 133 | this->logfile_.flush(); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
| 137 | Shell::getInstance().getOutputBuffer() << manipulator; |
---|
| 138 | |
---|
| 139 | return *this; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
| 144 | @param manipulator A function, manipulating the outstream. |
---|
| 145 | @return A reference to the OutputHandler itself |
---|
| 146 | */ |
---|
| 147 | OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&)) |
---|
| 148 | { |
---|
| 149 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 150 | manipulator(std::cout); |
---|
| 151 | |
---|
| 152 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 153 | { |
---|
| 154 | manipulator(this->logfile_); |
---|
| 155 | this->logfile_.flush(); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
| 159 | Shell::getInstance().getOutputBuffer() << manipulator; |
---|
| 160 | |
---|
| 161 | return *this; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | @brief Overloaded << operator, redirects the output to the console and the logfile. |
---|
| 166 | @param manipulator A function, manipulating the outstream. |
---|
| 167 | @return A reference to the OutputHandler itself |
---|
| 168 | */ |
---|
| 169 | OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&)) |
---|
| 170 | { |
---|
| 171 | if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 172 | manipulator(std::cout); |
---|
| 173 | |
---|
| 174 | if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 175 | { |
---|
| 176 | manipulator(this->logfile_); |
---|
| 177 | this->logfile_.flush(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
| 181 | Shell::getInstance().getOutputBuffer() << manipulator; |
---|
| 182 | |
---|
| 183 | return *this; |
---|
| 184 | } |
---|
| 185 | } |
---|