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-2020 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 <string>
24 #include <vector>
25 #include <map>
26 #include <iostream>
28 
29 
30 // ===========================================================================
31 // class definitions
32 // ===========================================================================
36 class MsgHandler {
37 public:
43  enum MsgType {
54  };
55 
56 private:
57  typedef MsgHandler* (*Factory)(MsgType);
58 
59 public:
61  static void setFactory(Factory func) {
62  // clean old instances
63  cleanupOnEnd();
64  myFactory = func;
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  virtual void inform(std::string msg, bool addType = true);
109 
111  // variadic function
112  template<typename T, typename... Targs>
113  void informf(const std::string& format, T value, Targs... Fargs) {
114  if (!aggregationThresholdReached(format)) {
115  std::ostringstream os;
116  os << std::fixed << std::setprecision(gPrecision);
117  _informf(format.c_str(), os, value, Fargs...);
118  inform(os.str(), true);
119  }
120  }
121 
129  virtual void beginProcessMsg(std::string msg, bool addType = true);
130 
132  virtual void endProcessMsg(std::string msg);
133 
135  virtual void clear(bool resetInformed = true);
136 
138  virtual void addRetriever(OutputDevice* retriever);
139 
141  virtual void removeRetriever(OutputDevice* retriever);
142 
144  bool isRetriever(OutputDevice* retriever) const;
145 
147  bool wasInformed() const;
148 
152  template <class T>
153  MsgHandler& operator<<(const T& t) {
154  // inform all other receivers
155  for (OutputDevice* o : myRetrievers) {
156  (*o) << t;
157  }
158  return *this;
159  }
160 
161 protected:
163  inline std::string build(const std::string& msg, bool addType) {
164  if (addType) {
165  switch (myType) {
166  case MT_MESSAGE:
167  break;
168  case MT_WARNING:
169  return "Warning: " + msg;
170  break;
171  case MT_ERROR:
172  return "Error: " + msg;
173  break;
174  case MT_DEBUG:
175  return "Debug: " + msg;
176  break;
177  case MT_GLDEBUG:
178  return "GLDebug: " + msg;
179  break;
180  default:
181  break;
182  }
183  }
184  return msg;
185  }
186 
187  virtual bool aggregationThresholdReached(const std::string& format) {
189  }
190 
191  void _informf(const char* format, std::ostringstream& os) {
192  os << format;
193  }
194 
196  // variadic function
197  template<typename T, typename... Targs>
198  void _informf(const char* format, std::ostringstream& os, T value, Targs... Fargs) {
199  for (; *format != '\0'; format++) {
200  if (*format == '%') {
201  os << value;
202  _informf(format + 1, os, Fargs...); // recursive call
203  return;
204  }
205  os << *format;
206  }
207  }
208 
209  void setAggregationThreshold(const int thresh) {
210  myAggregationThreshold = thresh;
211  }
212 
214  MsgHandler(MsgType type);
215 
217  virtual ~MsgHandler();
218 
219 private:
222 
225 
228 
231 
234 
237 
240 
241 private:
244 
247 
250 
252  std::map<const std::string, int> myAggregationCount;
253 
255  std::vector<OutputDevice*> myRetrievers;
256 
257 private:
259  MsgHandler(const MsgHandler& s) = delete;
260 
262  MsgHandler& operator=(const MsgHandler& s) = delete;
263 
268  static bool myWriteDebugMessages;
270 };
271 
272 
273 // ===========================================================================
274 // global definitions
275 // ===========================================================================
276 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
277 #define WRITE_WARNINGF(...) MsgHandler::getWarningInstance()->informf(__VA_ARGS__);
278 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
279 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
280 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
281 #define PROGRESS_BEGIN_TIME_MESSAGE(msg) SysUtils::getCurrentMillis(); MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
282 #define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(SysUtils::getCurrentMillis() - before) + "ms).");
283 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
284 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
285 #define WRITE_ERRORF(...) MsgHandler::getErrorInstance()->informf(__VA_ARGS__);
286 #define WRITE_DEBUG(msg) if(MsgHandler::writeDebugMessages()){MsgHandler::getDebugInstance()->inform(msg);};
287 #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:175
void _informf(const char *format, std::ostringstream &os)
Definition: MsgHandler.h:191
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:281
MsgType myType
The type of the instance.
Definition: MsgHandler.h:243
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:227
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:147
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:163
virtual bool aggregationThresholdReached(const std::string &format)
Definition: MsgHandler.h:187
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition: MsgHandler.h:221
bool myWasInformed
information whether an output occurred at all
Definition: MsgHandler.h:246
MsgHandler *(* Factory)(MsgType)
Definition: MsgHandler.h:57
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:217
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:230
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:236
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:94
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:192
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:61
std::map< const std::string, int > myAggregationCount
count for messages of the same type
Definition: MsgHandler.h:252
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:239
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:255
MsgHandler(const MsgHandler &s)=delete
invalid copy constructor
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:276
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:159
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:224
void setAggregationThreshold(const int thresh)
Definition: MsgHandler.h:209
static bool writeDebugMessages()
check whether to enable/disable debug messages
Definition: MsgHandler.h:89
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:233
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:134
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:268
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:269
void informf(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
Definition: MsgHandler.h:113
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:252
void _informf(const char *format, std::ostringstream &os, T value, Targs... Fargs)
adds a new formatted message
Definition: MsgHandler.h:198
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
Definition: MsgHandler.cpp:198
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:183
int myAggregationThreshold
do not output more messages of the same type if the count exceeds this threshold
Definition: MsgHandler.h:249
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:153
MsgHandler & operator=(const MsgHandler &s)=delete
invalid assignment operator
@ MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:45
@ MT_DEBUG
The message is an debug.
Definition: MsgHandler.h:51
@ MT_WARNING
The message is a warning.
Definition: MsgHandler.h:47
@ MT_GLDEBUG
The message is an debug.
Definition: MsgHandler.h:53
@ MT_ERROR
The message is an error.
Definition: MsgHandler.h:49
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:266
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:60