Eclipse SUMO - Simulation of Urban MObility
MsgHandler.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2001-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/****************************************************************************/
19// Retrieves messages about the process and gives them further to output
20/****************************************************************************/
21#include <config.h>
22
23#include <string>
24#include <cassert>
25#include <vector>
26#include <algorithm>
27#include <iostream>
31#include "MsgHandler.h"
32
33
34// ===========================================================================
35// static member variables
36// ===========================================================================
46
47
48// ===========================================================================
49// method definitions
50// ===========================================================================
51
54 if (myMessageInstance == nullptr) {
55 if (myFactory == nullptr) {
57 } else {
59 }
60 }
61 return myMessageInstance;
62}
63
64
67 if (myWarningInstance == nullptr) {
68 if (myFactory == nullptr) {
70 } else {
72 }
73 }
74 return myWarningInstance;
75}
76
77
80 if (myErrorInstance == nullptr) {
82 }
83 return myErrorInstance;
84}
85
86
89 if (myDebugInstance == nullptr) {
91 }
92 return myDebugInstance;
93}
94
95
98 if (myGLDebugInstance == nullptr) {
100 }
101 return myGLDebugInstance;
102}
103
104
105void
107 myWriteDebugMessages = enable;
108}
109
110void
112 myWriteDebugGLMessages = enable;
113}
114
115void
116MsgHandler::inform(std::string msg, bool addType) {
117 if (addType && !myInitialMessages.empty() && myInitialMessages.size() < 5) {
118 myInitialMessages.push_back(msg);
119 }
120 // beautify progress output
122 myAmProcessingProcess = false;
124 }
125 msg = build(msg, addType);
126 // inform all receivers
127 for (auto i : myRetrievers) {
128 i->inform(msg);
129 }
130 // set the information that something occurred
131 myWasInformed = true;
132}
133
134
135void
136MsgHandler::beginProcessMsg(std::string msg, bool addType) {
137 msg = build(msg, addType);
138 // inform all other receivers
139 for (auto i : myRetrievers) {
140 i->inform(msg, ' ');
142 }
143 // set the information that something occurred
144 myWasInformed = true;
145}
146
147
148void
150 // inform all other receivers
151 for (auto i : myRetrievers) {
152 i->inform(msg);
153 }
154 // set the information that something occurred
155 myWasInformed = true;
156 myAmProcessingProcess = false;
157}
158
159
160void
161MsgHandler::clear(bool resetInformed) {
162 if (resetInformed) {
163 myWasInformed = false;
164 }
165 if (myAggregationThreshold >= 0) {
166 for (const auto& i : myAggregationCount) {
167 if (i.second > myAggregationThreshold) {
168 inform(toString(i.second) + " total messages of type: " + i.first);
169 }
170 }
171 }
172 myAggregationCount.clear();
173 if (!resetInformed && myInitialMessages.size() > 1) {
174 const bool wasInformed = myWasInformed;
175 for (const std::string& msg : myInitialMessages) {
176 inform(msg, false);
177 }
178 myInitialMessages.clear();
180 }
181}
182
183
184void
186 if (!isRetriever(retriever)) {
187 myRetrievers.push_back(retriever);
188 }
189}
190
191
192void
194 std::vector<OutputDevice*>::iterator i = find(myRetrievers.begin(), myRetrievers.end(), retriever);
195 if (i != myRetrievers.end()) {
196 myRetrievers.erase(i);
197 }
198}
199
200
201bool
203 return std::find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
204}
205
206
207void
209 if (myDebugInstance != nullptr) {
211 }
212 if (myGLDebugInstance != nullptr) {
214 }
215 if (myErrorInstance != nullptr) {
217 }
218 if (myWarningInstance != nullptr) {
220 }
221 if (myMessageInstance != nullptr) {
223 }
224}
225
226
227void
228MsgHandler::setupI18n(const std::string& locale) {
229#ifdef HAVE_INTL
230 if (!setlocale(LC_MESSAGES, locale.data())) {
231 WRITE_WARNING("Could not set locale to '" + locale + "'.");
232 }
233 const char* sumoPath = std::getenv("SUMO_HOME");
234 if (sumoPath == nullptr) {
235 if (!bindtextdomain("sumo", nullptr)) {
236 WRITE_WARNING(TL("Environment variable SUMO_HOME is not set, could not find localized messages."));
237 return;
238 }
239 } else {
240 const std::string path = sumoPath + std::string("/data/locale/");
241 if (!bindtextdomain("sumo", path.data())) {
242 WRITE_WARNING(TL("Could not find localized messages."));
243 return;
244 }
245 }
246 bind_textdomain_codeset("sumo", "UTF-8");
247 textdomain("sumo");
248#else
249 UNUSED_PARAMETER(locale);
250#endif
251}
252
253
254void
256 // initialize console properly
257 OutputDevice::getDevice("stdout");
258 OutputDevice::getDevice("stderr");
260 getWarningInstance()->setAggregationThreshold(oc.getInt("aggregate-warnings"));
261 getErrorInstance()->setAggregationThreshold(oc.getInt("aggregate-warnings"));
262 if (oc.getBool("no-warnings")) {
264 }
265 // build the logger if possible
266 if (oc.isSet("log", false)) {
267 OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
268 getErrorInstance()->addRetriever(logFile);
269 if (!oc.getBool("no-warnings")) {
271 }
273 }
274 if (oc.isSet("message-log", false)) {
275 OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
277 }
278 if (oc.isSet("error-log", false)) {
279 OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
280 getErrorInstance()->addRetriever(logFile);
282 }
283 if (oc.getBool("verbose")) {
284 getErrorInstance()->myInitialMessages.push_back("Repeating initial error messages:");
285 } else {
287 }
288}
289
290
291void
293 delete myMessageInstance;
294 myMessageInstance = nullptr;
295 delete myWarningInstance;
296 myWarningInstance = nullptr;
297 delete myErrorInstance;
298 myErrorInstance = nullptr;
299 delete myDebugInstance;
300 myDebugInstance = nullptr;
301 delete myGLDebugInstance;
302 myGLDebugInstance = nullptr;
303}
304
305
307 myType(type), myWasInformed(false), myAggregationThreshold(-1) {
308 if (type == MsgType::MT_MESSAGE) {
310 } else {
312 }
313}
314
315
317}
318
319
320bool
322 return myWasInformed;
323}
324
325
326/****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
#define TL(string)
Definition: MsgHandler.h:282
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
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
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
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
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
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
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 *(* 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 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
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
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.
Definition: OptionsCont.cpp:59
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.