Eclipse 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-2019 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 
27 #include <string>
28 #include <vector>
29 #include <iostream>
30 
31 
32 // ===========================================================================
33 // class declarations
34 // ===========================================================================
35 class OutputDevice;
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
44 class MsgHandler {
45 public:
51  enum MsgType {
62  };
63 
64 private:
65  typedef MsgHandler* (*Factory)(MsgType);
66 
67 public:
69  static void setFactory(Factory func) {
70  myFactory = func;
71  }
72 
75 
78 
80  static MsgHandler* getErrorInstance();
81 
83  static MsgHandler* getDebugInstance();
84 
87 
89  static void enableDebugMessages(bool enable);
90 
92  static void enableDebugGLMessages(bool enable);
93 
95  static inline bool writeDebugMessages() {
96  return myWriteDebugMessages;
97  }
98 
100  static inline bool writeDebugGLMessages() {
101  return myWriteDebugGLMessages;
102  }
103 
106 
108  static void initOutputOptions();
109 
111  static void cleanupOnEnd();
112 
114  virtual void inform(std::string msg, bool addType = true);
115 
123  virtual void beginProcessMsg(std::string msg, bool addType = true);
124 
126  virtual void endProcessMsg(std::string msg);
127 
129  virtual void clear();
130 
132  virtual void addRetriever(OutputDevice* retriever);
133 
135  virtual void removeRetriever(OutputDevice* retriever);
136 
138  bool isRetriever(OutputDevice* retriever) const;
139 
141  bool wasInformed() const;
142 
146  template <class T>
147  MsgHandler& operator<<(const T& t) {
148  // inform all other receivers
149  for (auto i : myRetrievers) {
150  (*i) << t;
151  }
152  return *this;
153  }
154 
155 protected:
157  inline std::string build(const std::string& msg, bool addType) {
158  if (addType) {
159  switch (myType) {
160  case MT_MESSAGE:
161  break;
162  case MT_WARNING:
163  return "Warning: " + msg;
164  break;
165  case MT_ERROR:
166  return "Error: " + msg;
167  break;
168  case MT_DEBUG:
169  return "Debug: " + msg;
170  break;
171  case MT_GLDEBUG:
172  return "GLDebug: " + msg;
173  break;
174  default:
175  break;
176  }
177  }
178  return msg;
179  }
180 
181 
183  MsgHandler(MsgType type);
184 
186  virtual ~MsgHandler();
187 
188 private:
191 
194 
197 
200 
203 
206 
209 
210 private:
213 
216 
218  std::vector<OutputDevice*> 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 // global definitions
238 // ===========================================================================
239 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
240 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
241 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string("..."));
242 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
243 #define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(SysUtils::getCurrentMillis() - before) + "ms).");
244 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
245 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
246 #define WRITE_DEBUG(msg) if(MsgHandler::writeDebugMessages()){MsgHandler::getDebugInstance()->inform(msg);};
247 #define WRITE_GLDEBUG(msg) if(MsgHandler::writeDebugGLMessages()){MsgHandler::getGLDebugInstance()->inform(msg);};
248 
249 #endif
250 
251 /****************************************************************************/
MsgHandler::MsgType
MsgType
Definition: MsgHandler.h:51
MsgHandler::MT_ERROR
The message is an error.
Definition: MsgHandler.h:57
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:208
MsgHandler::wasInformed
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:270
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
MsgHandler::removeRetrieverFromAllInstances
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
Definition: MsgHandler.cpp:189
MsgHandler::MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:53
MsgHandler::MT_WARNING
The message is a warning.
Definition: MsgHandler.h:55
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
MsgHandler::isRetriever
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:183
MsgHandler::beginProcessMsg
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:135
MsgHandler::writeDebugMessages
static bool writeDebugMessages()
check whether to enable/disable debug messages
Definition: MsgHandler.h:95
MsgHandler::build
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:157
MsgHandler::myWasInformed
bool myWasInformed
information wehther an error occurred at all
Definition: MsgHandler.h:215
MsgHandler::cleanupOnEnd
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:241
MsgHandler::myErrorInstance
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:199
MsgHandler::myAmProcessingProcess
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:208
MsgHandler::clear
virtual void clear()
Clears information whether an error occurred previously.
Definition: MsgHandler.cpp:160
MsgHandler::enableDebugGLMessages
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:113
MsgHandler::Factory
MsgHandler *(* Factory)(MsgType)
Definition: MsgHandler.h:65
MsgHandler::setFactory
static void setFactory(Factory func)
Sets the factory function to use for new MsgHandlers.
Definition: MsgHandler.h:69
MsgHandler::myWarningInstance
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:202
MsgHandler
Definition: MsgHandler.h:44
MsgHandler::getWarningInstance
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:72
MsgHandler::endProcessMsg
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:148
MsgHandler::MsgHandler
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:255
MsgHandler::myWriteDebugGLMessages
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:232
MsgHandler::getGLDebugInstance
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:99
MsgHandler::myFactory
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition: MsgHandler.h:190
MsgHandler::operator<<
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:147
MsgHandler::operator=
MsgHandler & operator=(const MsgHandler &s)=delete
invalid assignment operator
MsgHandler::myMessageInstance
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:205
MsgHandler::myDebugInstance
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:193
MsgHandler::getDebugInstance
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:90
MsgHandler::MT_DEBUG
The message is an debug.
Definition: MsgHandler.h:59
MsgHandler::writeDebugGLMessages
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:100
MsgHandler::addRetriever
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:166
MsgHandler::myRetrievers
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:218
MsgHandler::MT_GLDEBUG
The message is an debug.
Definition: MsgHandler.h:61
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
MsgHandler::myType
MsgType myType
The type of the instance.
Definition: MsgHandler.h:212
MsgHandler::myGLDebugInstance
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:196
MsgHandler::myWriteDebugMessages
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:231
MsgHandler::~MsgHandler
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:265
MsgHandler::removeRetriever
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:174
MsgHandler::enableDebugMessages
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:108
MsgHandler::getMessageInstance
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:59