Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/OutputHandler.cc @ 1277

Last change on this file since 1277 was 1230, checked in by landauf, 17 years ago

added a tcl-thread, but there are still some bugs and problems

File size: 5.6 KB
RevLine 
[684]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[684]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
[871]29/**
30    @file OutputHandler.cc
31    @brief Implementation of the OutputHandler class.
32*/
33
[1062]34#include "OutputHandler.h"
[1052]35#include "CoreSettings.h"
36#include "ConsoleCommand.h"
[684]37
38namespace orxonox
39{
[1230]40    ConsoleCommandShortcutGeneric(log,     createExecutor(createFunctor(&OutputHandler::log),     "log",     AccessLevel::None));
41    ConsoleCommandShortcutGeneric(error,   createExecutor(createFunctor(&OutputHandler::error),   "error",   AccessLevel::None));
42    ConsoleCommandShortcutGeneric(warning, createExecutor(createFunctor(&OutputHandler::warning), "warning", AccessLevel::None));
43    ConsoleCommandShortcutGeneric(info,    createExecutor(createFunctor(&OutputHandler::info),    "info",    AccessLevel::None));
44    ConsoleCommandShortcutGeneric(debug,   createExecutor(createFunctor(&OutputHandler::debug),   "debug",   AccessLevel::None));
[1052]45
[684]46    /**
47        @brief Constructor: Opens the logfile and writes the first line.
48        @param logfilename The name of the logfile
49    */
[715]50    OutputHandler::OutputHandler(const std::string& logfilename)
[684]51    {
52        this->logfilename_ = logfilename;
53        this->logfile_.open(this->logfilename_.c_str(), std::fstream::out);
[1052]54        this->logfile_ << "Started log at yyyy/mm/dd hh:mm:ss" << std::endl;
[684]55        this->logfile_.flush();
56    }
57
58    /**
59        @brief Destructor: Writes the last line to the logfile and closes it.
60    */
61    OutputHandler::~OutputHandler()
62    {
63        this->logfile_ << "Closed log" << std::endl;
64        this->logfile_.close();
65    }
66
67    /**
[871]68        @brief Returns a reference to the only existing instance of the OutputHandler class.
69        @return The instance
[684]70    */
71    OutputHandler& OutputHandler::getOutStream()
72    {
73        static OutputHandler orxout("orxonox.log");
74        return orxout;
75    }
76
77    /**
[699]78        @brief Returns the soft debug level for a given output device.
79        @param device The output device
80        @return The debug level
81    */
82    int OutputHandler::getSoftDebugLevel(OutputHandler::OutputDevice device)
83    {
[1052]84        return CoreSettings::getSoftDebugLevel(device);
[699]85    }
86
87    /**
[684]88        @brief Overloaded << operator, redirects the output to the console and the logfile.
89        @param sb The streambuffer that should be shown in the console
90        @return A reference to the OutputHandler itself
91    */
92    OutputHandler& OutputHandler::operator<<(std::streambuf* sb)
93    {
[699]94        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
95            std::cout << sb;
96
97        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
98        {
99            this->logfile_ << sb;
100            this->logfile_.flush();
101        }
102
[684]103        return *this;
104    }
105
106    /**
107        @brief Overloaded << operator, redirects the output to the console and the logfile.
108        @param manipulator A function, manipulating the outstream.
109        @return A reference to the OutputHandler itself
110    */
111    OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&))
112    {
[699]113        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
114            manipulator(std::cout);
115
116        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
117        {
118            manipulator(this->logfile_);
119            this->logfile_.flush();
120        }
121
[684]122        return *this;
123    }
124
125    /**
126        @brief Overloaded << operator, redirects the output to the console and the logfile.
127        @param manipulator A function, manipulating the outstream.
128        @return A reference to the OutputHandler itself
129    */
130    OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&))
131    {
[699]132        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
133            manipulator(std::cout);
134
135        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
136        {
137            manipulator(this->logfile_);
138            this->logfile_.flush();
139        }
140
[684]141        return *this;
142    }
143
144    /**
145        @brief Overloaded << operator, redirects the output to the console and the logfile.
146        @param manipulator A function, manipulating the outstream.
147        @return A reference to the OutputHandler itself
148    */
149    OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&))
150    {
[699]151        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
152            manipulator(std::cout);
153
154        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
155        {
156            manipulator(this->logfile_);
157            this->logfile_.flush();
158        }
159
[684]160        return *this;
161    }
[699]162}
Note: See TracBrowser for help on using the repository browser.