Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/output/src/libraries/util/output/OutputManager.cc @ 8780

Last change on this file since 8780 was 8780, checked in by landauf, 13 years ago

added ConsoleWriter

  • Property svn:eol-style set to native
File size: 7.2 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#include "OutputManager.h"
30
31#include "MemoryWriter.h"
32#include "ConsoleWriter.h"
33#include "LogWriter.h"
34
35namespace orxonox
36{
37namespace test
38{
39    OutputManager::OutputManager()
40    {
41        this->combinedLevelMask_ = 0;
42        this->combinedContextMask_ = 0;
43    }
44
45    OutputManager::~OutputManager()
46    {
47    }
48
49    /*static*/ OutputManager& OutputManager::getInstance()
50    {
51        static OutputManager instance;
52        return instance;
53    }
54
55    /*static*/ OutputManager& OutputManager::getInstanceAndCreateListeners()
56    {
57        static OutputManager& instance = OutputManager::getInstance();
58
59        static MemoryWriter& memoryWriterInstance = MemoryWriter::getInstance(); (void)memoryWriterInstance;
60        static ConsoleWriter& consoleWriterInstance = ConsoleWriter::getInstance(); (void)consoleWriterInstance;
61        static LogWriter& logWriterInstance = LogWriter::getInstance(); (void)logWriterInstance;
62
63        return instance;
64    }
65
66    void OutputManager::pushMessage(OutputLevel level, OutputContext context, const std::string& message)
67    {
68        std::vector<std::string> lines;
69        for (size_t start = 0, end = 0; end != std::string::npos; start = end + 1)
70        {
71            end = message.find_first_of('\n', start);
72            lines.push_back(message.substr(start, end));
73        }
74
75        for (size_t i = 0; i < this->listeners_.size(); ++i)
76            this->listeners_[i]->unfilteredOutput(level, context, lines);
77    }
78
79    void OutputManager::registerListener(OutputListener* listener)
80    {
81        this->listeners_.push_back(listener);
82        this->updateMasks();
83    }
84
85    void OutputManager::unregisterListener(OutputListener* listener)
86    {
87        for (std::vector<OutputListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it)
88        {
89            if (*it == listener)
90            {
91                this->listeners_.erase(it);
92                break;
93            }
94        }
95        this->updateMasks();
96    }
97
98    void OutputManager::updateMasks()
99    {
100        this->updateCombinedLevelMask();
101        this->updateCombinedContextMask();
102    }
103
104    void OutputManager::updateCombinedLevelMask()
105    {
106        this->combinedLevelMask_ = 0;
107        for (size_t i = 0; i < this->listeners_.size(); ++i)
108            this->combinedLevelMask_ |= this->listeners_[i]->getLevelMask();
109    }
110
111    void OutputManager::updateCombinedContextMask()
112    {
113        this->combinedContextMask_ = 0;
114        for (size_t i = 0; i < this->listeners_.size(); ++i)
115            this->combinedContextMask_ |= this->listeners_[i]->getContextMask();
116    }
117
118    OutputContext OutputManager::registerContext(const std::string& name)
119    {
120        boost::bimap<OutputContext, std::string>::right_map::iterator it = this->contexts_.right.find(name);
121        if (it == this->contexts_.right.end())
122        {
123            OutputContext context = 0x1 << this->contexts_.size();
124            this->contexts_.insert(boost::bimap<OutputContext, std::string>::value_type(context, name));
125            return context;
126        }
127        else
128        {
129            return it->second;
130        }
131    }
132
133    OutputContext registerContext(const std::string& name)
134    {
135        return OutputManager::getInstance().registerContext(name);
136    }
137
138    const std::string& OutputManager::getLevelName(OutputLevel level) const
139    {
140        switch (level)
141        {
142            case level::none:               { static std::string name = "None"; return name; }
143            case level::debug_output:       { static std::string name = "Debug"; return name; }
144            case level::user_error:         { static std::string name = "Error"; return name; }
145            case level::user_warning:       { static std::string name = "Warning"; return name; }
146            case level::user_status:        { static std::string name = "Status"; return name; }
147            case level::user_info:          { static std::string name = "Info"; return name; }
148            case level::internal_error:     { static std::string name = "Error (internal)"; return name; }
149            case level::internal_warning:   { static std::string name = "Warning (internal)"; return name; }
150            case level::internal_status:    { static std::string name = "Status (internal)"; return name; }
151            case level::internal_info:      { static std::string name = "Info (internal)"; return name; }
152            case level::verbose:            { static std::string name = "Verbose"; return name; }
153            case level::verbose_more:       { static std::string name = "Verbose (more)"; return name; }
154            case level::verbose_ultra:      { static std::string name = "Verbose (ultra)"; return name; }
155            default:                        { static std::string name = ""; return name; }
156        }
157    }
158
159    const std::string& OutputManager::getContextName(OutputContext context) const
160    {
161        if (context != context::undefined())
162        {
163            boost::bimap<OutputContext, std::string>::left_map::const_iterator it = this->contexts_.left.find(context);
164            if (it != this->contexts_.left.end())
165                return it->second;
166        }
167        return BLANKSTRING;
168    }
169
170    std::string OutputManager::getComposedContextName(OutputContext context) const
171    {
172        std::string name;
173        size_t counter = 0;
174        for (OutputContext context_test = 0x1; context_test != 0x0; context_test = context_test << 1)
175        {
176            if (context & context_test)
177            {
178                boost::bimap<OutputContext, std::string>::left_map::const_iterator it = this->contexts_.left.find(context_test);
179                if (it != this->contexts_.left.end())
180                {
181                    if (counter)
182                        name += ", ";
183
184                    name += it->second;
185                    ++counter;
186                }
187            }
188        }
189        return name;
190    }
191
192    std::string OutputManager::getDefaultPrefix(OutputLevel level, OutputContext context) const
193    {
194        std::string prefix = this->getLevelName(level) + ": ";
195        if (context != context::undefined())
196        {
197            std::string context_name = this->getContextName(context);
198            if (context_name == "")
199                context_name = this->getComposedContextName(context);
200            prefix += "[" + context_name + "] ";
201        }
202        return prefix;
203    }
204}
205}
Note: See TracBrowser for help on using the repository browser.