Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem2/src/util/OutputHandler.cc @ 2495

Last change on this file since 2495 was 2087, checked in by landauf, 16 years ago

merged objecthierarchy branch back to trunk

  • Property svn:eol-style set to native
File size: 6.4 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#include <time.h>
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    {
45        this->outputBuffer_ = &this->fallbackBuffer_;
46        this->softDebugLevel_[0] = this->softDebugLevel_[1] = this->softDebugLevel_[2] = this->softDebugLevel_[3] = 2;
47        this->logfilename_ = logfilename;
48        this->logfile_.open(this->logfilename_.c_str(), std::fstream::out);
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;
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    /**
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    /**
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    {
95        return OutputHandler::getOutStream().softDebugLevel_[(unsigned int)device];
96    }
97
98    /**
99        @brief Sets the OutputBuffer, representing the third output stream.
100        @param buffer The OutputBuffer
101    */
102    void OutputHandler::setOutputBuffer(OutputBuffer& buffer)
103    {
104        buffer.getStream() >> this->outputBuffer_->getStream().rdbuf();
105        this->outputBuffer_ = &buffer;
106    }
107
108    /**
109        @brief Overloaded << operator, redirects the output to the console and the logfile.
110        @param sb The streambuffer that should be shown in the console
111        @return A reference to the OutputHandler itself
112    */
113    OutputHandler& OutputHandler::operator<<(std::streambuf* sb)
114    {
115        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
116            std::cout << sb;
117
118        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
119        {
120            this->logfile_ << sb;
121            this->logfile_.flush();
122        }
123
124        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
125            (*this->outputBuffer_) << sb;
126
127        return *this;
128    }
129
130    /**
131        @brief Overloaded << operator, redirects the output to the console, the logfile and the ingame shell.
132        @param manipulator A function, manipulating the outstream.
133        @return A reference to the OutputHandler itself
134    */
135    OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&))
136    {
137        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
138            manipulator(std::cout);
139
140        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
141        {
142            manipulator(this->logfile_);
143            this->logfile_.flush();
144        }
145
146        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
147            (*this->outputBuffer_) << manipulator;
148
149        return *this;
150    }
151
152    /**
153        @brief Overloaded << operator, redirects the output to the console, the logfile and the ingame shell.
154        @param manipulator A function, manipulating the outstream.
155        @return A reference to the OutputHandler itself
156    */
157    OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&))
158    {
159        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
160            manipulator(std::cout);
161
162        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
163        {
164            manipulator(this->logfile_);
165            this->logfile_.flush();
166        }
167
168        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
169            (*this->outputBuffer_) << manipulator;
170
171        return *this;
172    }
173
174    /**
175        @brief Overloaded << operator, redirects the output to the console, the logfile and the ingame shell.
176        @param manipulator A function, manipulating the outstream.
177        @return A reference to the OutputHandler itself
178    */
179    OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&))
180    {
181        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
182            manipulator(std::cout);
183
184        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
185        {
186            manipulator(this->logfile_);
187            this->logfile_.flush();
188        }
189
190        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
191            (*this->outputBuffer_) << manipulator;
192
193        return *this;
194    }
195}
Note: See TracBrowser for help on using the repository browser.