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