[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 | { |
---|
[5991] | 50 | namespace OutputLevel |
---|
| 51 | { |
---|
| 52 | enum Value |
---|
| 53 | { |
---|
| 54 | None = 0, |
---|
| 55 | Error = 1, |
---|
| 56 | Warning = 2, |
---|
| 57 | Info = 3, |
---|
| 58 | Debug = 4, |
---|
| 59 | Verbose = 5, |
---|
| 60 | Ultra = 6, |
---|
| 61 | }; |
---|
| 62 | } |
---|
| 63 | |
---|
[1791] | 64 | //! The OutputHandler acts like std::cout, but redirects output to the console, the logfile and the ingame shell. |
---|
[1586] | 65 | class _UtilExport OutputHandler |
---|
[1505] | 66 | { |
---|
| 67 | public: |
---|
| 68 | enum OutputDevice |
---|
| 69 | { |
---|
[1586] | 70 | LD_All = 0, |
---|
| 71 | LD_Console = 1, |
---|
| 72 | LD_Logfile = 2, |
---|
| 73 | LD_Shell = 3 |
---|
[1505] | 74 | }; |
---|
| 75 | |
---|
| 76 | static OutputHandler& getOutStream(); |
---|
[5991] | 77 | static inline OutputHandler& getOutStream(int level) |
---|
| 78 | { return OutputHandler::getOutStream().setOutputLevel(level); } |
---|
[1505] | 79 | |
---|
| 80 | /** @brief Puts some text on the outstream. @param text The text */ |
---|
[3196] | 81 | static inline const std::string& log(const std::string& text) |
---|
[1505] | 82 | { OutputHandler::getOutStream().setOutputLevel(0); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 83 | |
---|
| 84 | /** @brief Puts an error on the outstream. @param text The text */ |
---|
[3196] | 85 | static inline const std::string& error(const std::string& text) |
---|
[1505] | 86 | { OutputHandler::getOutStream().setOutputLevel(1); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 87 | |
---|
| 88 | /** @brief Puts a warning on the outstream. @param text The text */ |
---|
[3196] | 89 | static inline const std::string& warning(const std::string& text) |
---|
[1505] | 90 | { OutputHandler::getOutStream().setOutputLevel(2); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 91 | |
---|
| 92 | /** @brief Puts an info on the outstream. @param text The text */ |
---|
[3196] | 93 | static inline const std::string& info(const std::string& text) |
---|
[1505] | 94 | { OutputHandler::getOutStream().setOutputLevel(3); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 95 | |
---|
| 96 | /** @brief Puts some debug output on the outstream. @param text The text */ |
---|
[3196] | 97 | static inline const std::string& debug(const std::string& text) |
---|
[1505] | 98 | { OutputHandler::getOutStream().setOutputLevel(4); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
| 99 | |
---|
| 100 | /** @brief Returns a reference to the logfile. @return The logfile */ |
---|
| 101 | inline std::ofstream& getLogfile() |
---|
| 102 | { return this->logfile_; } |
---|
| 103 | |
---|
[1586] | 104 | /** @brief Returns a pointer to the OutputBuffer. @return The OutputBuffer */ |
---|
| 105 | inline OutputBuffer* getOutputBuffer() |
---|
| 106 | { return this->outputBuffer_; } |
---|
| 107 | |
---|
[1505] | 108 | /** @brief Sets the level of the incoming output. @param level The level of the incoming output @return The OutputHandler itself */ |
---|
| 109 | inline OutputHandler& setOutputLevel(int level) |
---|
| 110 | { this->outputLevel_ = level; return *this; } |
---|
| 111 | |
---|
| 112 | /** @brief Returns the level of the incoming output. @return The level */ |
---|
| 113 | inline int getOutputLevel() const |
---|
| 114 | { return this->outputLevel_; } |
---|
| 115 | |
---|
[1586] | 116 | static void setSoftDebugLevel(OutputHandler::OutputDevice device, int level); |
---|
| 117 | static int getSoftDebugLevel(OutputHandler::OutputDevice device = OutputHandler::LD_All); |
---|
[1505] | 118 | |
---|
[2710] | 119 | static void setLogPath(const std::string& path); |
---|
| 120 | |
---|
[2662] | 121 | void setOutputBuffer(OutputBuffer* buffer); |
---|
[1505] | 122 | |
---|
| 123 | template <class T> |
---|
| 124 | OutputHandler& output(const T& output); |
---|
| 125 | |
---|
| 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 char 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<<(short 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<<(unsigned short 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<<(int val) { return this->output(val); } |
---|
[1505] | 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 */ |
---|
[1725] | 135 | inline OutputHandler& operator<<(unsigned int val) { return this->output(val); } |
---|
[1505] | 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 */ |
---|
[1725] | 137 | inline OutputHandler& operator<<(long val) { return this->output(val); } |
---|
[1505] | 138 | /** @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] | 139 | inline OutputHandler& operator<<(unsigned long val) { return this->output(val); } |
---|
[1505] | 140 | /** @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] | 141 | inline OutputHandler& operator<<(long long val) { return this->output(val); } |
---|
[1505] | 142 | /** @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] | 143 | inline OutputHandler& operator<<(unsigned long long val) { return this->output(val); } |
---|
[1505] | 144 | /** @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] | 145 | inline OutputHandler& operator<<(float val) { return this->output(val); } |
---|
[1505] | 146 | /** @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] | 147 | inline OutputHandler& operator<<(double val) { return this->output(val); } |
---|
[1505] | 148 | /** @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] | 149 | inline OutputHandler& operator<<(long double val) { return this->output(val); } |
---|
| 150 | /** @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 */ |
---|
| 151 | inline OutputHandler& operator<<(const void* val) { return this->output(val); } |
---|
| 152 | /** @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 */ |
---|
| 153 | inline OutputHandler& operator<<(bool val) { return this->output(val); } |
---|
[1505] | 154 | |
---|
| 155 | OutputHandler& operator<<(std::streambuf* sb); |
---|
| 156 | |
---|
| 157 | OutputHandler& operator<<(std::ostream& (*manipulator)(std::ostream&)); |
---|
| 158 | OutputHandler& operator<<(std::ios& (*manipulator)(std::ios&)); |
---|
| 159 | OutputHandler& operator<<(std::ios_base& (*manipulator)(std::ios_base&)); |
---|
| 160 | |
---|
[5991] | 161 | /** @brief Dummy operator required by Debug.h for the ternary operator */ |
---|
| 162 | inline operator int() { return 0; } |
---|
| 163 | |
---|
[1505] | 164 | private: |
---|
[2710] | 165 | explicit OutputHandler(); |
---|
| 166 | OutputHandler(const OutputHandler& oh); |
---|
[1505] | 167 | virtual ~OutputHandler(); |
---|
[1586] | 168 | |
---|
| 169 | std::ofstream logfile_; //!< The logfile where the output is logged |
---|
| 170 | std::string logfilename_; //!< The name of the logfile |
---|
| 171 | OutputBuffer fallbackBuffer_; //!< The OutputBuffer that gets used if there is no other OutputBuffer |
---|
| 172 | OutputBuffer* outputBuffer_; //!< The OutputBuffer to put output in (usually used by the Shell) |
---|
| 173 | int outputLevel_; //!< The level of the incoming output |
---|
| 174 | int softDebugLevel_[4]; //!< The soft debug level for each OutputDevice - the configurable maximal output level |
---|
[1505] | 175 | }; |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | @brief Redirects the output to the console and the logfile. |
---|
| 179 | @param output The value that should be shown in the console |
---|
| 180 | @return A reference to the OutputHandler itself |
---|
| 181 | */ |
---|
| 182 | template<class T> |
---|
| 183 | OutputHandler& OutputHandler::output(const T& output) |
---|
| 184 | { |
---|
[5983] | 185 | //if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
| 186 | // std::cout << output; |
---|
[1505] | 187 | |
---|
| 188 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
| 189 | { |
---|
| 190 | this->logfile_ << output; |
---|
| 191 | this->logfile_.flush(); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_) |
---|
[1586] | 195 | (*this->outputBuffer_) << output; |
---|
[1505] | 196 | |
---|
| 197 | return *this; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | @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. |
---|
| 202 | @param out The OutputHandler itself |
---|
| 203 | @param output The class that should be shown in the console |
---|
| 204 | @return The OutputHandler itself |
---|
| 205 | */ |
---|
| 206 | template<class T> |
---|
| 207 | OutputHandler& operator<<(OutputHandler& out, const T& output) |
---|
| 208 | { |
---|
[5983] | 209 | //if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= out.getOutputLevel()) |
---|
| 210 | // std::cout << output; |
---|
[1505] | 211 | |
---|
| 212 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= out.getOutputLevel()) |
---|
| 213 | { |
---|
| 214 | out.getLogfile() << output; |
---|
| 215 | out.getLogfile().flush(); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= out.getOutputLevel()) |
---|
[1586] | 219 | (*out.getOutputBuffer()) << output; |
---|
[1505] | 220 | |
---|
| 221 | return out; |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | #endif /* _OutputHandler_H__ */ |
---|