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-2020 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  // beautify progress output
119  if (myAmProcessingProcess) {
120  myAmProcessingProcess = false;
122  }
123  msg = build(msg, addType);
124  // inform all receivers
125  for (auto i : myRetrievers) {
126  i->inform(msg);
127  }
128  // set the information that something occurred
129  myWasInformed = true;
130 }
131 
132 
133 void
134 MsgHandler::beginProcessMsg(std::string msg, bool addType) {
135  msg = build(msg, addType);
136  // inform all other receivers
137  for (auto i : myRetrievers) {
138  i->inform(msg, ' ');
139  myAmProcessingProcess = true;
140  }
141  // set the information that something occurred
142  myWasInformed = true;
143 }
144 
145 
146 void
147 MsgHandler::endProcessMsg(std::string msg) {
148  // inform all other receivers
149  for (auto i : myRetrievers) {
150  i->inform(msg);
151  }
152  // set the information that something occurred
153  myWasInformed = true;
154  myAmProcessingProcess = false;
155 }
156 
157 
158 void
159 MsgHandler::clear(bool resetInformed) {
160  if (resetInformed) {
161  myWasInformed = false;
162  }
163  if (myAggregationThreshold >= 0) {
164  for (const auto& i : myAggregationCount) {
165  if (i.second > myAggregationThreshold) {
166  inform(toString(i.second) + " total messages of type: " + i.first);
167  }
168  }
169  }
170  myAggregationCount.clear();
171 }
172 
173 
174 void
176  if (!isRetriever(retriever)) {
177  myRetrievers.push_back(retriever);
178  }
179 }
180 
181 
182 void
184  std::vector<OutputDevice*>::iterator i = find(myRetrievers.begin(), myRetrievers.end(), retriever);
185  if (i != myRetrievers.end()) {
186  myRetrievers.erase(i);
187  }
188 }
189 
190 
191 bool
193  return std::find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
194 }
195 
196 
197 void
199  if (myDebugInstance != nullptr) {
201  }
202  if (myGLDebugInstance != nullptr) {
204  }
205  if (myErrorInstance != nullptr) {
207  }
208  if (myWarningInstance != nullptr) {
210  }
211  if (myMessageInstance != nullptr) {
213  }
214 }
215 
216 void
218  // initialize console properly
219  OutputDevice::getDevice("stdout");
220  OutputDevice::getDevice("stderr");
222  getWarningInstance()->setAggregationThreshold(oc.getInt("aggregate-warnings"));
223  getErrorInstance()->setAggregationThreshold(oc.getInt("aggregate-warnings"));
224  if (oc.getBool("no-warnings")) {
226  }
227  // build the logger if possible
228  if (oc.isSet("log", false)) {
229  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
230  getErrorInstance()->addRetriever(logFile);
231  if (!oc.getBool("no-warnings")) {
232  getWarningInstance()->addRetriever(logFile);
233  }
234  getMessageInstance()->addRetriever(logFile);
235  }
236  if (oc.isSet("message-log", false)) {
237  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
238  getMessageInstance()->addRetriever(logFile);
239  }
240  if (oc.isSet("error-log", false)) {
241  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
242  getErrorInstance()->addRetriever(logFile);
243  getWarningInstance()->addRetriever(logFile);
244  }
245  if (!oc.getBool("verbose")) {
247  }
248 }
249 
250 
251 void
253  delete myMessageInstance;
254  myMessageInstance = nullptr;
255  delete myWarningInstance;
256  myWarningInstance = nullptr;
257  delete myErrorInstance;
258  myErrorInstance = nullptr;
259  delete myDebugInstance;
260  myDebugInstance = nullptr;
261  delete myGLDebugInstance;
262  myGLDebugInstance = nullptr;
263 }
264 
265 
267  myType(type), myWasInformed(false), myAggregationThreshold(-1) {
268  if (type == MT_MESSAGE) {
270  } else {
272  }
273 }
274 
275 
277 }
278 
279 
280 bool
282  return myWasInformed;
283 }
284 
285 
286 /****************************************************************************/
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:175
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:281
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:227
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:147
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:163
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition: MsgHandler.h:221
bool myWasInformed
information whether an output occurred at all
Definition: MsgHandler.h:246
MsgHandler *(* Factory)(MsgType)
Definition: MsgHandler.h:57
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:217
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:230
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:236
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:192
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:252
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:239
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:255
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:276
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:159
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:224
void setAggregationThreshold(const int thresh)
Definition: MsgHandler.h:209
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:233
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:134
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:268
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:269
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:252
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
Definition: MsgHandler.cpp:198
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:183
int myAggregationThreshold
do not output more messages of the same type if the count exceeds this threshold
Definition: MsgHandler.h:249
@ MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:45
@ MT_DEBUG
The message is an debug.
Definition: MsgHandler.h:51
@ MT_WARNING
The message is a warning.
Definition: MsgHandler.h:47
@ MT_GLDEBUG
The message is an debug.
Definition: MsgHandler.h:53
@ MT_ERROR
The message is an error.
Definition: MsgHandler.h:49
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:266
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:60
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.