[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 | |
---|
[8848] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Implementation of the OutputManager singleton. |
---|
| 32 | */ |
---|
| 33 | |
---|
[8765] | 34 | #include "OutputManager.h" |
---|
| 35 | |
---|
[8774] | 36 | #include "MemoryWriter.h" |
---|
[8780] | 37 | #include "ConsoleWriter.h" |
---|
[8774] | 38 | #include "LogWriter.h" |
---|
[8833] | 39 | #include "util/Output.h" |
---|
[8801] | 40 | #include "util/StringUtils.h" |
---|
[8765] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[8848] | 44 | /** |
---|
| 45 | @brief Constructor, initializes all values. |
---|
| 46 | */ |
---|
[8765] | 47 | OutputManager::OutputManager() |
---|
| 48 | { |
---|
[8808] | 49 | this->combinedLevelMask_ = level::none; |
---|
[8833] | 50 | this->combinedAdditionalContextsLevelMask_ = level::none; |
---|
| 51 | this->combinedAdditionalContextsMask_ = context::none; |
---|
| 52 | |
---|
| 53 | this->subcontextCounter_ = 0; |
---|
[8765] | 54 | } |
---|
| 55 | |
---|
[8848] | 56 | /** |
---|
| 57 | @brief Destructor. |
---|
| 58 | */ |
---|
[8765] | 59 | OutputManager::~OutputManager() |
---|
| 60 | { |
---|
| 61 | } |
---|
| 62 | |
---|
[8848] | 63 | /** |
---|
| 64 | @brief Returns the only existing instance of the OutputManager singleton. |
---|
| 65 | */ |
---|
[8765] | 66 | /*static*/ OutputManager& OutputManager::getInstance() |
---|
| 67 | { |
---|
[8776] | 68 | static OutputManager instance; |
---|
| 69 | return instance; |
---|
| 70 | } |
---|
[8774] | 71 | |
---|
[8848] | 72 | /** |
---|
| 73 | @brief Returns the only existing instance of the OutputManager singleton |
---|
| 74 | and ensures that the most important output listeners exist. |
---|
| 75 | |
---|
| 76 | You should use this function if you send output to OutputManager and want |
---|
| 77 | to be sure that the most important output listeners exist. Don't use it |
---|
| 78 | elsewhere inside the output system to avoid circular calls. |
---|
| 79 | */ |
---|
[8776] | 80 | /*static*/ OutputManager& OutputManager::getInstanceAndCreateListeners() |
---|
| 81 | { |
---|
| 82 | static OutputManager& instance = OutputManager::getInstance(); |
---|
| 83 | |
---|
[8774] | 84 | static MemoryWriter& memoryWriterInstance = MemoryWriter::getInstance(); (void)memoryWriterInstance; |
---|
[8780] | 85 | static ConsoleWriter& consoleWriterInstance = ConsoleWriter::getInstance(); (void)consoleWriterInstance; |
---|
[8774] | 86 | static LogWriter& logWriterInstance = LogWriter::getInstance(); (void)logWriterInstance; |
---|
| 87 | |
---|
[8765] | 88 | return instance; |
---|
| 89 | } |
---|
| 90 | |
---|
[8848] | 91 | /** |
---|
| 92 | @brief Sends an output message to all output listeners. |
---|
| 93 | @param level The level of the message |
---|
| 94 | @param context The context of the message |
---|
| 95 | @param message The output message (may contain '\\n') |
---|
| 96 | |
---|
| 97 | This function splits the message into lines (if it contains '\\n') and |
---|
| 98 | sends it to the output listeners. They may ignore the message if it |
---|
| 99 | doesn't match their level- and context-masks. |
---|
| 100 | */ |
---|
[8833] | 101 | void OutputManager::pushMessage(OutputLevel level, const OutputContextContainer& context, const std::string& message) |
---|
[8765] | 102 | { |
---|
[8772] | 103 | std::vector<std::string> lines; |
---|
[8801] | 104 | vectorize(message, '\n', &lines); |
---|
[8772] | 105 | |
---|
[8765] | 106 | for (size_t i = 0; i < this->listeners_.size(); ++i) |
---|
[8774] | 107 | this->listeners_[i]->unfilteredOutput(level, context, lines); |
---|
[8765] | 108 | } |
---|
| 109 | |
---|
[8848] | 110 | /** |
---|
| 111 | @brief Adds an output listener to the list of listeners. |
---|
| 112 | */ |
---|
[8765] | 113 | void OutputManager::registerListener(OutputListener* listener) |
---|
| 114 | { |
---|
| 115 | this->listeners_.push_back(listener); |
---|
| 116 | this->updateMasks(); |
---|
| 117 | } |
---|
| 118 | |
---|
[8848] | 119 | /** |
---|
| 120 | @brief Removes an output listener from the list of listeners. |
---|
| 121 | */ |
---|
[8765] | 122 | void OutputManager::unregisterListener(OutputListener* listener) |
---|
| 123 | { |
---|
| 124 | for (std::vector<OutputListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) |
---|
| 125 | { |
---|
| 126 | if (*it == listener) |
---|
| 127 | { |
---|
| 128 | this->listeners_.erase(it); |
---|
| 129 | break; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | this->updateMasks(); |
---|
| 133 | } |
---|
| 134 | |
---|
[8848] | 135 | /** |
---|
| 136 | @brief Updates all three combined level- and context-masks. |
---|
| 137 | */ |
---|
[8765] | 138 | void OutputManager::updateMasks() |
---|
| 139 | { |
---|
| 140 | this->updateCombinedLevelMask(); |
---|
[8833] | 141 | this->updateCombinedAdditionalContextsLevelMask(); |
---|
| 142 | this->updateCombinedAdditionalContextsMask(); |
---|
[8765] | 143 | } |
---|
| 144 | |
---|
[8848] | 145 | /** |
---|
| 146 | @brief Updates the combined level mask. The masks of all listeners are ORed to form the combined mask. |
---|
| 147 | */ |
---|
[8765] | 148 | void OutputManager::updateCombinedLevelMask() |
---|
| 149 | { |
---|
[8808] | 150 | int mask = 0; |
---|
[8765] | 151 | for (size_t i = 0; i < this->listeners_.size(); ++i) |
---|
[8808] | 152 | mask |= this->listeners_[i]->getLevelMask(); |
---|
| 153 | this->combinedLevelMask_ = static_cast<OutputLevel>(mask); |
---|
[8765] | 154 | } |
---|
| 155 | |
---|
[8848] | 156 | /** |
---|
| 157 | @brief Updates the combined additional contexts level mask. The masks of all listeners are ORed to form the combined mask. |
---|
| 158 | */ |
---|
[8833] | 159 | void OutputManager::updateCombinedAdditionalContextsLevelMask() |
---|
[8765] | 160 | { |
---|
[8833] | 161 | int mask = 0; |
---|
[8765] | 162 | for (size_t i = 0; i < this->listeners_.size(); ++i) |
---|
[8833] | 163 | mask |= this->listeners_[i]->getAdditionalContextsLevelMask(); |
---|
| 164 | this->combinedAdditionalContextsLevelMask_ = static_cast<OutputLevel>(mask); |
---|
[8765] | 165 | } |
---|
[8771] | 166 | |
---|
[8848] | 167 | /** |
---|
| 168 | @brief Updates the combined additional contexts mask. The masks of all listeners are ORed to form the combined mask. |
---|
| 169 | */ |
---|
[8833] | 170 | void OutputManager::updateCombinedAdditionalContextsMask() |
---|
[8771] | 171 | { |
---|
[8833] | 172 | this->combinedAdditionalContextsMask_ = 0; |
---|
| 173 | for (size_t i = 0; i < this->listeners_.size(); ++i) |
---|
| 174 | this->combinedAdditionalContextsMask_ |= this->listeners_[i]->getAdditionalContextsMask(); |
---|
| 175 | } |
---|
| 176 | |
---|
[8848] | 177 | /** |
---|
| 178 | @brief Registers a context (or sub-context) and returns the container which identifies the context. |
---|
| 179 | @param name The name of the context |
---|
| 180 | @param subname The name of the sub-context (or "" if it is not a sub-context) |
---|
| 181 | |
---|
| 182 | If the context doesn't exist, it gets created. Otherwise the existing instance is returned. |
---|
| 183 | */ |
---|
[8833] | 184 | const OutputContextContainer& OutputManager::registerContext(const std::string& name, const std::string& subname) |
---|
| 185 | { |
---|
[8848] | 186 | // the full name of a context is a combination of name and subname with "::" in between |
---|
[8833] | 187 | std::string full_name = name; |
---|
| 188 | if (subname != "") |
---|
| 189 | full_name += "::" + subname; |
---|
| 190 | |
---|
[8848] | 191 | // check if the context already exists (and return it if it does) |
---|
[8833] | 192 | std::map<std::string, OutputContextContainer>::iterator it_container = this->contextContainers_.find(full_name); |
---|
| 193 | if (it_container != this->contextContainers_.end()) |
---|
| 194 | return it_container->second; |
---|
| 195 | |
---|
[8848] | 196 | // create a new context container |
---|
[8833] | 197 | OutputContextContainer container; |
---|
| 198 | container.name = full_name; |
---|
| 199 | |
---|
[8848] | 200 | // check if the mask of the main-context already exists |
---|
[8833] | 201 | std::map<std::string, OutputContextMask>::iterator it_mask = this->contextMasks_.find(name); |
---|
| 202 | if (it_mask != this->contextMasks_.end()) |
---|
[8771] | 203 | { |
---|
[8848] | 204 | // the mask exists, assign it to the container |
---|
[8833] | 205 | container.mask = it_mask->second; |
---|
[8771] | 206 | } |
---|
| 207 | else |
---|
| 208 | { |
---|
[8848] | 209 | // the mask doesn't exist, create it. It's a binary mask. The n-th main-context is defined by the n-th bit in the mask. |
---|
[8833] | 210 | container.mask = static_cast<OutputContextMask>(0x1) << this->contextMasks_.size(); |
---|
| 211 | this->contextMasks_[name] = container.mask; |
---|
| 212 | |
---|
| 213 | if (container.mask == 0) |
---|
| 214 | orxout(internal_warning) << "More than " << sizeof(OutputContextMask) * 8 << " output contexts defined. Context '" << name << "' might not get filtered correctly" << endl; |
---|
[8771] | 215 | } |
---|
[8833] | 216 | |
---|
[8848] | 217 | // if the context is a sub-context, assign a unique ID. |
---|
[8833] | 218 | if (subname == "") |
---|
| 219 | container.sub_id = context::no_subcontext; |
---|
| 220 | else |
---|
| 221 | container.sub_id = ++this->subcontextCounter_; // start with 1 |
---|
| 222 | |
---|
[8848] | 223 | // add the new context to the map and return it |
---|
[8833] | 224 | return (this->contextContainers_[full_name] = container); |
---|
[8771] | 225 | } |
---|
| 226 | |
---|
[8848] | 227 | /** |
---|
| 228 | @brief Static function, shortcut to OutputManager::registerContext(). |
---|
| 229 | The function is declared in OutputDefinitions.h. |
---|
| 230 | */ |
---|
[8833] | 231 | const OutputContextContainer& registerContext(const std::string& name, const std::string& subname) |
---|
[8771] | 232 | { |
---|
[8833] | 233 | return OutputManager::getInstance().registerContext(name, subname); |
---|
[8771] | 234 | } |
---|
| 235 | |
---|
[8848] | 236 | /** |
---|
| 237 | @brief Returns a human readable string for each output level. |
---|
| 238 | */ |
---|
[8771] | 239 | const std::string& OutputManager::getLevelName(OutputLevel level) const |
---|
| 240 | { |
---|
| 241 | switch (level) |
---|
| 242 | { |
---|
[8848] | 243 | // using static cache variables for speed |
---|
[8771] | 244 | case level::none: { static std::string name = "None"; return name; } |
---|
[8805] | 245 | case level::message: { static std::string name = "Message"; return name; } |
---|
[8771] | 246 | case level::debug_output: { static std::string name = "Debug"; return name; } |
---|
| 247 | case level::user_error: { static std::string name = "Error"; return name; } |
---|
| 248 | case level::user_warning: { static std::string name = "Warning"; return name; } |
---|
| 249 | case level::user_status: { static std::string name = "Status"; return name; } |
---|
| 250 | case level::user_info: { static std::string name = "Info"; return name; } |
---|
| 251 | case level::internal_error: { static std::string name = "Error (internal)"; return name; } |
---|
| 252 | case level::internal_warning: { static std::string name = "Warning (internal)"; return name; } |
---|
| 253 | case level::internal_status: { static std::string name = "Status (internal)"; return name; } |
---|
| 254 | case level::internal_info: { static std::string name = "Info (internal)"; return name; } |
---|
| 255 | case level::verbose: { static std::string name = "Verbose"; return name; } |
---|
| 256 | case level::verbose_more: { static std::string name = "Verbose (more)"; return name; } |
---|
| 257 | case level::verbose_ultra: { static std::string name = "Verbose (ultra)"; return name; } |
---|
| 258 | default: { static std::string name = ""; return name; } |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
[8848] | 262 | /** |
---|
| 263 | @brief Returns a string containing the name of the level and the context (if any) which |
---|
| 264 | can be prepended to an output message if it is written to the console or the log file. |
---|
| 265 | */ |
---|
[8833] | 266 | std::string OutputManager::getDefaultPrefix(OutputLevel level, const OutputContextContainer& context) const |
---|
[8771] | 267 | { |
---|
[8848] | 268 | // "undefined" context is ignored because it's used implicitly if no explicit context is defined |
---|
[8833] | 269 | static OutputContextMask undefined_mask = context::undefined().mask; |
---|
[8772] | 270 | |
---|
[8833] | 271 | std::string prefix = this->getLevelName(level) + ": "; |
---|
| 272 | if (context.mask != undefined_mask) |
---|
| 273 | prefix += "[" + context.name + "] "; |
---|
[8799] | 274 | |
---|
[8774] | 275 | return prefix; |
---|
| 276 | } |
---|
[8765] | 277 | } |
---|