Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

The InGameConsole is now using the new Shell. There's a clear separation between the two classes: InGameConsole implements all graphical parts of the console (and has a tick), while Shell handles the internal actions (like listening on input and output changes). That's why InGameConsole has no longer it's own InputBuffer and doesn't care about command-executing or anything else.

There are currently three new features:

  • Every output through COUT(level) is now visible in the InGameConsole, provided the configured output-level for the shell matches. default: 1 (only forced output and errors)
  • The cursor in the input-line is movable with the left and right arrow keys (home and end works too)
  • You can scroll through all output-lines by pressing page up and page down

There's another feature to come, providing a command history, accessible with up and down arrow keys, but I couldn't finish it yet, because there's still a bug, causing Orxonox to scroll through the entire memory - that's maybe a bit too much history ;)

File size: 6.3 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 "CoreSettings.h"
36#include "ConsoleCommand.h"
37
38namespace orxonox
39{
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));
45
46    /**
47        @brief Constructor: Opens the logfile and writes the first line.
48        @param logfilename The name of the logfile
49    */
50    OutputHandler::OutputHandler(const std::string& logfilename)
51    {
52        this->logfilename_ = logfilename;
53        this->logfile_.open(this->logfilename_.c_str(), std::fstream::out);
54        this->logfile_ << "Started log at yyyy/mm/dd hh:mm:ss" << std::endl;
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    /**
68        @brief Returns a reference to the only existing instance of the OutputHandler class.
69        @return The instance
70    */
71    OutputHandler& OutputHandler::getOutStream()
72    {
73        static OutputHandler orxout("orxonox.log");
74        return orxout;
75    }
76
77    /**
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    {
84        return CoreSettings::getSoftDebugLevel(device);
85    }
86
87    /**
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    {
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
103        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
104            Shell::getInstance().getOutputBuffer() << sb;
105
106        return *this;
107    }
108
109    /**
110        @brief Overloaded << operator, redirects the output to the console and the logfile.
111        @param manipulator A function, manipulating the outstream.
112        @return A reference to the OutputHandler itself
113    */
114    OutputHandler& OutputHandler::operator<<(std::ostream& (*manipulator)(std::ostream&))
115    {
116        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
117            manipulator(std::cout);
118
119        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
120        {
121            manipulator(this->logfile_);
122            this->logfile_.flush();
123        }
124
125        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
126            Shell::getInstance().getOutputBuffer() << manipulator;
127
128        return *this;
129    }
130
131    /**
132        @brief Overloaded << operator, redirects the output to the console and the logfile.
133        @param manipulator A function, manipulating the outstream.
134        @return A reference to the OutputHandler itself
135    */
136    OutputHandler& OutputHandler::operator<<(std::ios& (*manipulator)(std::ios&))
137    {
138        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
139            manipulator(std::cout);
140
141        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
142        {
143            manipulator(this->logfile_);
144            this->logfile_.flush();
145        }
146
147        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
148            Shell::getInstance().getOutputBuffer() << manipulator;
149
150        return *this;
151    }
152
153    /**
154        @brief Overloaded << operator, redirects the output to the console and the logfile.
155        @param manipulator A function, manipulating the outstream.
156        @return A reference to the OutputHandler itself
157    */
158    OutputHandler& OutputHandler::operator<<(std::ios_base& (*manipulator)(std::ios_base&))
159    {
160        if (getSoftDebugLevel(OutputHandler::LD_Console) >= this->outputLevel_)
161            manipulator(std::cout);
162
163        if (getSoftDebugLevel(OutputHandler::LD_Logfile) >= this->outputLevel_)
164        {
165            manipulator(this->logfile_);
166            this->logfile_.flush();
167        }
168
169        if (OutputHandler::getSoftDebugLevel(OutputHandler::LD_Shell) >= this->outputLevel_)
170            Shell::getInstance().getOutputBuffer() << manipulator;
171
172        return *this;
173    }
174}
Note: See TracBrowser for help on using the repository browser.