Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 697 was 685, checked in by landauf, 16 years ago

commented the type-specific overloaded non-member << operators out, the template works for them too (i hope this solves a problem on windows with non-g++ compilers)

File size: 7.5 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 "CorePrereqs.h"
40
41#include <iostream>
42#include <fstream>
43#include <string>
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            static OutputHandler& getOutStream();
52
53            /** @returns a reference to the logfile. */
54            inline std::ofstream& getLogfile() { return this->logfile_; }
55
56            template <class T>
57            OutputHandler& output(const T& output);
58
59            /** @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 */
60            inline OutputHandler& operator<<(unsigned char val)   { return this->output(val); }
61            /** @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 */
62            inline OutputHandler& operator<<(short val)           { return this->output(val); }
63            /** @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 */
64            inline OutputHandler& operator<<(unsigned short val)  { return this->output(val); }
65            /** @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 */
66            inline OutputHandler& operator<<(int val)             { return this->output(val); }
67            /** @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 */
68            inline OutputHandler& operator<<(unsigned int val)    { return this->output(val); }
69            /** @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 */
70            inline OutputHandler& operator<<(long val)            { return this->output(val); }
71            /** @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 */
72            inline OutputHandler& operator<<(unsigned long val)   { return this->output(val); }
73            /** @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 */
74            inline OutputHandler& operator<<(float val)           { return this->output(val); }
75            /** @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 */
76            inline OutputHandler& operator<<(double val)          { return this->output(val); }
77            /** @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 */
78            inline OutputHandler& operator<<(long double val)     { return this->output(val); }
79            /** @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 */
80            inline OutputHandler& operator<<(const void* val)     { return this->output(val); }
81            /** @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 */
82            inline OutputHandler& operator<<(bool val)            { return this->output(val); }
83
84            OutputHandler& operator<<(std::streambuf* sb);
85
86            OutputHandler& operator<<(std::ostream& (*manipulator)(std::ostream&));
87            OutputHandler& operator<<(std::ios& (*manipulator)(std::ios&));
88            OutputHandler& operator<<(std::ios_base& (*manipulator)(std::ios_base&));
89
90        private:
91            explicit OutputHandler(const std::string& logfilename);
92            OutputHandler(const OutputHandler& oh) {}; // don't copy
93            ~OutputHandler();
94            std::ofstream logfile_;     //!< The logfile where the output is logged
95            std::string logfilename_;   //!< The name of the logfile
96    };
97
98    /**
99        @brief Redirects the output to the console and the logfile.
100        @param output The value that should be shown in the console
101        @return A reference to the OutputHandler itself
102    */
103    template<class T>
104    OutputHandler& OutputHandler::output(const T& output)
105    {
106        std::cout << output;
107        this->logfile_ << output;
108        this->logfile_.flush();
109        return *this;
110    }
111/*
112    _CoreExport OutputHandler& operator<<(OutputHandler& out, char c);
113    _CoreExport OutputHandler& operator<<(OutputHandler& out, signed char c);
114    _CoreExport OutputHandler& operator<<(OutputHandler& out, unsigned char c);
115
116    _CoreExport OutputHandler& operator<<(OutputHandler& out, const char* s);
117    _CoreExport OutputHandler& operator<<(OutputHandler& out, const signed char* s);
118    _CoreExport OutputHandler& operator<<(OutputHandler& out, const unsigned char* s);
119*/
120    /**
121        @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.
122        @param out The OutputHandler itself
123        @param output The class that should be shown in the console
124        @return The OutputHandler itself
125    */
126    template<class T>
127    OutputHandler& operator<<(OutputHandler& out, const T& output)
128    {
129        std::cout << output;
130        out.getLogfile() << output;
131        out.getLogfile().flush();
132        return out;
133    }
134
135}
136
137#endif /* _OutputHandler_H__ */
Note: See TracBrowser for help on using the repository browser.