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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Retrieves messages about the process and gives them further to output
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 #include <string>
25 #include <vector>
26 #include <map>
27 #include <iostream>
29 
30 
31 // ===========================================================================
32 // class definitions
33 // ===========================================================================
37 class MsgHandler {
38 public:
44  enum class MsgType {
46  MT_MESSAGE,
48  MT_WARNING,
50  MT_ERROR,
52  MT_DEBUG,
55  };
56 
57 private:
58  typedef MsgHandler* (*Factory)(MsgType);
59 
60 public:
62  static void setFactory(Factory func) {
63  // clean old instances
64  cleanupOnEnd();
65  myFactory = func;
66  }
67 
70 
73 
75  static MsgHandler* getErrorInstance();
76 
78  static MsgHandler* getDebugInstance();
79 
82 
84  static void enableDebugMessages(bool enable);
85 
87  static void enableDebugGLMessages(bool enable);
88 
90  static inline bool writeDebugMessages() {
91  return myWriteDebugMessages;
92  }
93 
95  static inline bool writeDebugGLMessages() {
97  }
98 
101 
103  static void initOutputOptions();
104 
106  static void cleanupOnEnd();
107 
109  virtual void inform(std::string msg, bool addType = true);
110 
112  // variadic function
113  template<typename T, typename... Targs>
114  void informf(const std::string& format, T value, Targs... Fargs) {
115  if (!aggregationThresholdReached(format)) {
116  std::ostringstream os;
117  os << std::fixed << std::setprecision(gPrecision);
118  _informf(format.c_str(), os, value, Fargs...);
119  inform(os.str(), true);
120  }
121  }
122 
130  virtual void beginProcessMsg(std::string msg, bool addType = true);
131 
133  virtual void endProcessMsg(std::string msg);
134 
136  virtual void clear(bool resetInformed = true);
137 
139  virtual void addRetriever(OutputDevice* retriever);
140 
142  virtual void removeRetriever(OutputDevice* retriever);
143 
145  bool isRetriever(OutputDevice* retriever) const;
146 
148  bool wasInformed() const;
149 
153  template <class T>
154  MsgHandler& operator<<(const T& t) {
155  // inform all other receivers
156  for (OutputDevice* o : myRetrievers) {
157  (*o) << t;
158  }
159  return *this;
160  }
161 
162 protected:
164  inline std::string build(const std::string& msg, bool addType) {
165  if (addType) {
166  switch (myType) {
167  case MsgType::MT_MESSAGE:
168  break;
169  case MsgType::MT_WARNING:
170  return "Warning: " + msg;
171  break;
172  case MsgType::MT_ERROR:
173  return "Error: " + msg;
174  break;
175  case MsgType::MT_DEBUG:
176  return "Debug: " + msg;
177  break;
178  case MsgType::MT_GLDEBUG:
179  return "GLDebug: " + msg;
180  break;
181  default:
182  break;
183  }
184  }
185  return msg;
186  }
187 
188  virtual bool aggregationThresholdReached(const std::string& format) {
190  }
191 
192  void _informf(const char* format, std::ostringstream& os) {
193  os << format;
194  }
195 
197  // variadic function
198  template<typename T, typename... Targs>
199  void _informf(const char* format, std::ostringstream& os, T value, Targs... Fargs) {
200  for (; *format != '\0'; format++) {
201  if (*format == '%') {
202  os << value;
203  _informf(format + 1, os, Fargs...); // recursive call
204  return;
205  }
206  os << *format;
207  }
208  }
209 
210  void setAggregationThreshold(const int thresh) {
211  myAggregationThreshold = thresh;
212  }
213 
215  MsgHandler(MsgType type);
216 
218  virtual ~MsgHandler();
219 
220 private:
223 
226 
229 
232 
235 
238 
241 
242 private:
245 
248 
251 
253  std::map<const std::string, int> myAggregationCount;
254 
256  std::vector<OutputDevice*> myRetrievers;
257 
259  std::vector<std::string> myInitialMessages;
260 
261 private:
263  MsgHandler(const MsgHandler& s) = delete;
264 
266  MsgHandler& operator=(const MsgHandler& s) = delete;
267 
272  static bool myWriteDebugMessages;
274 };
275 
276 
277 // ===========================================================================
278 // global definitions
279 // ===========================================================================
280 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
281 #define WRITE_WARNINGF(...) MsgHandler::getWarningInstance()->informf(__VA_ARGS__);
282 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
283 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
284 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
285 #define PROGRESS_BEGIN_TIME_MESSAGE(msg) SysUtils::getCurrentMillis(); MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
286 #define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(SysUtils::getCurrentMillis() - before) + "ms).");
287 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
288 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
289 #define WRITE_ERRORF(...) MsgHandler::getErrorInstance()->informf(__VA_ARGS__);
290 #define WRITE_DEBUG(msg) if(MsgHandler::writeDebugMessages()){MsgHandler::getDebugInstance()->inform(msg);};
291 #define WRITE_GLDEBUG(msg) if(MsgHandler::writeDebugGLMessages()){MsgHandler::getGLDebugInstance()->inform(msg);};
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:25
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:186
std::vector< std::string > myInitialMessages
storage for initial messages
Definition: MsgHandler.h:259
void _informf(const char *format, std::ostringstream &os)
Definition: MsgHandler.h:192
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:98
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:294
MsgType myType
The type of the instance.
Definition: MsgHandler.h:244
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:117
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:112
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:228
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:150
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:164
virtual bool aggregationThresholdReached(const std::string &format)
Definition: MsgHandler.h:188
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition: MsgHandler.h:222
bool myWasInformed
information whether an output occurred at all
Definition: MsgHandler.h:247
MsgHandler *(* Factory)(MsgType)
Definition: MsgHandler.h:58
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:228
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:231
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:89
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:237
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:95
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:203
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:67
static void setFactory(Factory func)
Sets the factory function to use for new MsgHandlers.
Definition: MsgHandler.h:62
std::map< const std::string, int > myAggregationCount
count for messages of the same type
Definition: MsgHandler.h:253
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:107
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:240
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:256
MsgHandler(const MsgHandler &s)=delete
invalid copy constructor
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:289
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:162
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:225
void setAggregationThreshold(const int thresh)
Definition: MsgHandler.h:210
static bool writeDebugMessages()
check whether to enable/disable debug messages
Definition: MsgHandler.h:90
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:234
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:137
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:272
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:273
void informf(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
Definition: MsgHandler.h:114
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:265
void _informf(const char *format, std::ostringstream &os, T value, Targs... Fargs)
adds a new formatted message
Definition: MsgHandler.h:199
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
Definition: MsgHandler.cpp:209
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:194
int myAggregationThreshold
do not output more messages of the same type if the count exceeds this threshold
Definition: MsgHandler.h:250
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:154
MsgHandler & operator=(const MsgHandler &s)=delete
invalid assignment operator
@ MT_GLDEBUG
The message is GL debug output.
@ MT_DEBUG
The message is debug output.
@ MT_MESSAGE
The message is only something to show.
@ MT_ERROR
The message is an error.
@ MT_WARNING
The message is a warning.
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:279
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:54
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61