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#ifdef HAVE_INTL
28#include <libintl.h>
29#endif
32
33
34// ===========================================================================
35// class definitions
36// ===========================================================================
41public:
47 enum class MsgType {
49 MT_MESSAGE,
51 MT_WARNING,
53 MT_ERROR,
55 MT_DEBUG,
57 MT_GLDEBUG
58 };
59
60private:
61 typedef MsgHandler* (*Factory)(MsgType);
62
63public:
65 static void setFactory(Factory func) {
66 // clean old instances
68 myFactory = func;
69 }
70
73
76
79
82
85
87 static void enableDebugMessages(bool enable);
88
90 static void enableDebugGLMessages(bool enable);
91
93 static inline bool writeDebugMessages() {
95 }
96
98 static inline bool writeDebugGLMessages() {
100 }
101
104
106 static void setupI18n(const std::string& locale = "");
107
109 static void initOutputOptions();
110
112 static void cleanupOnEnd();
113
115 virtual void inform(std::string msg, bool addType = true);
116
118 // variadic function
119 template<typename T, typename... Targs>
120 void informf(const std::string& format, T value, Targs... Fargs) {
121 if (!aggregationThresholdReached(format)) {
122 inform(StringUtils::format(format, value, Fargs...), true);
123 }
124 }
125
133 virtual void beginProcessMsg(std::string msg, bool addType = true);
134
136 virtual void endProcessMsg(std::string msg);
137
139 virtual void clear(bool resetInformed = true);
140
142 virtual void addRetriever(OutputDevice* retriever);
143
145 virtual void removeRetriever(OutputDevice* retriever);
146
148 bool isRetriever(OutputDevice* retriever) const;
149
151 bool wasInformed() const;
152
156 template <class T>
157 MsgHandler& operator<<(const T& t) {
158 // inform all other receivers
159 for (OutputDevice* o : myRetrievers) {
160 (*o) << t;
161 }
162 return *this;
163 }
164
165protected:
167 inline std::string build(const std::string& msg, bool addType) {
168 if (addType) {
169 switch (myType) {
171 break;
173 return "Warning: " + msg;
174 break;
176 return "Error: " + msg;
177 break;
179 return "Debug: " + msg;
180 break;
182 return "GLDebug: " + msg;
183 break;
184 default:
185 break;
186 }
187 }
188 return msg;
189 }
190
191 virtual bool aggregationThresholdReached(const std::string& format) {
193 }
194
195 void setAggregationThreshold(const int thresh) {
196 myAggregationThreshold = thresh;
197 }
198
200 MsgHandler(MsgType type);
201
203 virtual ~MsgHandler();
204
205private:
208
211
214
217
220
223
226
227private:
230
233
236
238 std::map<const std::string, int> myAggregationCount;
239
241 std::vector<OutputDevice*> myRetrievers;
242
244 std::vector<std::string> myInitialMessages;
245
246private:
248 MsgHandler(const MsgHandler& s) = delete;
249
251 MsgHandler& operator=(const MsgHandler& s) = delete;
252
259};
260
261
262// ===========================================================================
263// global definitions
264// ===========================================================================
265#define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
266#define WRITE_WARNINGF(...) MsgHandler::getWarningInstance()->informf(__VA_ARGS__);
267#define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
268#define WRITE_MESSAGEF(...) MsgHandler::getMessageInstance()->informf(__VA_ARGS__);
269#define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
270#define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
271#define PROGRESS_BEGIN_TIME_MESSAGE(msg) SysUtils::getCurrentMillis(); MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
272#define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(SysUtils::getCurrentMillis() - before) + "ms).");
273#define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
274#define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
275#define WRITE_ERRORF(...) MsgHandler::getErrorInstance()->informf(__VA_ARGS__);
276#define WRITE_DEBUG(msg) if(MsgHandler::writeDebugMessages()){MsgHandler::getDebugInstance()->inform(msg);};
277#define WRITE_GLDEBUG(msg) if(MsgHandler::writeDebugGLMessages()){MsgHandler::getGLDebugInstance()->inform(msg);};
278#ifdef HAVE_INTL
279#define TL(string) gettext(string)
280#define TLF(string, ...) StringUtils::format(gettext(string), __VA_ARGS__)
281#else
282#define TL(string) (string)
283#define TLF(string, ...) StringUtils::format(string, __VA_ARGS__)
284#endif
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:185
std::vector< std::string > myInitialMessages
storage for initial messages
Definition: MsgHandler.h:244
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:97
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:321
MsgType myType
The type of the instance.
Definition: MsgHandler.h:229
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:79
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:116
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:111
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:213
MsgHandler & operator=(const MsgHandler &s)=delete
invalid assignment operator
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:149
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:167
virtual bool aggregationThresholdReached(const std::string &format)
Definition: MsgHandler.h:191
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition: MsgHandler.h:207
bool myWasInformed
information whether an output occurred at all
Definition: MsgHandler.h:232
static void setupI18n(const std::string &locale="")
set up gettext stuff
Definition: MsgHandler.cpp:228
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:255
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:216
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:88
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:222
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:98
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:202
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:66
static void setFactory(Factory func)
Sets the factory function to use for new MsgHandlers.
Definition: MsgHandler.h:65
std::map< const std::string, int > myAggregationCount
count for messages of the same type
Definition: MsgHandler.h:238
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:106
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:225
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:241
MsgHandler(const MsgHandler &s)=delete
invalid copy constructor
MsgHandler *(* Factory)(MsgType)
Definition: MsgHandler.h:61
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:316
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:161
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:210
void setAggregationThreshold(const int thresh)
Definition: MsgHandler.h:195
static bool writeDebugMessages()
check whether to enable/disable debug messages
Definition: MsgHandler.h:93
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:157
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:219
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:136
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:257
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:258
void informf(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
Definition: MsgHandler.h:120
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:292
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
Definition: MsgHandler.cpp:208
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:193
int myAggregationThreshold
do not output more messages of the same type if the count exceeds this threshold
Definition: MsgHandler.h:235
@ 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:306
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:53
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:170