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
37#include "MsgHandler.h"
38
39
40// ===========================================================================
41// static member variables
42// ===========================================================================
52std::string MsgHandler::myErrorPrefix = "Error: ";
53std::string MsgHandler::myWarningPrefix = "Warning: ";
54
55
56// ===========================================================================
57// method definitions
58// ===========================================================================
59
71
72
84
85
93
94
95void
99
100void
104
105
106std::string
107MsgHandler::insertLineBreaks(std::string msg, int lineWidth) {
108 // TODO: check what FXFont::getTextWidth can do
109 //int textWidth = getApp()->getNormalFont()->getTextWidth
110 if ((int)msg.size() <= lineWidth) {
111 return msg;
112 }
113 size_t pos = 0;
114 size_t nextLineBreak = msg.find('\n');
115 size_t spaceAfterLine = msg.find(' ', lineWidth);
116 while (spaceAfterLine != std::string::npos) {
117 if (nextLineBreak == std::string::npos || nextLineBreak > spaceAfterLine) {
118 msg = msg.replace(spaceAfterLine, 1, "\n");
119 pos = spaceAfterLine + 1;
120 } else {
121 pos = nextLineBreak + 1;
122 }
123 spaceAfterLine = msg.find(' ', pos + lineWidth);
124 nextLineBreak = msg.find('\n', pos);
125 }
126 return msg;
127}
128
129
130void
131MsgHandler::inform(std::string msg, bool addType) {
132 if (addType && !myInitialMessages.empty() && myInitialMessages.size() < 5) {
133 myInitialMessages.push_back(msg);
134 }
135 // beautify progress output
137 myAmProcessingProcess = false;
139 }
140 msg = build(msg, addType);
141 // inform all receivers
142 for (auto i : myRetrievers) {
143 i->inform(msg);
144 }
145 // set the information that something occurred
146 myWasInformed = true;
147}
148
149
150void
151MsgHandler::beginProcessMsg(std::string msg, bool addType) {
152 msg = build(msg, addType);
153 // inform all other receivers
154 for (auto i : myRetrievers) {
155 i->inform(msg, true);
157 }
158 // set the information that something occurred
159 myWasInformed = true;
160}
161
162
163void
164MsgHandler::endProcessMsg2(bool success, long duration) {
165 if (success) {
166 if (duration > -1) {
167 endProcessMsg(TLF(" done (%ms).", toString(duration)));
168 } else {
169 endProcessMsg(TL(" done."));
170 }
171 } else {
172 endProcessMsg(TL(" failed."));
173 }
174}
175
176
177void
179 // inform all other receivers
180 for (auto i : myRetrievers) {
181 i->inform(msg);
182 }
183 // set the information that something occurred
184 myWasInformed = true;
185 myAmProcessingProcess = false;
186}
187
188
189void
190MsgHandler::clear(bool resetInformed) {
191 if (myAggregationThreshold >= 0) {
192 for (const auto& i : myAggregationCount) {
193 if (i.second > myAggregationThreshold) {
194 inform(toString(i.second) + " total messages of type: " + i.first);
195 }
196 }
197 }
198 myAggregationCount.clear();
199 if (!resetInformed && myInitialMessages.size() > 1) {
200 const bool wasInformed = myWasInformed;
201 for (const std::string& msg : myInitialMessages) {
202 inform(msg, false);
203 }
204 myInitialMessages.clear();
206 }
207 if (resetInformed) {
208 myWasInformed = false;
209 }
210}
211
212
213void
215 if (!isRetriever(retriever)) {
216 myRetrievers.push_back(retriever);
217 }
218}
219
220
221void
223 std::vector<OutputDevice*>::iterator i = find(myRetrievers.begin(), myRetrievers.end(), retriever);
224 if (i != myRetrievers.end()) {
225 myRetrievers.erase(i);
226 }
227}
228
229
230bool
232 return std::find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
233}
234
235
236void
238 if (myErrorInstance != nullptr) {
240 }
241 if (myWarningInstance != nullptr) {
243 }
244 if (myMessageInstance != nullptr) {
246 }
247}
248
249
250void
251MsgHandler::setupI18n(const std::string& locale) {
252#ifdef HAVE_INTL
253 if (locale != "") {
254#ifdef WIN32
255 _putenv_s("LANGUAGE", locale.data());
256#else
257 setenv("LANGUAGE", locale.data(), true);
258#endif
259 }
260 if (!setlocale(LC_MESSAGES, "")) {
261 WRITE_WARNINGF(TL("Could not set locale to '%'."), locale);
262 }
263 const char* sumoPath = getenv("SUMO_HOME");
264 if (sumoPath == nullptr) {
265 if (!bindtextdomain("sumo", nullptr)) {
266 WRITE_WARNING(TL("Environment variable SUMO_HOME is not set, could not find localized messages."));
267 return;
268 }
269 } else {
270 const std::string path = sumoPath + std::string("/data/locale/");
271 if (!bindtextdomain("sumo", path.data())) {
272 WRITE_WARNING(TL("Could not find localized messages."));
273 return;
274 }
275 }
276 bind_textdomain_codeset("sumo", "UTF-8");
277 textdomain("sumo");
278#ifdef WIN32
279 SetConsoleOutputCP(CP_UTF8);
280#endif
281#else
282 UNUSED_PARAMETER(locale);
283#endif
284 myWarningPrefix = TL("Warning: ");
285 myErrorPrefix = TL("Error: ");
286 gLocaleInitialized = true;
287}
288
289
290void
292 // initialize console properly
293 OutputDevice::getDevice("stdout");
294 OutputDevice::getDevice("stderr");
296 getWarningInstance()->setAggregationThreshold(oc.getInt("aggregate-warnings"));
297 getErrorInstance()->setAggregationThreshold(oc.getInt("aggregate-warnings"));
298 if (oc.getBool("no-warnings")) {
300 }
301 // build the logger if possible
302 if (oc.isSet("log", false)) {
303 OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
304 getErrorInstance()->addRetriever(logFile);
305 if (!oc.getBool("no-warnings")) {
307 }
309 if (oc.getBool("log.timestamps")) {
310 myWriteTimestamps = true;
311 }
312 if (oc.getBool("log.processid")) {
313 myWriteProcessId = true;
314 }
315 }
316 if (oc.isSet("message-log", false)) {
317 OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
319 }
320 if (oc.isSet("error-log", false)) {
321 OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
322 getErrorInstance()->addRetriever(logFile);
324 }
325 if (oc.getBool("verbose")) {
326 getErrorInstance()->myInitialMessages.push_back("Repeating initial error messages:");
327 } else {
329 }
330}
331
332
333void
335 delete myMessageInstance;
336 myMessageInstance = nullptr;
337 delete myWarningInstance;
338 myWarningInstance = nullptr;
339 delete myErrorInstance;
340 myErrorInstance = nullptr;
341}
342
343
344std::string
346 std::stringstream prefix;
347 prefix << "[PID: ";
348#ifdef WIN32
349 prefix << GetCurrentProcessId();
350#else
351 prefix << getpid();
352#endif
353 prefix << "] ";
354 return prefix.str();
355}
356
357
359 myType(type), myWasInformed(false), myAggregationThreshold(-1) {
360 if (type == MsgType::MT_MESSAGE) {
362 } else {
364 }
365}
366
367
370
371
372bool
374 return myWasInformed;
375}
376
377
378/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
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:251
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:177
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition MsgHandler.h:220
bool myWasInformed
information whether an output occurred at all
Definition MsgHandler.h:239
static bool myWriteTimestamps
Whether to prefix every message with a time stamp.
Definition MsgHandler.h:263
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:223
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition MsgHandler.h:229
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:245
static void enableDebugMessages(bool enable)
enable/disable debug messages
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition MsgHandler.h:232
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition MsgHandler.h:248
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 MsgHandler * myWarningInstance
The instance to handle warnings.
Definition MsgHandler.h:226
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:257
static bool myWriteDebugGLMessages
Flag to enable or disable GL specific debug output.
Definition MsgHandler.h:260
static std::string myWarningPrefix
The possibly translated warning prefix (mainly for speedup)
Definition MsgHandler.h:272
static bool myWriteProcessId
Whether to prefix every message with the process id.
Definition MsgHandler.h:266
static void cleanupOnEnd()
Removes pending handler.
static std::string myErrorPrefix
The possibly translated error prefix (mainly for speedup)
Definition MsgHandler.h:269
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:242
@ 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.
A storage for options typed value containers)
Definition OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static OptionsCont & getOptions()
Retrieves the options.
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)