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