Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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-2025 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// ===========================================================================
40public:
46 enum class MsgType {
57 };
58
59private:
60 typedef MsgHandler* (*Factory)(MsgType);
61
62public:
64 static void setFactory(Factory func) {
65 // clean old instances
67 myFactory = func;
68 }
69
72
75
78
80 static void enableDebugMessages(bool enable);
81
83 static void enableDebugGLMessages(bool enable);
84
86 static inline bool writeDebugMessages() {
88 }
89
91 static inline bool writeDebugGLMessages() {
93 }
94
96 static std::string insertLineBreaks(std::string msg, int lineWidth);
97
100
102 static void setupI18n(const std::string& locale = "");
103
105 static void initOutputOptions();
106
108 static void cleanupOnEnd();
109
111 virtual void inform(std::string msg, bool addType = true);
112
114 // variadic function
115 template<typename T, typename... Targs>
116 void informf(const std::string& format, T value, Targs... Fargs) {
117 if (!aggregationThresholdReached(format)) {
118 inform(StringUtils::format(format, value, Fargs...), true);
119 }
120 }
121
129 virtual void beginProcessMsg(std::string msg, bool addType = true);
130
132 virtual void endProcessMsg2(bool success, long duration = -1);
133
135 virtual void endProcessMsg(std::string msg);
136
138 virtual void clear(bool resetInformed = true);
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 void setAggregationThreshold(const int thresh) {
165 myAggregationThreshold = thresh;
166 }
167
170 }
171
172protected:
173
174 std::string buildTimestampPrefix(void) const;
175 std::string buildProcessIdPrefix(void) const;
176
178 inline std::string build(const std::string& msg, bool addType) {
179 std::string prefix;
180 if (myWriteTimestamps) {
181 prefix += buildTimestampPrefix();
182 }
183 if (myWriteProcessId) {
184 prefix += buildProcessIdPrefix();
185 }
186 if (addType) {
187 switch (myType) {
189 break;
191 prefix += myWarningPrefix;
192 break;
194 prefix += myErrorPrefix;
195 break;
197 prefix += "Debug: ";
198 break;
200 prefix += "GLDebug: ";
201 break;
202 default:
203 break;
204 }
205 }
206 return prefix + msg;
207 }
208
209 virtual bool aggregationThresholdReached(const std::string& format) {
211 }
212
214 MsgHandler(MsgType type);
215
217 virtual ~MsgHandler();
218
219private:
222
225
228
231
234
235private:
238
241
244
246 std::map<const std::string, int> myAggregationCount;
247
249 std::vector<OutputDevice*> myRetrievers;
250
252 std::vector<std::string> myInitialMessages;
253
259
262
264 static bool myWriteTimestamps;
265
267 static bool myWriteProcessId;
268
270 static std::string myErrorPrefix;
271
273 static std::string myWarningPrefix;
274
275private:
277 MsgHandler(const MsgHandler& s) = delete;
278
280 MsgHandler& operator=(const MsgHandler& s) = delete;
281};
282
283
284// ===========================================================================
285// global definitions
286// ===========================================================================
287#define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
288#define WRITE_WARNINGF(...) MsgHandler::getWarningInstance()->informf(__VA_ARGS__);
289#define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
290#define WRITE_MESSAGEF(...) MsgHandler::getMessageInstance()->informf(__VA_ARGS__);
291#define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
292#define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg2(true);
293#define PROGRESS_BEGIN_TIME_MESSAGE(msg) SysUtils::getCurrentMillis(); MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
294#define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg2(true, SysUtils::getCurrentMillis() - before);
295#define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg2(false);
296#define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
297#define WRITE_ERRORF(...) MsgHandler::getErrorInstance()->informf(__VA_ARGS__);
298#ifdef HAVE_INTL
299// basic translation
300#define TL(string) gettext(string)
301// complex translation ("This % an %", "is", "example")
302#define TLF(string, ...) StringUtils::format(gettext(string), __VA_ARGS__)
303#else
304// basic translation
305#define TL(string) (string)
306// complex translation ("This % an %", "is", "example")
307#define TLF(string, ...) StringUtils::format(string, __VA_ARGS__)
308#endif
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
std::vector< std::string > myInitialMessages
storage for initial messages
Definition MsgHandler.h:252
bool wasInformed() const
Returns the information whether any messages were added.
MsgType myType
The type of the instance.
Definition MsgHandler.h:237
std::string buildProcessIdPrefix(void) const
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
std::string buildTimestampPrefix(void) const
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
MsgHandler & operator=(const MsgHandler &s)=delete
invalid assignment operator
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...
virtual void endProcessMsg(std::string msg)
Ends a process information.
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition MsgHandler.h:178
virtual bool aggregationThresholdReached(const std::string &format)
Definition MsgHandler.h:209
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:240
static bool myWriteTimestamps
Whether to prefix every message with a time stamp.
Definition MsgHandler.h:264
static void setupI18n(const std::string &locale="")
set up gettext stuff
static void initOutputOptions()
init output options
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition MsgHandler.h:224
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition MsgHandler.h:230
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition MsgHandler.h:91
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
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:246
static void enableDebugMessages(bool enable)
enable/disable debug messages
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition MsgHandler.h:233
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition MsgHandler.h:249
MsgHandler(const MsgHandler &s)=delete
invalid copy constructor
MsgHandler *(* Factory)(MsgType)
Definition MsgHandler.h:60
virtual ~MsgHandler()
destructor
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
void setAggregationThreshold(const int thresh)
Definition MsgHandler.h:164
static bool writeDebugMessages()
check whether to enable/disable debug messages
Definition MsgHandler.h:86
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition MsgHandler.h:156
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition MsgHandler.h:227
virtual void endProcessMsg2(bool success, long duration=-1)
Ends a process information with predefined messages.
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
static bool myWriteDebugMessages
Flag to enable or disable debug output.
Definition MsgHandler.h:258
static bool myWriteDebugGLMessages
Flag to enable or disable GL specific debug output.
Definition MsgHandler.h:261
static std::string myWarningPrefix
The possibly translated warning prefix (mainly for speedup)
Definition MsgHandler.h:273
void informf(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
Definition MsgHandler.h:116
static bool myWriteProcessId
Whether to prefix every message with the process id.
Definition MsgHandler.h:267
static void cleanupOnEnd()
Removes pending handler.
static std::string myErrorPrefix
The possibly translated error prefix (mainly for speedup)
Definition MsgHandler.h:270
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
int myAggregationThreshold
do not output more messages of the same type if the count exceeds this threshold
Definition MsgHandler.h:243
int getAggregationThreshold() const
Definition MsgHandler.h:168
@ 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.
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Static storage of an output device and its base (abstract) implementation.
static const std::string format(const std::string &format, T value, Targs... Fargs)
adds a new formatted message