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 // ===========================================================================
37 
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 
55  if (myMessageInstance == nullptr) {
56  if (myFactory == nullptr) {
58  } else {
60  }
61  }
62  return myMessageInstance;
63 }
64 
65 
68  if (myWarningInstance == nullptr) {
69  if (myFactory == nullptr) {
71  } else {
73  }
74  }
75  return myWarningInstance;
76 }
77 
78 
81  if (myErrorInstance == nullptr) {
83  }
84  return myErrorInstance;
85 }
86 
87 
90  if (myDebugInstance == nullptr) {
92  }
93  return myDebugInstance;
94 }
95 
96 
99  if (myGLDebugInstance == nullptr) {
101  }
102  return myGLDebugInstance;
103 }
104 
105 
106 void
108  myWriteDebugMessages = enable;
109 }
110 
111 void
113  myWriteDebugGLMessages = enable;
114 }
115 
116 void
117 MsgHandler::inform(std::string msg, bool addType) {
118  if (addType && !myInitialMessages.empty() && myInitialMessages.size() < 5) {
119  myInitialMessages.push_back(msg);
120  }
121  // beautify progress output
122  if (myAmProcessingProcess) {
123  myAmProcessingProcess = false;
125  }
126  msg = build(msg, addType);
127  // inform all receivers
128  for (auto i : myRetrievers) {
129  i->inform(msg);
130  }
131  // set the information that something occurred
132  myWasInformed = true;
133 }
134 
135 
136 void
137 MsgHandler::beginProcessMsg(std::string msg, bool addType) {
138  msg = build(msg, addType);
139  // inform all other receivers
140  for (auto i : myRetrievers) {
141  i->inform(msg, ' ');
142  myAmProcessingProcess = true;
143  }
144  // set the information that something occurred
145  myWasInformed = true;
146 }
147 
148 
149 void
150 MsgHandler::endProcessMsg(std::string msg) {
151  // inform all other receivers
152  for (auto i : myRetrievers) {
153  i->inform(msg);
154  }
155  // set the information that something occurred
156  myWasInformed = true;
157  myAmProcessingProcess = false;
158 }
159 
160 
161 void
162 MsgHandler::clear(bool resetInformed) {
163  if (resetInformed) {
164  myWasInformed = false;
165  }
166  if (myAggregationThreshold >= 0) {
167  for (const auto& i : myAggregationCount) {
168  if (i.second > myAggregationThreshold) {
169  inform(toString(i.second) + " total messages of type: " + i.first);
170  }
171  }
172  }
173  myAggregationCount.clear();
174  if (!resetInformed && myInitialMessages.size() > 1) {
175  const bool wasInformed = myWasInformed;
176  for (const std::string& msg : myInitialMessages) {
177  inform(msg, false);
178  }
179  myInitialMessages.clear();
181  }
182 }
183 
184 
185 void
187  if (!isRetriever(retriever)) {
188  myRetrievers.push_back(retriever);
189  }
190 }
191 
192 
193 void
195  std::vector<OutputDevice*>::iterator i = find(myRetrievers.begin(), myRetrievers.end(), retriever);
196  if (i != myRetrievers.end()) {
197  myRetrievers.erase(i);
198  }
199 }
200 
201 
202 bool
204  return std::find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
205 }
206 
207 
208 void
210  if (myDebugInstance != nullptr) {
212  }
213  if (myGLDebugInstance != nullptr) {
215  }
216  if (myErrorInstance != nullptr) {
218  }
219  if (myWarningInstance != nullptr) {
221  }
222  if (myMessageInstance != nullptr) {
224  }
225 }
226 
227 void
229  // initialize console properly
230  OutputDevice::getDevice("stdout");
231  OutputDevice::getDevice("stderr");
233  getWarningInstance()->setAggregationThreshold(oc.getInt("aggregate-warnings"));
234  getErrorInstance()->setAggregationThreshold(oc.getInt("aggregate-warnings"));
235  if (oc.getBool("no-warnings")) {
237  }
238  // build the logger if possible
239  if (oc.isSet("log", false)) {
240  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
241  getErrorInstance()->addRetriever(logFile);
242  if (!oc.getBool("no-warnings")) {
243  getWarningInstance()->addRetriever(logFile);
244  }
245  getMessageInstance()->addRetriever(logFile);
246  }
247  if (oc.isSet("message-log", false)) {
248  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
249  getMessageInstance()->addRetriever(logFile);
250  }
251  if (oc.isSet("error-log", false)) {
252  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
253  getErrorInstance()->addRetriever(logFile);
254  getWarningInstance()->addRetriever(logFile);
255  }
256  if (oc.getBool("verbose")) {
257  getErrorInstance()->myInitialMessages.push_back("Repeating initial error messages:");
258  } else {
260  }
261 }
262 
263 
264 void
266  delete myMessageInstance;
267  myMessageInstance = nullptr;
268  delete myWarningInstance;
269  myWarningInstance = nullptr;
270  delete myErrorInstance;
271  myErrorInstance = nullptr;
272  delete myDebugInstance;
273  myDebugInstance = nullptr;
274  delete myGLDebugInstance;
275  myGLDebugInstance = nullptr;
276 }
277 
278 
280  myType(type), myWasInformed(false), myAggregationThreshold(-1) {
281  if (type == MsgType::MT_MESSAGE) {
283  } else {
285  }
286 }
287 
288 
290 }
291 
292 
293 bool
295  return myWasInformed;
296 }
297 
298 
299 /****************************************************************************/
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:186
std::vector< std::string > myInitialMessages
storage for initial messages
Definition: MsgHandler.h:259
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:98
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:294
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:117
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:112
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:228
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:150
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:164
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition: MsgHandler.h:222
bool myWasInformed
information whether an output occurred at all
Definition: MsgHandler.h:247
MsgHandler *(* Factory)(MsgType)
Definition: MsgHandler.h:58
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:228
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:231
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:89
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:237
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:203
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:67
std::map< const std::string, int > myAggregationCount
count for messages of the same type
Definition: MsgHandler.h:253
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:107
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:240
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:256
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:289
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:162
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:225
void setAggregationThreshold(const int thresh)
Definition: MsgHandler.h:210
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:234
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:137
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:272
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:273
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:265
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
Definition: MsgHandler.cpp:209
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:194
int myAggregationThreshold
do not output more messages of the same type if the count exceeds this threshold
Definition: MsgHandler.h:250
@ 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:279
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:54
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:58
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.