Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/OutputHandler.h @ 1351

Last change on this file since 1351 was 1313, checked in by landauf, 17 years ago
  • implemented Shell, but not yet linked with the graphical console
  • added new features (cursor, OIS::KeyCode listener) to InputBuffer
  • changed some includes to avoid circular header-dependencies in OrxonoxClass and Shell
File size: 10.0 KB
Line 
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#include "Shell.h"
47
48namespace orxonox
49{
50    //! The OutputHandler acts like std::cout, but redirects output to the console AND the logfile.
51    class _CoreExport OutputHandler
52    {
53        public:
54            enum OutputDevice
55            {
56                LD_All,
57                LD_Console,
58                LD_Logfile,
59                LD_Shell
60            };
61
62            static OutputHandler& getOutStream();
63
64            /** @brief Puts some text on the outstream. @param text The text */
65            static inline std::string log(const std::string& text)
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 */
69            static inline std::string error(const std::string& text)
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 */
73            static inline std::string warning(const std::string& text)
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 */
77            static inline std::string info(const std::string& text)
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 */
81            static inline std::string debug(const std::string& text)
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
88            /** @brief Sets the level of the incoming output. @param level The level of the incoming output @return The OutputHandler itself */
89            inline OutputHandler& setOutputLevel(int level)
90                { this->outputLevel_ = level; return *this; }
91
92            /** @brief Returns the level of the incoming output. @return The level */
93            inline int getOutputLevel() const
94                { return this->outputLevel_; }
95
96            static int getSoftDebugLevel(OutputHandler::OutputDevice device);
97
98            template <class T>
99            OutputHandler& output(const T& output);
100
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<<(unsigned char 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<<(short 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<<(unsigned short val)  { return this->output(val); }
107            /** @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 */
108            inline OutputHandler& operator<<(int val)             { return this->output(val); }
109            /** @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 */
110            inline OutputHandler& operator<<(unsigned int val)    { return this->output(val); }
111            /** @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 */
112            inline OutputHandler& operator<<(long val)            { return this->output(val); }
113            /** @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 */
114            inline OutputHandler& operator<<(unsigned long val)   { return this->output(val); }
115            /** @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 */
116            inline OutputHandler& operator<<(float val)           { return this->output(val); }
117            /** @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 */
118            inline OutputHandler& operator<<(double val)          { return this->output(val); }
119            /** @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 */
120            inline OutputHandler& operator<<(long double val)     { return this->output(val); }
121            /** @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 */
122            inline OutputHandler& operator<<(const void* val)     { return this->output(val); }
123            /** @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 */
124            inline OutputHandler& operator<<(bool val)            { return this->output(val); }
125
126            OutputHandler& operator<<(std::streambuf* sb);
127
128            OutputHandler& operator<<(std::ostream& (*manipulator)(std::ostream&));
129            OutputHandler& operator<<(std::ios& (*manipulator)(std::ios&));
130            OutputHandler& operator<<(std::ios_base& (*manipulator)(std::ios_base&));
131
132        private:
133            explicit OutputHandler(const std::string& logfilename);
134            OutputHandler(const OutputHandler& oh);  // don't copy
135            virtual ~OutputHandler();
136            std::ofstream logfile_;     //!< The logfile where the output is logged
137            std::string logfilename_;   //!< The name of the logfile
138            int outputLevel_;           //!< The level of the incoming output
139    };
140
141    /**
142        @brief Redirects the output to the console and the logfile.
143        @param output The value that should be shown in the console
144        @return A reference to the OutputHandler itself
145    */
146    template<class T>
147    OutputHandler& OutputHandler::output(const T& output)
148    {
149        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
150            std::cout << output;
151
152        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
153        {
154            this->logfile_ << output;
155            this->logfile_.flush();
156        }
157
158        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
159            Shell::getInstance().getOutputBuffer() << output;
160
161        return *this;
162    }
163
164    /**
165        @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.
166        @param out The OutputHandler itself
167        @param output The class that should be shown in the console
168        @return The OutputHandler itself
169    */
170    template<class T>
171    OutputHandler& operator<<(OutputHandler& out, const T& output)
172    {
173        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= out.getOutputLevel())
174            std::cout << output;
175
176        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= out.getOutputLevel())
177        {
178            out.getLogfile() << output;
179            out.getLogfile().flush();
180        }
181
182        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= out.getOutputLevel())
183            Shell::getInstance().getOutputBuffer() << output;
184
185        return out;
186    }
187
188}
189
190#endif /* _OutputHandler_H__ */
Note: See TracBrowser for help on using the repository browser.