Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialFS09/src/util/OutputHandler.cc @ 11127

Last change on this file since 11127 was 2710, checked in by rgrieder, 16 years ago

Merged buildsystem3 containing buildsystem2 containing Adi's buildsystem branch back to the trunk.
Please update the media directory if you were not using buildsystem3 before.

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