Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/util/OutputHandler.cc @ 2662

Last change on this file since 2662 was 2343, checked in by rgrieder, 16 years ago

Fixed a segfault bug than occurred when producing debug output after the Shell has been destroyed. The reason was that the Shell provides an OutputBuffer itself, so when the Shell gets destroyed, it has to set the fallback buffer again to the OutputHandler.

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