SUMO - Simulation of Urban MObility
MsgHandler.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2003-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Retrieves messages about the process and gives them further to output
18 /****************************************************************************/
19 #ifndef MsgHandler_h
20 #define MsgHandler_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <vector>
30 #include <iostream>
32 
33 
34 // ===========================================================================
35 // class declarations
36 // ===========================================================================
37 class AbstractMutex;
38 class OutputDevice;
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
47 class MsgHandler {
48 public:
54  enum MsgType {
65  };
66 
69 
72 
74  static MsgHandler* getErrorInstance();
75 
77  static MsgHandler* getDebugInstance();
78 
81 
83  static void enableDebugMessages(bool enable);
84 
86  static void enableDebugGLMessages(bool enable);
87 
89  static inline bool writeDebugMessages() {
90  return myWriteDebugMessages;
91  }
92 
94  static inline bool writeDebugGLMessages() {
96  }
97 
100 
102  static void initOutputOptions();
103 
105  static void cleanupOnEnd();
106 
108  void inform(std::string msg, bool addType = true);
109 
117  void beginProcessMsg(std::string msg, bool addType = true);
118 
120  void endProcessMsg(std::string msg);
121 
123  void clear();
124 
126  void addRetriever(OutputDevice* retriever);
127 
129  void removeRetriever(OutputDevice* retriever);
130 
132  bool isRetriever(OutputDevice* retriever) const;
133 
135  bool wasInformed() const;
136 
138  static void assignLock(AbstractMutex* lock);
139 
143  template <class T>
144  MsgHandler& operator<<(const T& t) {
145  // inform all other receivers
146  for (auto i : myRetrievers) {
147  (*i) << t;
148  }
149  return *this;
150  }
151 
152 protected:
154  inline std::string build(const std::string& msg, bool addType) {
155  if (addType) {
156  switch (myType) {
157  case MT_MESSAGE:
158  break;
159  case MT_WARNING:
160  return "Warning: " + msg;
161  break;
162  case MT_ERROR:
163  return "Error: " + msg;
164  break;
165  case MT_DEBUG:
166  return "Debug: " + msg;
167  break;
168  case MT_GLDEBUG:
169  return "GLDebug: " + msg;
170  break;
171  default:
172  break;
173  }
174  }
175  return msg;
176  }
177 
178 
179 private:
181  MsgHandler(MsgType type);
182 
184  ~MsgHandler();
185 
188 
191 
194 
197 
200 
203 
206 
207 private:
210 
213 
215  typedef std::vector<OutputDevice*> RetrieverVector;
216 
218  RetrieverVector myRetrievers;
219 
220 private:
222  MsgHandler(const MsgHandler& s) = delete;
223 
225  MsgHandler& operator=(const MsgHandler& s) = delete;
226 
231  static bool myWriteDebugMessages;
233 };
234 
235 
236 
237 
238 
239 // ===========================================================================
240 // global definitions
241 // ===========================================================================
242 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
243 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
244 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string("..."));
245 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
246 #define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(SysUtils::getCurrentMillis() - before) + "ms).");
247 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
248 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
249 #define WRITE_DEBUG(msg) if(MsgHandler::writeDebugMessages()){MsgHandler::getDebugInstance()->inform(msg);};
250 #define WRITE_GLDEBUG(msg) if(MsgHandler::writeDebugGLMessages()){MsgHandler::getGLDebugInstance()->inform(msg);};
251 
252 #endif
253 
254 /****************************************************************************/
255 
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:144
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:67
std::vector< OutputDevice * > RetrieverVector
Definition of the list of retrievers to inform.
Definition: MsgHandler.h:215
The message is only something to show.
Definition: MsgHandler.h:56
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:232
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
MsgType myType
The type of the instance.
Definition: MsgHandler.h:209
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:214
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:292
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:94
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:187
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:196
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:94
An abstract class for encapsulating mutex implementations.
Definition: AbstractMutex.h:42
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:185
static void assignLock(AbstractMutex *lock)
Sets the lock to use The lock will not be deleted.
Definition: MsgHandler.cpp:313
RetrieverVector myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:218
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:307
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance ...
Definition: MsgHandler.cpp:220
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:231
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:272
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:190
~MsgHandler()
destructor
Definition: MsgHandler.cpp:302
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:85
static AbstractMutex * myLock
The lock if any has to be used. The lock will not be deleted.
Definition: MsgHandler.h:205
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:199
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:58
void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:136
The message is a warning.
Definition: MsgHandler.h:58
static bool writeDebugMessages()
check whether to enable/disable debug messages
Definition: MsgHandler.h:89
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:199
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:202
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:113
MsgHandler & operator=(const MsgHandler &s)=delete
invalid assignment operator
The message is an debug.
Definition: MsgHandler.h:62
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:154
bool myWasInformed
information wehther an error occurred at all
Definition: MsgHandler.h:212
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:108
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:103
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:193
void clear()
Clears information whether an error occurred previously.
Definition: MsgHandler.cpp:173
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:239
The message is an debug.
Definition: MsgHandler.h:64
The message is an error.
Definition: MsgHandler.h:60
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:155