Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2026 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 setWriteTimestamps(bool enable) {
106 myWriteTimestamps = enable;
107 }
108
110 static void setWriteProcessId(bool enable) {
111 myWriteProcessId = enable;
112 }
113
115 void addInitialMessage(const std::string& msg) {
116 myInitialMessages.push_back(msg);
117 }
118
120 static void cleanupOnEnd();
121
123 virtual void inform(std::string msg, bool addType = true);
124
126 // variadic function
127 template<typename T, typename... Targs>
128 void informf(const std::string& format, T value, Targs... Fargs) {
129 if (!aggregationThresholdReached(format)) {
130 inform(StringUtils::format(format, value, Fargs...), true);
131 }
132 }
133
141 virtual void beginProcessMsg(std::string msg, bool addType = true);
142
144 virtual void endProcessMsg2(bool success, long duration = -1);
145
147 virtual void endProcessMsg(std::string msg);
148
150 virtual void clear(bool resetInformed = true);
151
153 virtual void addRetriever(OutputDevice* retriever);
154
156 virtual void removeRetriever(OutputDevice* retriever);
157
159 bool isRetriever(OutputDevice* retriever) const;
160
162 bool wasInformed() const;
163
167 template <class T>
168 MsgHandler& operator<<(const T& t) {
169 // inform all other receivers
170 for (OutputDevice* o : myRetrievers) {
171 (*o) << t;
172 }
173 return *this;
174 }
175
176 void setAggregationThreshold(const int thresh) {
177 myAggregationThreshold = thresh;
178 }
179
182 }
183
184protected:
185
186 std::string buildProcessIdPrefix() const;
187
189 inline std::string build(const std::string& msg, bool addType) {
190 std::string prefix;
191 if (myWriteTimestamps) {
192 prefix += "[" + StringUtils::isoTimeString() + "] ";
193 }
194 if (myWriteProcessId) {
195 prefix += buildProcessIdPrefix();
196 }
197 if (addType) {
198 switch (myType) {
200 break;
202 prefix += myWarningPrefix;
203 break;
205 prefix += myErrorPrefix;
206 break;
208 prefix += "Debug: ";
209 break;
211 prefix += "GLDebug: ";
212 break;
213 default:
214 break;
215 }
216 }
217 return prefix + msg;
218 }
219
220 virtual bool aggregationThresholdReached(const std::string& format) {
222 }
223
225 MsgHandler(MsgType type);
226
228 virtual ~MsgHandler();
229
230private:
233
236
239
242
245
246private:
249
252
255
257 std::map<const std::string, int> myAggregationCount;
258
260 std::vector<OutputDevice*> myRetrievers;
261
263 std::vector<std::string> myInitialMessages;
264
270
273
275 static bool myWriteTimestamps;
276
278 static bool myWriteProcessId;
279
281 static std::string myErrorPrefix;
282
284 static std::string myWarningPrefix;
285
286private:
288 MsgHandler(const MsgHandler& s) = delete;
289
291 MsgHandler& operator=(const MsgHandler& s) = delete;
292};
293
294
295// ===========================================================================
296// global definitions
297// ===========================================================================
298#define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
299#define WRITE_WARNINGF(...) MsgHandler::getWarningInstance()->informf(__VA_ARGS__);
300#define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
301#define WRITE_MESSAGEF(...) MsgHandler::getMessageInstance()->informf(__VA_ARGS__);
302#define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
303#define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg2(true);
304#define PROGRESS_BEGIN_TIME_MESSAGE(msg) SysUtils::getCurrentMillis(); MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string(" ..."));
305#define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg2(true, SysUtils::getCurrentMillis() - before);
306#define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg2(false);
307#define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
308#define WRITE_ERRORF(...) MsgHandler::getErrorInstance()->informf(__VA_ARGS__);
309#ifdef HAVE_INTL
310// basic translation
311#define TL(string) gettext(string)
312// complex translation ("This % an %", "is", "example")
313#define TLF(string, ...) StringUtils::format(gettext(string), __VA_ARGS__)
314#else
315// basic translation
316#define TL(string) (string)
317// complex translation ("This % an %", "is", "example")
318#define TLF(string, ...) StringUtils::format(string, __VA_ARGS__)
319#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:263
bool wasInformed() const
Returns the information whether any messages were added.
MsgType myType
The type of the instance.
Definition MsgHandler.h:248
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
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 buildProcessIdPrefix() const
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition MsgHandler.h:189
virtual bool aggregationThresholdReached(const std::string &format)
Definition MsgHandler.h:220
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition MsgHandler.h:232
bool myWasInformed
information whether an output occurred at all
Definition MsgHandler.h:251
static bool myWriteTimestamps
Whether to prefix every message with a time stamp.
Definition MsgHandler.h:275
static void setupI18n(const std::string &locale="")
set up gettext stuff
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition MsgHandler.h:235
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition MsgHandler.h:241
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 void setWriteTimestamps(bool enable)
enable/disable timestamps in log output
Definition MsgHandler.h:105
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:257
static void enableDebugMessages(bool enable)
enable/disable debug messages
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition MsgHandler.h:244
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition MsgHandler.h:260
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:176
static bool writeDebugMessages()
check whether to enable/disable debug messages
Definition MsgHandler.h:86
void addInitialMessage(const std::string &msg)
add a message which is repeated at the end if errors occurred
Definition MsgHandler.h:115
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition MsgHandler.h:168
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition MsgHandler.h:238
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:269
static void setWriteProcessId(bool enable)
enable/disable process id in log output
Definition MsgHandler.h:110
static bool myWriteDebugGLMessages
Flag to enable or disable GL specific debug output.
Definition MsgHandler.h:272
static std::string myWarningPrefix
The possibly translated warning prefix (mainly for speedup)
Definition MsgHandler.h:284
void informf(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
Definition MsgHandler.h:128
static bool myWriteProcessId
Whether to prefix every message with the process id.
Definition MsgHandler.h:278
static void cleanupOnEnd()
Removes pending handler.
static std::string myErrorPrefix
The possibly translated error prefix (mainly for speedup)
Definition MsgHandler.h:281
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:254
int getAggregationThreshold() const
Definition MsgHandler.h:180
@ 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
static std::string isoTimeString(const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Returns an ISO8601 formatted time string with microsecond precision.