1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Fabian 'x3n' Landau |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | /** |
---|
29 | @file OutputHandler.h |
---|
30 | @brief Definition of the OutputHandler class. |
---|
31 | |
---|
32 | The OutputHandler acts like std::cout, but output isn't only shown in the console, |
---|
33 | but also written to the logfile. |
---|
34 | */ |
---|
35 | |
---|
36 | #ifndef _OutputHandler_H__ |
---|
37 | #define _OutputHandler_H__ |
---|
38 | |
---|
39 | #include <iostream> |
---|
40 | #include <fstream> |
---|
41 | #include <string> |
---|
42 | |
---|
43 | #include "CorePrereqs.h" |
---|
44 | |
---|
45 | namespace orxonox |
---|
46 | { |
---|
47 | //! The OutputHandler acts like std::cout, but redirects output to the console AND the logfile. |
---|
48 | class _CoreExport OutputHandler |
---|
49 | { |
---|
50 | public: |
---|
51 | enum OutputDevice |
---|
52 | { |
---|
53 | LD_All, |
---|
54 | LD_Console, |
---|
55 | LD_Logfile, |
---|
56 | LD_Shell |
---|
57 | }; |
---|
58 | |
---|
59 | static OutputHandler& getOutStream(); |
---|
60 | |
---|
61 | /** @brief Puts some text on the outstream. @param text The text */ |
---|
62 | static inline std::string log(const std::string& text) |
---|
63 | { OutputHandler::getOutStream().setOutputLevel(0); OutputHandler::getOutStream().output(text + "\n"); return text; } |
---|
64 | |
---|
65 | /** @brief Returns a reference to the logfile. @return The logfile */ |
---|
66 | inline std::ofstream& getLogfile() |
---|
67 | { return this->logfile_; } |
---|
68 | |
---|
69 | /** @brief Sets the level of the incoming output. @param level The level of the incoming output @return The OutputHandler itself */ |
---|
70 | inline OutputHandler& setOutputLevel(int level) |
---|
71 | { this->outputLevel_ = level; return *this; } |
---|
72 | |
---|
73 | /** @brief Returns the level of the incoming output. @return The level */ |
---|
74 | inline int getOutputLevel() const |
---|
75 | { return this->outputLevel_; } |
---|
76 | |
---|
77 | static int getSoftDebugLevel(OutputHandler::OutputDevice device); |
---|
78 | |
---|
79 | template <class T> |
---|
80 | OutputHandler& output(const T& output); |
---|
81 | |
---|
82 | /** @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 */ |
---|
83 | inline OutputHandler& operator<<(unsigned char val) { return this->output(val); } |
---|
84 | /** @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 */ |
---|
85 | inline OutputHandler& operator<<(short val) { return this->output(val); } |
---|
86 | /** @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 */ |
---|
87 | inline OutputHandler& operator<<(unsigned short val) { return this->output(val); } |
---|
88 | /** @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 */ |
---|
89 | inline OutputHandler& operator<<(int val) { return this->output(val); } |
---|
90 | /** @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 */ |
---|
91 | inline OutputHandler& operator<<(unsigned int val) { return this->output(val); } |
---|
92 | /** @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 */ |
---|
93 | inline OutputHandler& operator<<(long val) { return this->output(val); } |
---|
94 | /** @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 */ |
---|
95 | inline OutputHandler& operator<<(unsigned long val) { return this->output(val); } |
---|
96 | /** @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 */ |
---|
97 | inline OutputHandler& operator<<(float val) { return this->output(val); } |
---|
98 | /** @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 */ |
---|
99 | inline OutputHandler& operator<<(double val) { return this->output(val); } |
---|
100 | /** @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 */ |
---|
101 | inline OutputHandler& operator<<(long double val) { return this->output(val); } |
---|
102 | /** @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 */ |
---|
103 | inline OutputHandler& operator<<(const void* val) { return this->output(val); } |
---|
104 | /** @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 */ |
---|
105 | inline OutputHandler& operator<<(bool val) { return this->output(val); } |
---|
106 | |
---|
107 | OutputHandler& operator<<(std::streambuf* sb); |
---|
108 | |
---|
109 | OutputHandler& operator<<(std::ostream& (*manipulator)(std::ostream&)); |
---|
110 | OutputHandler& operator<<(std::ios& (*manipulator)(std::ios&)); |
---|
111 | OutputHandler& operator<<(std::ios_base& (*manipulator)(std::ios_base&)); |
---|
112 | |
---|
113 | private: |
---|
114 | explicit OutputHandler(const std::string& logfilename); |
---|
115 | OutputHandler(const OutputHandler& oh) {}; // don't copy |
---|
116 | virtual ~OutputHandler(); |
---|
117 | std::ofstream logfile_; //!< The logfile where the output is logged |
---|
118 | std::string logfilename_; //!< The name of the logfile |
---|
119 | int outputLevel_; //!< The level of the incoming output |
---|
120 | }; |
---|
121 | |
---|
122 | /** |
---|
123 | @brief Redirects the output to the console and the logfile. |
---|
124 | @param output The value that should be shown in the console |
---|
125 | @return A reference to the OutputHandler itself |
---|
126 | */ |
---|
127 | template<class T> |
---|
128 | OutputHandler& OutputHandler::output(const T& output) |
---|
129 | { |
---|
130 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_) |
---|
131 | std::cout << output; |
---|
132 | |
---|
133 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_) |
---|
134 | { |
---|
135 | this->logfile_ << output; |
---|
136 | this->logfile_.flush(); |
---|
137 | } |
---|
138 | |
---|
139 | return *this; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | @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. |
---|
144 | @param out The OutputHandler itself |
---|
145 | @param output The class that should be shown in the console |
---|
146 | @return The OutputHandler itself |
---|
147 | */ |
---|
148 | template<class T> |
---|
149 | OutputHandler& operator<<(OutputHandler& out, const T& output) |
---|
150 | { |
---|
151 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= out.getOutputLevel()) |
---|
152 | std::cout << output; |
---|
153 | |
---|
154 | if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= out.getOutputLevel()) |
---|
155 | { |
---|
156 | out.getLogfile() << output; |
---|
157 | out.getLogfile().flush(); |
---|
158 | } |
---|
159 | |
---|
160 | return out; |
---|
161 | } |
---|
162 | |
---|
163 | } |
---|
164 | |
---|
165 | #endif /* _OutputHandler_H__ */ |
---|