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 /****************************************************************************/
16 // Retrieves messages about the process and gives them further to output
17 /****************************************************************************/
18 #ifndef MsgHandler_h
19 #define MsgHandler_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <string>
26 #include <vector>
27 #include <map>
28 #include <iostream>
30 
31 
32 // ===========================================================================
33 // class definitions
34 // ===========================================================================
38 class MsgHandler {
39 public:
45  enum MsgType {
56  };
57 
58 private:
59  typedef MsgHandler* (*Factory)(MsgType);
60 
61 public:
63  static void setFactory(Factory func) {
64  // clean old instances
65  cleanupOnEnd();
66  myFactory = func;
67  }
68 
71 
74 
76  static MsgHandler* getErrorInstance();
77 
79  static MsgHandler* getDebugInstance();
80 
83 
85  static void enableDebugMessages(bool enable);
86 
88  static void enableDebugGLMessages(bool enable);
89 
91  static inline bool writeDebugMessages() {
92  return myWriteDebugMessages;
93  }
94 
96  static inline bool writeDebugGLMessages() {
98  }
99 
102 
104  static void initOutputOptions();
105 
107  static void cleanupOnEnd();
108 
110  virtual void inform(std::string msg, bool addType = true);
111 
113  // variadic function
114  template<typename T, typename... Targs>
115  void informf(const std::string& format, T value, Targs... Fargs) {
116  if (myAggregationThreshold >= 0) {
117  if (myAggregationCount[format]++ >= myAggregationThreshold) {
118  return;
119  }
120  }
121  (*this) << build("", true);
122  _informf(format.c_str(), value, Fargs...);
123  }
124 
132  virtual void beginProcessMsg(std::string msg, bool addType = true);
133 
135  virtual void endProcessMsg(std::string msg);
136 
138  virtual void clear();
139 
141  virtual void addRetriever(OutputDevice* retriever);
142 
144  virtual void removeRetriever(OutputDevice* retriever);
145 
147  bool isRetriever(OutputDevice* retriever) const;
148 
150  bool wasInformed() const;
151 
155  template <class T>
156  MsgHandler& operator<<(const T& t) {
157  // inform all other receivers
158  for (OutputDevice* o : myRetrievers) {
159  (*o) << t;
160  }
161  return *this;
162  }
163 
164 protected:
166  inline std::string build(const std::string& msg, bool addType) {
167  if (addType) {
168  switch (myType) {
169  case MT_MESSAGE:
170  break;
171  case MT_WARNING:
172  return "Warning: " + msg;
173  break;
174  case MT_ERROR:
175  return "Error: " + msg;
176  break;
177  case MT_DEBUG:
178  return "Debug: " + msg;
179  break;
180  case MT_GLDEBUG:
181  return "GLDebug: " + msg;
182  break;
183  default:
184  break;
185  }
186  }
187  return msg;
188  }
189 
190  void _informf(const char* format) {
191  inform(format, false);
192  }
193 
195  // variadic function
196  template<typename T, typename... Targs>
197  void _informf(const char* format, T value, Targs... Fargs) {
198  for (; *format != '\0'; format++) {
199  if (*format == '%') {
200  (*this) << value;
201  _informf(format + 1, Fargs...); // recursive call
202  return;
203  }
204  (*this) << *format;
205  }
206  }
207 
208  void setAggregationThreshold(const int thresh) {
209  myAggregationThreshold = thresh;
210  }
211 
213  MsgHandler(MsgType type);
214 
216  virtual ~MsgHandler();
217 
218 private:
221 
224 
227 
230 
233 
236 
239 
240 private:
243 
246 
249 
251  std::map<const std::string, int> myAggregationCount;
252 
254  std::vector<OutputDevice*> myRetrievers;
255 
256 private:
258  MsgHandler(const MsgHandler& s) = delete;
259 
261  MsgHandler& operator=(const MsgHandler& s) = delete;
262 
267  static bool myWriteDebugMessages;
269 };
270 
271 
272 // ===========================================================================
273 // global definitions
274 // ===========================================================================
275 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
276 #define WRITE_WARNINGF(...) MsgHandler::getWarningInstance()->informf(__VA_ARGS__);
277 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
278 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
279 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
280 #define PROGRESS_BEGIN_TIME_MESSAGE(msg) SysUtils::getCurrentMillis(); MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
281 #define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(SysUtils::getCurrentMillis() - before) + "ms).");
282 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
283 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
284 #define WRITE_DEBUG(msg) if(MsgHandler::writeDebugMessages()){MsgHandler::getDebugInstance()->inform(msg);};
285 #define WRITE_GLDEBUG(msg) if(MsgHandler::writeDebugGLMessages()){MsgHandler::getGLDebugInstance()->inform(msg);};
286 
287 #endif
288 
289 /****************************************************************************/
MsgHandler::MsgType
MsgType
Definition: MsgHandler.h:45
MsgHandler::MT_ERROR
The message is an error.
Definition: MsgHandler.h:51
MsgHandler::setAggregationThreshold
void setAggregationThreshold(const int thresh)
Definition: MsgHandler.h:208
MsgHandler::_informf
void _informf(const char *format, T value, Targs... Fargs)
adds a new formatted message
Definition: MsgHandler.h:197
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:216
MsgHandler::wasInformed
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:279
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
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:197
MsgHandler::MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:47
MsgHandler::_informf
void _informf(const char *format)
Definition: MsgHandler.h:190
MsgHandler::MT_WARNING
The message is a warning.
Definition: MsgHandler.h:49
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:191
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:91
MsgHandler::build
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:166
MsgHandler::myWasInformed
bool myWasInformed
information whether an output occurred at all
Definition: MsgHandler.h:245
MsgHandler::myAggregationCount
std::map< const std::string, int > myAggregationCount
count for messages of the same type
Definition: MsgHandler.h:251
MsgHandler::cleanupOnEnd
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:250
MsgHandler::myErrorInstance
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:229
MsgHandler::myAmProcessingProcess
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:238
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:59
MsgHandler::setFactory
static void setFactory(Factory func)
Sets the factory function to use for new MsgHandlers.
Definition: MsgHandler.h:63
MsgHandler::myWarningInstance
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:232
MsgHandler
Definition: MsgHandler.h:38
MsgHandler::getWarningInstance
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:68
MsgHandler::endProcessMsg
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:148
OutputDevice.h
MsgHandler::MsgHandler
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:264
MsgHandler::myWriteDebugGLMessages
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:268
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:220
MsgHandler::operator<<
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:156
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:235
MsgHandler::myDebugInstance
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:223
MsgHandler::myAggregationThreshold
int myAggregationThreshold
do not output more messages of the same type if the count exceeds this threshold
Definition: MsgHandler.h:248
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:53
MsgHandler::writeDebugGLMessages
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:96
MsgHandler::addRetriever
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:174
MsgHandler::informf
void informf(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
Definition: MsgHandler.h:115
MsgHandler::myRetrievers
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:254
MsgHandler::MT_GLDEBUG
The message is an debug.
Definition: MsgHandler.h:55
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:242
MsgHandler::myGLDebugInstance
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:226
MsgHandler::myWriteDebugMessages
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:267
MsgHandler::~MsgHandler
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:274
MsgHandler::removeRetriever
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:182
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:55