| 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.h | 
|---|
| 31 | @brief Definition of the OutputHandler class. | 
|---|
| 32 |  | 
|---|
| 33 | The OutputHandler acts like std::cout, but output isn't only shown in the console, | 
|---|
| 34 | but also written to the logfile. | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | #ifndef _OutputHandler_H__ | 
|---|
| 38 | #define _OutputHandler_H__ | 
|---|
| 39 |  | 
|---|
| 40 | #include "CorePrereqs.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include <iostream> | 
|---|
| 43 | #include <fstream> | 
|---|
| 44 | #include <string> | 
|---|
| 45 |  | 
|---|
| 46 | namespace orxonox | 
|---|
| 47 | { | 
|---|
| 48 | //! The OutputHandler acts like std::cout, but redirects output to the console AND the logfile. | 
|---|
| 49 | class _CoreExport OutputHandler | 
|---|
| 50 | { | 
|---|
| 51 | public: | 
|---|
| 52 | enum OutputDevice | 
|---|
| 53 | { | 
|---|
| 54 | LD_All, | 
|---|
| 55 | LD_Console, | 
|---|
| 56 | LD_Logfile, | 
|---|
| 57 | LD_Shell | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | static OutputHandler& getOutStream(); | 
|---|
| 61 |  | 
|---|
| 62 | /** @brief Puts some text on the outstream. @param text The text */ | 
|---|
| 63 | static inline std::string log(const std::string& text) | 
|---|
| 64 | { OutputHandler::getOutStream().setOutputLevel(0); OutputHandler::getOutStream().output(text + "\n"); return text; } | 
|---|
| 65 |  | 
|---|
| 66 | /** @brief Returns a reference to the logfile. @return The logfile */ | 
|---|
| 67 | inline std::ofstream& getLogfile() | 
|---|
| 68 | { return this->logfile_; } | 
|---|
| 69 |  | 
|---|
| 70 | /** @brief Sets the level of the incoming output. @param level The level of the incoming output @return The OutputHandler itself */ | 
|---|
| 71 | inline OutputHandler& setOutputLevel(int level) | 
|---|
| 72 | { this->outputLevel_ = level; return *this; } | 
|---|
| 73 |  | 
|---|
| 74 | /** @brief Returns the level of the incoming output. @return The level */ | 
|---|
| 75 | inline int getOutputLevel() const | 
|---|
| 76 | { return this->outputLevel_; } | 
|---|
| 77 |  | 
|---|
| 78 | static int getSoftDebugLevel(OutputHandler::OutputDevice device); | 
|---|
| 79 |  | 
|---|
| 80 | template <class T> | 
|---|
| 81 | OutputHandler& output(const T& output); | 
|---|
| 82 |  | 
|---|
| 83 | /** @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 */ | 
|---|
| 84 | inline OutputHandler& operator<<(unsigned char val)   { return this->output(val); } | 
|---|
| 85 | /** @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 */ | 
|---|
| 86 | inline OutputHandler& operator<<(short val)           { return this->output(val); } | 
|---|
| 87 | /** @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 */ | 
|---|
| 88 | inline OutputHandler& operator<<(unsigned short val)  { return this->output(val); } | 
|---|
| 89 | /** @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 */ | 
|---|
| 90 | inline OutputHandler& operator<<(int val)             { return this->output(val); } | 
|---|
| 91 | /** @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 */ | 
|---|
| 92 | inline OutputHandler& operator<<(unsigned int val)    { return this->output(val); } | 
|---|
| 93 | /** @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 */ | 
|---|
| 94 | inline OutputHandler& operator<<(long val)            { return this->output(val); } | 
|---|
| 95 | /** @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 */ | 
|---|
| 96 | inline OutputHandler& operator<<(unsigned long val)   { return this->output(val); } | 
|---|
| 97 | /** @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 */ | 
|---|
| 98 | inline OutputHandler& operator<<(float val)           { return this->output(val); } | 
|---|
| 99 | /** @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 */ | 
|---|
| 100 | inline OutputHandler& operator<<(double val)          { return this->output(val); } | 
|---|
| 101 | /** @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 */ | 
|---|
| 102 | inline OutputHandler& operator<<(long double val)     { return this->output(val); } | 
|---|
| 103 | /** @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 */ | 
|---|
| 104 | inline OutputHandler& operator<<(const void* val)     { return this->output(val); } | 
|---|
| 105 | /** @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 */ | 
|---|
| 106 | inline OutputHandler& operator<<(bool val)            { return this->output(val); } | 
|---|
| 107 |  | 
|---|
| 108 | OutputHandler& operator<<(std::streambuf* sb); | 
|---|
| 109 |  | 
|---|
| 110 | OutputHandler& operator<<(std::ostream& (*manipulator)(std::ostream&)); | 
|---|
| 111 | OutputHandler& operator<<(std::ios& (*manipulator)(std::ios&)); | 
|---|
| 112 | OutputHandler& operator<<(std::ios_base& (*manipulator)(std::ios_base&)); | 
|---|
| 113 |  | 
|---|
| 114 | private: | 
|---|
| 115 | explicit OutputHandler(const std::string& logfilename); | 
|---|
| 116 | OutputHandler(const OutputHandler& oh);  // don't copy | 
|---|
| 117 | virtual ~OutputHandler(); | 
|---|
| 118 | std::ofstream logfile_;     //!< The logfile where the output is logged | 
|---|
| 119 | std::string logfilename_;   //!< The name of the logfile | 
|---|
| 120 | int outputLevel_;           //!< The level of the incoming output | 
|---|
| 121 | }; | 
|---|
| 122 |  | 
|---|
| 123 | /** | 
|---|
| 124 | @brief Redirects the output to the console and the logfile. | 
|---|
| 125 | @param output The value that should be shown in the console | 
|---|
| 126 | @return A reference to the OutputHandler itself | 
|---|
| 127 | */ | 
|---|
| 128 | template<class T> | 
|---|
| 129 | OutputHandler& OutputHandler::output(const T& output) | 
|---|
| 130 | { | 
|---|
| 131 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) | 
|---|
| 132 | std::cout << output; | 
|---|
| 133 |  | 
|---|
| 134 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) | 
|---|
| 135 | { | 
|---|
| 136 | this->logfile_ << output; | 
|---|
| 137 | this->logfile_.flush(); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | return *this; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | /** | 
|---|
| 144 | @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. | 
|---|
| 145 | @param out The OutputHandler itself | 
|---|
| 146 | @param output The class that should be shown in the console | 
|---|
| 147 | @return The OutputHandler itself | 
|---|
| 148 | */ | 
|---|
| 149 | template<class T> | 
|---|
| 150 | OutputHandler& operator<<(OutputHandler& out, const T& output) | 
|---|
| 151 | { | 
|---|
| 152 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= out.getOutputLevel()) | 
|---|
| 153 | std::cout << output; | 
|---|
| 154 |  | 
|---|
| 155 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= out.getOutputLevel()) | 
|---|
| 156 | { | 
|---|
| 157 | out.getLogfile() << output; | 
|---|
| 158 | out.getLogfile().flush(); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | return out; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | #endif /* _OutputHandler_H__ */ | 
|---|