[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 Definition of the OutputHandler class. |
---|
| 32 | |
---|
| 33 | The OutputHandler acts like std::cout, but output isn't only shown in the console, |
---|
[1791] | 34 | but also written to the logfile and the ingame shell. |
---|
[1505] | 35 | */ |
---|
| 36 | |
---|
| 37 | #ifndef _OutputHandler_H__ |
---|
| 38 | #define _OutputHandler_H__ |
---|
| 39 | |
---|
[1586] | 40 | #include "UtilPrereqs.h" |
---|
[1505] | 41 | |
---|
| 42 | #include <iostream> |
---|
| 43 | #include <fstream> |
---|
| 44 | #include <string> |
---|
| 45 | |
---|
| 46 | #include "OutputBuffer.h" |
---|
| 47 | |
---|
| 48 | namespace orxonox |
---|
| 49 | { |
---|
[1791] | 50 | //! The OutputHandler acts like std::cout, but redirects output to the console, the logfile and the ingame shell. |
---|
[1586] | 51 | class _UtilExport OutputHandler |
---|
[1505] | 52 | { |
---|
| 53 | public: |
---|
| 54 | enum OutputDevice |
---|
| 55 | { |
---|
[1586] | 56 | LD_All = 0, |
---|
| 57 | LD_Console = 1, |
---|
| 58 | LD_Logfile = 2, |
---|
| 59 | LD_Shell = 3 |
---|
[1505] | 60 | }; |
---|
| 61 | |
---|
| 62 | static OutputHandler& getOutStream(); |
---|
| 63 | |
---|
| 64 | /** @brief Puts some text on the outstream. @param text The text */ |
---|
[3196] | 65 | static inline const std::string& log(const std::string& text) |
---|
[1505] | 66 | { OutputHandler::getOutStream().setOutputLevel(0); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 67 | |
---|
| 68 | /** @brief Puts an error on the outstream. @param text The text */ |
---|
[3196] | 69 | static inline const std::string& error(const std::string& text) |
---|
[1505] | 70 | { OutputHandler::getOutStream().setOutputLevel(1); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 71 | |
---|
| 72 | /** @brief Puts a warning on the outstream. @param text The text */ |
---|
[3196] | 73 | static inline const std::string& warning(const std::string& text) |
---|
[1505] | 74 | { OutputHandler::getOutStream().setOutputLevel(2); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 75 | |
---|
| 76 | /** @brief Puts an info on the outstream. @param text The text */ |
---|
[3196] | 77 | static inline const std::string& info(const std::string& text) |
---|
[1505] | 78 | { OutputHandler::getOutStream().setOutputLevel(3); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 79 | |
---|
| 80 | /** @brief Puts some debug output on the outstream. @param text The text */ |
---|
[3196] | 81 | static inline const std::string& debug(const std::string& text) |
---|
[1505] | 82 | { OutputHandler::getOutStream().setOutputLevel(4); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 83 | |
---|
| 84 | /** @brief Returns a reference to the logfile. @return The logfile */ |
---|
| 85 | inline std::ofstream& getLogfile() |
---|
| 86 | { return this->logfile_; } |
---|
| 87 | |
---|
[1586] | 88 | /** @brief Returns a pointer to the OutputBuffer. @return The OutputBuffer */ |
---|
| 89 | inline OutputBuffer* getOutputBuffer() |
---|
| 90 | { return this->outputBuffer_; } |
---|
| 91 | |
---|
[1505] | 92 | /** @brief Sets the level of the incoming output. @param level The level of the incoming output @return The OutputHandler itself */ |
---|
| 93 | inline OutputHandler& setOutputLevel(int level) |
---|
| 94 | { this->outputLevel_ = level; return *this; } |
---|
| 95 | |
---|
| 96 | /** @brief Returns the level of the incoming output. @return The level */ |
---|
| 97 | inline int getOutputLevel() const |
---|
| 98 | { return this->outputLevel_; } |
---|
| 99 | |
---|
[1586] | 100 | static void setSoftDebugLevel(OutputHandler::OutputDevice device, int level); |
---|
| 101 | static int getSoftDebugLevel(OutputHandler::OutputDevice device = OutputHandler::LD_All); |
---|
[1505] | 102 | |
---|
[2710] | 103 | static void setLogPath(const std::string& path); |
---|
| 104 | |
---|
[2662] | 105 | void setOutputBuffer(OutputBuffer* buffer); |
---|
[1505] | 106 | |
---|
| 107 | template <class T> |
---|
| 108 | OutputHandler& output(const T& output); |
---|
| 109 | |
---|
| 110 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 111 | inline OutputHandler& operator<<(unsigned char val) { return this->output(val); } |
---|
[1505] | 112 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 113 | inline OutputHandler& operator<<(short val) { return this->output(val); } |
---|
[1505] | 114 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 115 | inline OutputHandler& operator<<(unsigned short val) { return this->output(val); } |
---|
[1505] | 116 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 117 | inline OutputHandler& operator<<(int val) { return this->output(val); } |
---|
[1505] | 118 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 119 | inline OutputHandler& operator<<(unsigned int val) { return this->output(val); } |
---|
[1505] | 120 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 121 | inline OutputHandler& operator<<(long val) { return this->output(val); } |
---|
[1505] | 122 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 123 | inline OutputHandler& operator<<(unsigned long val) { return this->output(val); } |
---|
[1505] | 124 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 125 | inline OutputHandler& operator<<(long long val) { return this->output(val); } |
---|
[1505] | 126 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 127 | inline OutputHandler& operator<<(unsigned long long val) { return this->output(val); } |
---|
[1505] | 128 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 129 | inline OutputHandler& operator<<(float val) { return this->output(val); } |
---|
[1505] | 130 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 131 | inline OutputHandler& operator<<(double val) { return this->output(val); } |
---|
[1505] | 132 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
[1725] | 133 | inline OutputHandler& operator<<(long double val) { return this->output(val); } |
---|
| 134 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
| 135 | inline OutputHandler& operator<<(const void* val) { return this->output(val); } |
---|
| 136 | /** @brief Overloaded << operator, redirects the output to the console and the logfile. @param val The value that should be shown in the console @return A reference to the OutputHandler itself */ |
---|
| 137 | inline OutputHandler& operator<<(bool val) { return this->output(val); } |
---|
[1505] | 138 | |
---|
| 139 | OutputHandler& operator<<(std::streambuf* sb); |
---|
| 140 | |
---|
| 141 | OutputHandler& operator<<(std::ostream& (*manipulator)(std::ostream&)); |
---|
| 142 | OutputHandler& operator<<(std::ios& (*manipulator)(std::ios&)); |
---|
| 143 | OutputHandler& operator<<(std::ios_base& (*manipulator)(std::ios_base&)); |
---|
| 144 | |
---|
| 145 | private: |
---|
[2710] | 146 | explicit OutputHandler(); |
---|
| 147 | OutputHandler(const OutputHandler& oh); |
---|
[1505] | 148 | virtual ~OutputHandler(); |
---|
[1586] | 149 | |
---|
| 150 | std::ofstream logfile_; //!< The logfile where the output is logged |
---|
| 151 | std::string logfilename_; //!< The name of the logfile |
---|
| 152 | OutputBuffer fallbackBuffer_; //!< The OutputBuffer that gets used if there is no other OutputBuffer |
---|
| 153 | OutputBuffer* outputBuffer_; //!< The OutputBuffer to put output in (usually used by the Shell) |
---|
| 154 | int outputLevel_; //!< The level of the incoming output |
---|
| 155 | int softDebugLevel_[4]; //!< The soft debug level for each OutputDevice - the configurable maximal output level |
---|
[1505] | 156 | }; |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | @brief Redirects the output to the console and the logfile. |
---|
| 160 | @param output The value that should be shown in the console |
---|
| 161 | @return A reference to the OutputHandler itself |
---|
| 162 | */ |
---|
| 163 | template<class T> |
---|
| 164 | OutputHandler& OutputHandler::output(const T& output) |
---|
| 165 | { |
---|
| 166 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 167 | std::cout << output; |
---|
| 168 | |
---|
| 169 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 170 | { |
---|
| 171 | this->logfile_ << output; |
---|
| 172 | this->logfile_.flush(); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
[1586] | 176 | (*this->outputBuffer_) << output; |
---|
[1505] | 177 | |
---|
| 178 | return *this; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | @brief Overloading of the non-member << operator to redirect the output of classes with self defined '<< to std::ostream' operators to the console and the logfile. |
---|
| 183 | @param out The OutputHandler itself |
---|
| 184 | @param output The class that should be shown in the console |
---|
| 185 | @return The OutputHandler itself |
---|
| 186 | */ |
---|
| 187 | template<class T> |
---|
| 188 | OutputHandler& operator<<(OutputHandler& out, const T& output) |
---|
| 189 | { |
---|
| 190 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= out.getOutputLevel()) |
---|
| 191 | std::cout << output; |
---|
| 192 | |
---|
| 193 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= out.getOutputLevel()) |
---|
| 194 | { |
---|
| 195 | out.getLogfile() << output; |
---|
| 196 | out.getLogfile().flush(); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= out.getOutputLevel()) |
---|
[1586] | 200 | (*out.getOutputBuffer()) << output; |
---|
[1505] | 201 | |
---|
| 202 | return out; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | #endif /* _OutputHandler_H__ */ |
---|