Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/core/OutputHandler.h @ 1093

Last change on this file since 1093 was 715, checked in by rgrieder, 17 years ago
  • the master has spoken…
  • misc/String.h is not anymore..
File size: 8.2 KB
Line 
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
45namespace 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            /** @returns a reference to the logfile. */
62            inline std::ofstream& getLogfile()
63                { return this->logfile_; }
64
65            /** @brief Sets the level of the incoming output. @param level The level of the incoming output @return The OutputHandler itself */
66            inline OutputHandler& setOutputLevel(int level)
67                { this->outputLevel_ = level; return *this; }
68
69            /** @returns the level of the incoming output. */
70            inline int getOutputLevel() const
71                { return this->outputLevel_; }
72
73            static int getSoftDebugLevel(OutputHandler::OutputDevice device);
74
75            template <class T>
76            OutputHandler& output(const T& output);
77
78            /** @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 */
79            inline OutputHandler& operator<<(unsigned char val)   { return this->output(val); }
80            /** @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 */
81            inline OutputHandler& operator<<(short val)           { return this->output(val); }
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 short 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<<(int 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 int 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<<(long 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 long 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<<(float 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<<(double 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<<(long double 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<<(const void* 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<<(bool val)            { return this->output(val); }
102
103            OutputHandler& operator<<(std::streambuf* sb);
104
105            OutputHandler& operator<<(std::ostream& (*manipulator)(std::ostream&));
106            OutputHandler& operator<<(std::ios& (*manipulator)(std::ios&));
107            OutputHandler& operator<<(std::ios_base& (*manipulator)(std::ios_base&));
108
109        private:
110            explicit OutputHandler(const std::string& logfilename);
111            OutputHandler(const OutputHandler& oh) {}; // don't copy
112            virtual ~OutputHandler();
113            std::ofstream logfile_;     //!< The logfile where the output is logged
114            std::string logfilename_;   //!< The name of the logfile
115            int outputLevel_;           //!< The level of the incoming output
116    };
117
118    /**
119        @brief Redirects the output to the console and the logfile.
120        @param output The value that should be shown in the console
121        @return A reference to the OutputHandler itself
122    */
123    template<class T>
124    OutputHandler& OutputHandler::output(const T& output)
125    {
126        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
127            std::cout << output;
128
129        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
130        {
131            this->logfile_ << output;
132            this->logfile_.flush();
133        }
134
135        return *this;
136    }
137
138    /**
139        @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.
140        @param out The OutputHandler itself
141        @param output The class that should be shown in the console
142        @return The OutputHandler itself
143    */
144    template<class T>
145    OutputHandler& operator<<(OutputHandler& out, const T& output)
146    {
147        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Console) >= out.getOutputLevel())
148            std::cout << output;
149
150        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Logfile) >= out.getOutputLevel())
151        {
152            out.getLogfile() << output;
153            out.getLogfile().flush();
154        }
155
156        return out;
157    }
158
159}
160
161#endif /* _OutputHandler_H__ */
Note: See TracBrowser for help on using the repository browser.