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 |
---|
31 | @ingroup Output |
---|
32 | @brief Declaration of the OutputManager class which receives output from orxonox::OutputStream |
---|
33 | and distributes it to all instances of orxonox::OutputListener. |
---|
34 | */ |
---|
35 | |
---|
36 | #ifndef _OutputManager_H__ |
---|
37 | #define _OutputManager_H__ |
---|
38 | |
---|
39 | #include "util/UtilPrereqs.h" |
---|
40 | |
---|
41 | #include <vector> |
---|
42 | #include <map> |
---|
43 | #include <memory> |
---|
44 | |
---|
45 | #include "OutputDefinitions.h" |
---|
46 | #include "AdditionalContextListener.h" |
---|
47 | |
---|
48 | namespace orxonox |
---|
49 | { |
---|
50 | /** |
---|
51 | @brief OutputManager acts as the center of the output system and is implemented as a singleton. |
---|
52 | |
---|
53 | All instances of OutputStream (and hence also the orxout() function) |
---|
54 | send their buffered output to OutputManager. OutputManager then |
---|
55 | distributes this output to all registered instances of OutputListener. |
---|
56 | |
---|
57 | For each listener OutputManager checks if it wants to receive output |
---|
58 | with the given level and context. OutputManager itself also maintains |
---|
59 | masks that define the accepted levels and concept. They are a |
---|
60 | combination of the masks of all output listeners. See the description |
---|
61 | of OutputListener for a more conclusive description of these masks. |
---|
62 | |
---|
63 | Additionally OutputManager is used to register output contexts. |
---|
64 | */ |
---|
65 | class _UtilExport OutputManager : public AdditionalContextListener |
---|
66 | { |
---|
67 | public: |
---|
68 | OutputManager(); |
---|
69 | OutputManager(const OutputManager&); |
---|
70 | virtual ~OutputManager(); |
---|
71 | |
---|
72 | static OutputManager& getInstance(); |
---|
73 | static OutputManager& getInstanceAndCreateListeners(); |
---|
74 | |
---|
75 | inline MemoryWriter* getMemoryWriter() { return this->memoryWriterInstance_; } |
---|
76 | inline ConsoleWriter* getConsoleWriter() { return this->consoleWriterInstance_; } |
---|
77 | inline LogWriter* getLogWriter() { return this->logWriterInstance_; } |
---|
78 | |
---|
79 | virtual void pushMessage(OutputLevel level, const OutputContextContainer& context, const std::string& message); |
---|
80 | |
---|
81 | virtual void registerListener(OutputListener* listener); |
---|
82 | virtual void unregisterListener(OutputListener* listener); |
---|
83 | |
---|
84 | virtual void updatedLevelMask(const OutputListener* listener) override |
---|
85 | { this->updateCombinedLevelMask(); } |
---|
86 | virtual void updatedAdditionalContextsLevelMask(const OutputListener* listener) override |
---|
87 | { this->updateCombinedAdditionalContextsLevelMask(); } |
---|
88 | virtual void updatedAdditionalContextsMask(const OutputListener* listener) override |
---|
89 | { this->updateCombinedAdditionalContextsMask(); } |
---|
90 | |
---|
91 | /** |
---|
92 | @brief Returns true if at least one of the output listeners will accept output with the given level and context. |
---|
93 | |
---|
94 | For the sake of performance, output messages with levels or |
---|
95 | contexts that are not accepted should be ignored or, even |
---|
96 | better, not generated at all. |
---|
97 | */ |
---|
98 | inline bool acceptsOutput(OutputLevel level, const OutputContextContainer& context) const |
---|
99 | { |
---|
100 | return (this->combinedLevelMask_ & level) || |
---|
101 | ((this->combinedAdditionalContextsLevelMask_ & level) && (this->combinedAdditionalContextsMask_ & context.mask)); |
---|
102 | } |
---|
103 | |
---|
104 | const OutputContextContainer& registerContext(const std::string& name, const std::string& subname = ""); |
---|
105 | |
---|
106 | const std::string& getLevelName(OutputLevel level) const; |
---|
107 | std::string getDefaultPrefix(OutputLevel level, const OutputContextContainer& context) const; |
---|
108 | |
---|
109 | inline const std::vector<OutputListener*>& getListeners() const |
---|
110 | { return this->listeners_; } |
---|
111 | |
---|
112 | inline OutputLevel getCombinedLevelMask() const { return this->combinedLevelMask_; } |
---|
113 | inline OutputLevel getCombinedAdditionalContextsLevelMask() const { return this->combinedAdditionalContextsLevelMask_; } |
---|
114 | inline OutputContextMask getCombinedAdditionalContextsMask() const { return this->combinedAdditionalContextsMask_; } |
---|
115 | |
---|
116 | private: |
---|
117 | void updateMasks(); |
---|
118 | void updateCombinedLevelMask(); |
---|
119 | void updateCombinedAdditionalContextsLevelMask(); |
---|
120 | void updateCombinedAdditionalContextsMask(); |
---|
121 | |
---|
122 | std::vector<OutputListener*> listeners_; ///< List of all registered output listeners |
---|
123 | |
---|
124 | OutputLevel combinedLevelMask_; ///< The combined mask of accepted levels of all listeners |
---|
125 | OutputLevel combinedAdditionalContextsLevelMask_; ///< The combined mask of accepted additional contexts levels of all listeners |
---|
126 | OutputContextMask combinedAdditionalContextsMask_; ///< The combined mask of accepted additional contexts of all listeners |
---|
127 | |
---|
128 | std::map<std::string, OutputContextMask> contextMasks_; ///< Contains all main-contexts and their masks |
---|
129 | std::map<std::string, OutputContextContainer> contextContainers_; ///< Contains all contexts including sub-contexts and their containers |
---|
130 | OutputContextSubID subcontextCounter_; ///< Counts the number of sub-contexts (and generates their IDs) |
---|
131 | |
---|
132 | bool isInitialized_; ///< Becomes true once the following instances were created |
---|
133 | MemoryWriter* memoryWriterInstance_; ///< The main instance of MemoryWriter, managed by OutputManager |
---|
134 | ConsoleWriter* consoleWriterInstance_; ///< The main instance of ConsoleWriter, managed by OutputManager |
---|
135 | LogWriter* logWriterInstance_; ///< The main instance of LogWriter, managed by OutputManager |
---|
136 | |
---|
137 | public: |
---|
138 | struct _UtilExport Testing |
---|
139 | { |
---|
140 | static std::shared_ptr<OutputManager>& getInstancePointer(); |
---|
141 | }; |
---|
142 | }; |
---|
143 | } |
---|
144 | |
---|
145 | #endif /* _OutputManager_H__ */ |
---|