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.dev/sumo
3 // Copyright (C) 2003-2024 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 /****************************************************************************/
21 // Retrieves messages about the process and gives them further to output
22 /****************************************************************************/
23 #pragma once
24 #include <config.h>
25 #include <string>
26 #include <vector>
27 #include <map>
31 
32 
33 // ===========================================================================
34 // class definitions
35 // ===========================================================================
39 class MsgHandler {
40 public:
46  enum class MsgType {
48  MT_MESSAGE,
50  MT_WARNING,
52  MT_ERROR,
54  MT_DEBUG,
57  };
58 
59 private:
60  typedef MsgHandler* (*Factory)(MsgType);
61 
62 public:
64  static void setFactory(Factory func) {
65  // clean old instances
66  cleanupOnEnd();
67  myFactory = func;
68  }
69 
72 
75 
77  static MsgHandler* getErrorInstance();
78 
80  static MsgHandler* getDebugInstance();
81 
84 
86  static void enableDebugMessages(bool enable);
87 
89  static void enableDebugGLMessages(bool enable);
90 
92  static inline bool writeDebugMessages() {
93  return myWriteDebugMessages;
94  }
95 
97  static inline bool writeDebugGLMessages() {
99  }
100 
102  static std::string insertLineBreaks(std::string msg, int lineWidth);
103 
106 
108  static void setupI18n(const std::string& locale = "");
109 
111  static void initOutputOptions();
112 
114  static void cleanupOnEnd();
115 
117  virtual void inform(std::string msg, bool addType = true);
118 
120  // variadic function
121  template<typename T, typename... Targs>
122  void informf(const std::string& format, T value, Targs... Fargs) {
123  if (!aggregationThresholdReached(format)) {
124  inform(StringUtils::format(format, value, Fargs...), true);
125  }
126  }
127 
135  virtual void beginProcessMsg(std::string msg, bool addType = true);
136 
138  virtual void endProcessMsg2(bool success, long duration = -1);
139 
141  virtual void endProcessMsg(std::string msg);
142 
144  virtual void clear(bool resetInformed = true);
145 
147  virtual void addRetriever(OutputDevice* retriever);
148 
150  virtual void removeRetriever(OutputDevice* retriever);
151 
153  bool isRetriever(OutputDevice* retriever) const;
154 
156  bool wasInformed() const;
157 
161  template <class T>
162  MsgHandler& operator<<(const T& t) {
163  // inform all other receivers
164  for (OutputDevice* o : myRetrievers) {
165  (*o) << t;
166  }
167  return *this;
168  }
169 
170 protected:
171 
172  std::string buildTimestampPrefix(void) const;
173  std::string buildProcessIdPrefix(void) const;
174 
176  inline std::string build(const std::string& msg, bool addType) {
177  std::string prefix;
178  if (myWriteTimestamps) {
179  prefix += buildTimestampPrefix();
180  }
181  if (myWriteProcessId) {
182  prefix += buildProcessIdPrefix();
183  }
184  if (addType) {
185  switch (myType) {
186  case MsgType::MT_MESSAGE:
187  break;
188  case MsgType::MT_WARNING:
189  prefix += myWarningPrefix;
190  break;
191  case MsgType::MT_ERROR:
192  prefix += myErrorPrefix;
193  break;
194  case MsgType::MT_DEBUG:
195  prefix += "Debug: ";
196  break;
197  case MsgType::MT_GLDEBUG:
198  prefix += "GLDebug: ";
199  break;
200  default:
201  break;
202  }
203  }
204  return prefix + msg;
205  }
206 
207  virtual bool aggregationThresholdReached(const std::string& format) {
209  }
210 
211  void setAggregationThreshold(const int thresh) {
212  myAggregationThreshold = thresh;
213  }
214 
216  MsgHandler(MsgType type);
217 
219  virtual ~MsgHandler();
220 
221 private:
224 
227 
230 
233 
236 
239 
242 
243 private:
246 
249 
252 
254  std::map<const std::string, int> myAggregationCount;
255 
257  std::vector<OutputDevice*> myRetrievers;
258 
260  std::vector<std::string> myInitialMessages;
261 
266  static bool myWriteDebugMessages;
267 
270 
272  static bool myWriteTimestamps;
273 
275  static bool myWriteProcessId;
276 
278  static std::string myErrorPrefix;
279 
281  static std::string myWarningPrefix;
282 
283 private:
285  MsgHandler(const MsgHandler& s) = delete;
286 
288  MsgHandler& operator=(const MsgHandler& s) = delete;
289 };
290 
291 
292 // ===========================================================================
293 // global definitions
294 // ===========================================================================
295 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
296 #define WRITE_WARNINGF(...) MsgHandler::getWarningInstance()->informf(__VA_ARGS__);
297 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
298 #define WRITE_MESSAGEF(...) MsgHandler::getMessageInstance()->informf(__VA_ARGS__);
299 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
300 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg2(true);
301 #define PROGRESS_BEGIN_TIME_MESSAGE(msg) SysUtils::getCurrentMillis(); MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
302 #define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg2(true, SysUtils::getCurrentMillis() - before);
303 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg2(false);
304 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
305 #define WRITE_ERRORF(...) MsgHandler::getErrorInstance()->informf(__VA_ARGS__);
306 #define WRITE_DEBUG(msg) if(MsgHandler::writeDebugMessages()){MsgHandler::getDebugInstance()->inform(msg);};
307 #define WRITE_GLDEBUG(msg) if(MsgHandler::writeDebugGLMessages()){MsgHandler::getGLDebugInstance()->inform(msg);};
308 #ifdef HAVE_INTL
309 // basic translation
310 #define TL(string) gettext(string)
311 // complex translation ("This % an %", "is", "example")
312 #define TLF(string, ...) StringUtils::format(gettext(string), __VA_ARGS__)
313 #else
314 // basic translation
315 #define TL(string) (string)
316 // complex translation ("This % an %", "is", "example")
317 #define TLF(string, ...) StringUtils::format(string, __VA_ARGS__)
318 #endif
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:237
std::vector< std::string > myInitialMessages
storage for initial messages
Definition: MsgHandler.h:260
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:110
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:416
MsgType myType
The type of the instance.
Definition: MsgHandler.h:245
std::string buildProcessIdPrefix(void) const
Definition: MsgHandler.cpp:388
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:92
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:154
std::string buildTimestampPrefix(void) const
Definition: MsgHandler.cpp:374
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:124
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:229
static std::string insertLineBreaks(std::string msg, int lineWidth)
reformats a long string to contain newline after a certain line length in px (depending on the curren...
Definition: MsgHandler.cpp:130
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:201
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:176
virtual bool aggregationThresholdReached(const std::string &format)
Definition: MsgHandler.h:207
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition: MsgHandler.h:223
bool myWasInformed
information whether an output occurred at all
Definition: MsgHandler.h:248
static bool myWriteTimestamps
Whether to prefix every message with a time stamp.
Definition: MsgHandler.h:272
MsgHandler *(* Factory)(MsgType)
Definition: MsgHandler.h:60
static void setupI18n(const std::string &locale="")
set up gettext stuff
Definition: MsgHandler.cpp:280
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:316
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:232
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:101
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:238
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:97
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:254
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:79
static void setFactory(Factory func)
Sets the factory function to use for new MsgHandlers.
Definition: MsgHandler.h:64
std::map< const std::string, int > myAggregationCount
count for messages of the same type
Definition: MsgHandler.h:254
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:119
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:241
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:257
MsgHandler(const MsgHandler &s)=delete
invalid copy constructor
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:411
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:213
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:226
void setAggregationThreshold(const int thresh)
Definition: MsgHandler.h:211
static bool writeDebugMessages()
check whether to enable/disable debug messages
Definition: MsgHandler.h:92
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:235
virtual void endProcessMsg2(bool success, long duration=-1)
Ends a process information with predefined messages.
Definition: MsgHandler.cpp:187
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:174
static bool myWriteDebugMessages
Flag to enable or disable debug output.
Definition: MsgHandler.h:266
static bool myWriteDebugGLMessages
Flag to enable or disable GL specific debug output.
Definition: MsgHandler.h:269
static std::string myWarningPrefix
The possibly translated warning prefix (mainly for speedup)
Definition: MsgHandler.h:281
void informf(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
Definition: MsgHandler.h:122
static bool myWriteProcessId
Whether to prefix every message with the process id.
Definition: MsgHandler.h:275
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:359
static std::string myErrorPrefix
The possibly translated error prefix (mainly for speedup)
Definition: MsgHandler.h:278
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
Definition: MsgHandler.cpp:260
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:245
int myAggregationThreshold
do not output more messages of the same type if the count exceeds this threshold
Definition: MsgHandler.h:251
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:162
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:401
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:66
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
static const std::string format(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
Definition: StringUtils.h:176