Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/util/OutputHandler.cc @ 1913

Last change on this file since 1913 was 1586, checked in by landauf, 16 years ago

moved Debug.h, OutputHandler and OutputBuffer to util, to make COUT(x) available everywhere

  • Property svn:eol-style set to native
File size: 6.2 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.cc
31    @brief Implementation of the OutputHandler class.
32*/
33
34#include "OutputHandler.h"
35
36namespace orxonox
37{
38    /**
39        @brief Constructor: Opens the logfile and writes the first line.
40        @param logfilename The name of the logfile
41    */
42    OutputHandler::OutputHandler(const std::string& logfilename)
43    {
44        this->outputBuffer_ = &this->fallbackBuffer_;
45        this->softDebugLevel_[0] = this->softDebugLevel_[1] = this->softDebugLevel_[2] = this->softDebugLevel_[3] = 2;
46        this->logfilename_ = logfilename;
47        this->logfile_.open(this->logfilename_.c_str(), std::fstream::out);
48        this->logfile_ << "Started log at yyyy/mm/dd hh:mm:ss" << std::endl;
49        this->logfile_.flush();
50    }
51
52    /**
53        @brief Destructor: Writes the last line to the logfile and closes it.
54    */
55    OutputHandler::~OutputHandler()
56    {
57        this->logfile_ << "Closed log" << std::endl;
58        this->logfile_.close();
59    }
60
61    /**
62        @brief Returns a reference to the only existing instance of the OutputHandler class.
63        @return The instance
64    */
65    OutputHandler& OutputHandler::getOutStream()
66    {
67        static OutputHandler orxout("orxonox.log");
68        return orxout;
69    }
70
71    /**
72        @brief Sets the soft debug level for a given output device.
73        @param device The output device
74        @param level The debug level
75    */
76    void OutputHandler::setSoftDebugLevel(OutputHandler::OutputDevice device, int level)
77    {
78        OutputHandler::getOutStream().softDebugLevel_[(unsigned int)device] = level;
79    }
80
81    /**
82        @brief Returns the soft debug level for a given output device.
83        @param device The output device
84        @return The debug level
85    */
86    int OutputHandler::getSoftDebugLevel(OutputHandler::OutputDevice device)
87    {
88        return OutputHandler::getOutStream().softDebugLevel_[(unsigned int)device];
89    }
90
91    /**
92        @brief Sets the OutputBuffer, representing the third output stream.
93        @param buffer The OutputBuffer
94    */
95    void OutputHandler::setOutputBuffer(OutputBuffer& buffer)
96    {
97        buffer.getStream() >> this->outputBuffer_->getStream().rdbuf();
98        this->outputBuffer_ = &buffer;
99    }
100
101    /**
102        @brief Overloaded << operator, redirects the output to the console and the logfile.
103        @param sb The streambuffer that should be shown in the console
104        @return A reference to the OutputHandler itself
105    */
106    OutputHandler& OutputHandler::operator<<(std::streambuf* sb)
107    {
108        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
109            std::cout << sb;
110
111        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
112        {
113            this->logfile_ << sb;
114            this->logfile_.flush();
115        }
116
117        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
118            (*this->outputBuffer_) << sb;
119
120        return *this;
121    }
122
123    /**
124        @brief Overloaded << operator, redirects the output to the console and the logfile.
125        @param manipulator A function, manipulating the outstream.
126        @return A reference to the OutputHandler itself
127    */
128    OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&))
129    {
130        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
131            manipulator(std::cout);
132
133        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
134        {
135            manipulator(this->logfile_);
136            this->logfile_.flush();
137        }
138
139        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
140            (*this->outputBuffer_) << manipulator;
141
142        return *this;
143    }
144
145    /**
146        @brief Overloaded << operator, redirects the output to the console and the logfile.
147        @param manipulator A function, manipulating the outstream.
148        @return A reference to the OutputHandler itself
149    */
150    OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&))
151    {
152        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
153            manipulator(std::cout);
154
155        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
156        {
157            manipulator(this->logfile_);
158            this->logfile_.flush();
159        }
160
161        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
162            (*this->outputBuffer_) << manipulator;
163
164        return *this;
165    }
166
167    /**
168        @brief Overloaded << operator, redirects the output to the console and the logfile.
169        @param manipulator A function, manipulating the outstream.
170        @return A reference to the OutputHandler itself
171    */
172    OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&))
173    {
174        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
175            manipulator(std::cout);
176
177        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
178        {
179            manipulator(this->logfile_);
180            this->logfile_.flush();
181        }
182
183        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
184            (*this->outputBuffer_) << manipulator;
185
186        return *this;
187    }
188}
Note: See TracBrowser for help on using the repository browser.