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>
31 
32 
33 // ===========================================================================
34 // class declarations
35 // ===========================================================================
36 class AbstractMutex;
37 class OutputDevice;
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
46 class MsgHandler {
47 public:
53  enum MsgType {
64  };
65 
68 
71 
73  static MsgHandler* getErrorInstance();
74 
76  static MsgHandler* getDebugInstance();
77 
80 
82  static void enableDebugMessages(bool enable);
83 
85  static void enableDebugGLMessages(bool enable);
86 
88  static inline bool writeDebugMessages() {
89  return myWriteDebugMessages;
90  }
91 
93  static inline bool writeDebugGLMessages() {
95  }
96 
99 
101  static void initOutputOptions();
102 
104  static void cleanupOnEnd();
105 
107  void inform(std::string msg, bool addType = true);
108 
116  void beginProcessMsg(std::string msg, bool addType = true);
117 
119  void endProcessMsg(std::string msg);
120 
122  void clear();
123 
125  void addRetriever(OutputDevice* retriever);
126 
128  void removeRetriever(OutputDevice* retriever);
129 
131  bool isRetriever(OutputDevice* retriever) const;
132 
134  bool wasInformed() const;
135 
137  static void assignLock(AbstractMutex* lock);
138 
142  template <class T>
143  MsgHandler& operator<<(const T& t) {
144  // inform all other receivers
145  for (auto i : myRetrievers) {
146  (*i) << t;
147  }
148  return *this;
149  }
150 
151 protected:
153  inline std::string build(const std::string& msg, bool addType) {
154  if (addType) {
155  switch (myType) {
156  case MT_MESSAGE:
157  break;
158  case MT_WARNING:
159  return "Warning: " + msg;
160  break;
161  case MT_ERROR:
162  return "Error: " + msg;
163  break;
164  case MT_DEBUG:
165  return "Debug: " + msg;
166  break;
167  case MT_GLDEBUG:
168  return "GLDebug: " + msg;
169  break;
170  default:
171  break;
172  }
173  }
174  return msg;
175  }
176 
177 
178 private:
180  MsgHandler(MsgType type);
181 
183  ~MsgHandler();
184 
187 
190 
193 
196 
199 
202 
205 
206 private:
209 
212 
214  typedef std::vector<OutputDevice*> RetrieverVector;
215 
217  RetrieverVector myRetrievers;
218 
219 private:
221  MsgHandler(const MsgHandler& s) = delete;
222 
224  MsgHandler& operator=(const MsgHandler& s) = delete;
225 
230  static bool myWriteDebugMessages;
232 };
233 
234 
235 
236 
237 
238 // ===========================================================================
239 // global definitions
240 // ===========================================================================
241 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
242 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
243 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string("..."));
244 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
245 #define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(SysUtils::getCurrentMillis() - before) + "ms).");
246 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
247 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
248 #define WRITE_DEBUG(msg) if(MsgHandler::writeDebugMessages()){MsgHandler::getDebugInstance()->inform(msg);};
249 #define WRITE_GLDEBUG(msg) if(MsgHandler::writeDebugGLMessages()){MsgHandler::getGLDebugInstance()->inform(msg);};
250 
251 #endif
252 
253 /****************************************************************************/
254 
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:143
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:214
The message is only something to show.
Definition: MsgHandler.h:55
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:231
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
MsgType myType
The type of the instance.
Definition: MsgHandler.h:208
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:186
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:195
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:93
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:217
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:230
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:272
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:189
~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:204
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:57
static bool writeDebugMessages()
check whether to enable/disable debug messages
Definition: MsgHandler.h:88
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:198
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:201
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:61
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:153
bool myWasInformed
information wehther an error occurred at all
Definition: MsgHandler.h:211
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:192
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:63
The message is an error.
Definition: MsgHandler.h:59
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:155