Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MsgHandler.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-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/****************************************************************************/
20// Retrieves messages about the process and gives them further to output
21/****************************************************************************/
22#include <config.h>
23
24#include <string>
25#include <cassert>
26#include <vector>
27#include <algorithm>
28#include <iostream>
29#ifdef WIN32
30#include <windows.h>
31#else
32#include <unistd.h>
33#endif
36#include "MsgHandler.h"
37
38
39// ===========================================================================
40// static member variables
41// ===========================================================================
51std::string MsgHandler::myErrorPrefix = "Error: ";
52std::string MsgHandler::myWarningPrefix = "Warning: ";
53
54
55// ===========================================================================
56// method definitions
57// ===========================================================================
58
70
71
83
84
92
93
94void
98
99void
103
104
105std::string
106MsgHandler::insertLineBreaks(std::string msg, int lineWidth) {
107 // TODO: check what FXFont::getTextWidth can do
108 //int textWidth = getApp()->getNormalFont()->getTextWidth
109 if ((int)msg.size() <= lineWidth) {
110 return msg;
111 }
112 size_t pos = 0;
113 size_t nextLineBreak = msg.find('\n');
114 size_t spaceAfterLine = msg.find(' ', lineWidth);
115 while (spaceAfterLine != std::string::npos) {
116 if (nextLineBreak == std::string::npos || nextLineBreak > spaceAfterLine) {
117 msg = msg.replace(spaceAfterLine, 1, "\n");
118 pos = spaceAfterLine + 1;
119 } else {
120 pos = nextLineBreak + 1;
121 }
122 spaceAfterLine = msg.find(' ', pos + lineWidth);
123 nextLineBreak = msg.find('\n', pos);
124 }
125 return msg;
126}
127
128
129void
130MsgHandler::inform(std::string msg, bool addType) {
131 if (addType && !myInitialMessages.empty() && myInitialMessages.size() < 5) {
132 myInitialMessages.push_back(msg);
133 }
134 // beautify progress output
136 myAmProcessingProcess = false;
138 }
139 msg = build(msg, addType);
140 // inform all receivers
141 for (auto i : myRetrievers) {
142 i->inform(msg);
143 }
144 // set the information that something occurred
145 myWasInformed = true;
146}
147
148
149void
150MsgHandler::beginProcessMsg(std::string msg, bool addType) {
151 msg = build(msg, addType);
152 // inform all other receivers
153 for (auto i : myRetrievers) {
154 i->inform(msg, true);
156 }
157 // set the information that something occurred
158 myWasInformed = true;
159}
160
161
162void
163MsgHandler::endProcessMsg2(bool success, long duration) {
164 if (success) {
165 if (duration > -1) {
166 endProcessMsg(TLF(" done (%ms).", toString(duration)));
167 } else {
168 endProcessMsg(TL(" done."));
169 }
170 } else {
171 endProcessMsg(TL(" failed."));
172 }
173}
174
175
176void
178 // inform all other receivers
179 for (auto i : myRetrievers) {
180 i->inform(msg);
181 }
182 // set the information that something occurred
183 myWasInformed = true;
184 myAmProcessingProcess = false;
185}
186
187
188void
189MsgHandler::clear(bool resetInformed) {
190 if (myAggregationThreshold >= 0) {
191 for (const auto& i : myAggregationCount) {
192 if (i.second > myAggregationThreshold) {
193 inform(toString(i.second) + " total messages of type: " + i.first);
194 }
195 }
196 }
197 myAggregationCount.clear();
198 if (!resetInformed && myInitialMessages.size() > 1) {
199 const bool wasInformed = myWasInformed;
200 for (const std::string& msg : myInitialMessages) {
201 inform(msg, false);
202 }
203 myInitialMessages.clear();
205 }
206 if (resetInformed) {
207 myWasInformed = false;
208 }
209}
210
211
212void
214 if (!isRetriever(retriever)) {
215 myRetrievers.push_back(retriever);
216 }
217}
218
219
220void
222 std::vector<OutputDevice*>::iterator i = find(myRetrievers.begin(), myRetrievers.end(), retriever);
223 if (i != myRetrievers.end()) {
224 myRetrievers.erase(i);
225 }
226}
227
228
229bool
231 return std::find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
232}
233
234
235void
237 if (myErrorInstance != nullptr) {
239 }
240 if (myWarningInstance != nullptr) {
242 }
243 if (myMessageInstance != nullptr) {
245 }
246}
247
248
249void
250MsgHandler::setupI18n(const std::string& locale) {
251#ifdef HAVE_INTL
252 if (locale != "") {
253#ifdef WIN32
254 _putenv_s("LANGUAGE", locale.data());
255#else
256 setenv("LANGUAGE", locale.data(), true);
257#endif
258 }
259 if (!setlocale(LC_MESSAGES, "")) {
260 WRITE_WARNINGF(TL("Could not set locale to '%'."), locale);
261 }
262 const char* sumoPath = getenv("SUMO_HOME");
263 if (sumoPath == nullptr) {
264 if (!bindtextdomain("sumo", nullptr)) {
265 WRITE_WARNING(TL("Environment variable SUMO_HOME is not set, could not find localized messages."));
266 return;
267 }
268 } else {
269 const std::string path = sumoPath + std::string("/data/locale/");
270 if (!bindtextdomain("sumo", path.data())) {
271 WRITE_WARNING(TL("Could not find localized messages."));
272 return;
273 }
274 }
275 bind_textdomain_codeset("sumo", "UTF-8");
276 textdomain("sumo");
277#ifdef WIN32
278 SetConsoleOutputCP(CP_UTF8);
279#endif
280#else
281 UNUSED_PARAMETER(locale);
282#endif
283 myWarningPrefix = TL("Warning: ");
284 myErrorPrefix = TL("Error: ");
285 gLocaleInitialized = true;
286}
287
288
289void
291 delete myMessageInstance;
292 myMessageInstance = nullptr;
293 delete myWarningInstance;
294 myWarningInstance = nullptr;
295 delete myErrorInstance;
296 myErrorInstance = nullptr;
297}
298
299
300std::string
302 std::stringstream prefix;
303 prefix << "[PID: ";
304#ifdef WIN32
305 prefix << GetCurrentProcessId();
306#else
307 prefix << getpid();
308#endif
309 prefix << "] ";
310 return prefix.str();
311}
312
313
315 myType(type), myWasInformed(false), myAggregationThreshold(-1) {
316 if (type == MsgType::MT_MESSAGE) {
318 } else {
320 }
321}
322
323
326
327
328bool
330 return myWasInformed;
331}
332
333
334/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:299
#define WRITE_WARNING(msg)
Definition MsgHandler.h:298
#define TL(string)
Definition MsgHandler.h:316
#define TLF(string,...)
Definition MsgHandler.h:318
bool gLocaleInitialized
Definition StdDefs.cpp:34
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
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.
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
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
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
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.
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 *(* 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.
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 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
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
@ 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
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Static storage of an output device and its base (abstract) implementation.
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
#define UNUSED_PARAMETER(x)