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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // Retrieves messages about the process and gives them further to output
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <string>
25 #include <cassert>
26 #include <vector>
27 #include <algorithm>
28 #include <iostream>
32 #include "MsgHandler.h"
33 
34 
35 // ===========================================================================
36 // static member variables
37 // ===========================================================================
38 
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 
56  if (myMessageInstance == nullptr) {
57  if (myFactory == nullptr) {
59  } else {
61  }
62  }
63  return myMessageInstance;
64 }
65 
66 
69  if (myWarningInstance == nullptr) {
70  if (myFactory == nullptr) {
72  } else {
74  }
75  }
76  return myWarningInstance;
77 }
78 
79 
82  if (myErrorInstance == nullptr) {
84  }
85  return myErrorInstance;
86 }
87 
88 
91  if (myDebugInstance == nullptr) {
93  }
94  return myDebugInstance;
95 }
96 
97 
100  if (myGLDebugInstance == nullptr) {
102  }
103  return myGLDebugInstance;
104 }
105 
106 
107 void
109  myWriteDebugMessages = enable;
110 }
111 
112 void
114  myWriteDebugGLMessages = enable;
115 }
116 
117 void
118 MsgHandler::inform(std::string msg, bool addType) {
119  // beautify progress output
120  if (myAmProcessingProcess) {
121  myAmProcessingProcess = false;
123  }
124  msg = build(msg, addType);
125  // inform all receivers
126  for (auto i : myRetrievers) {
127  i->inform(msg);
128  }
129  // set the information that something occurred
130  myWasInformed = true;
131 }
132 
133 
134 void
135 MsgHandler::beginProcessMsg(std::string msg, bool addType) {
136  msg = build(msg, addType);
137  // inform all other receivers
138  for (auto i : myRetrievers) {
139  i->inform(msg, ' ');
140  myAmProcessingProcess = true;
141  }
142  // set the information that something occurred
143  myWasInformed = true;
144 }
145 
146 
147 void
148 MsgHandler::endProcessMsg(std::string msg) {
149  // inform all other receivers
150  for (auto i : myRetrievers) {
151  i->inform(msg);
152  }
153  // set the information that something occurred
154  myWasInformed = true;
155  myAmProcessingProcess = false;
156 }
157 
158 
159 void
161  myWasInformed = false;
162  if (myAggregationThreshold >= 0) {
163  for (const auto& i : myAggregationCount) {
164  if (i.second > myAggregationThreshold) {
165  inform(toString(i.second) + " total messages of type: " + i.first);
166  }
167  }
168  }
169  myAggregationCount.clear();
170 }
171 
172 
173 void
175  if (!isRetriever(retriever)) {
176  myRetrievers.push_back(retriever);
177  }
178 }
179 
180 
181 void
183  std::vector<OutputDevice*>::iterator i = find(myRetrievers.begin(), myRetrievers.end(), retriever);
184  if (i != myRetrievers.end()) {
185  myRetrievers.erase(i);
186  }
187 }
188 
189 
190 bool
192  return std::find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
193 }
194 
195 
196 void
198  if (myDebugInstance != nullptr) {
200  }
201  if (myGLDebugInstance != nullptr) {
203  }
204  if (myErrorInstance != nullptr) {
206  }
207  if (myWarningInstance != nullptr) {
209  }
210  if (myMessageInstance != nullptr) {
212  }
213 }
214 
215 void
217  // initialize console properly
218  OutputDevice::getDevice("stdout");
219  OutputDevice::getDevice("stderr");
221  getWarningInstance()->setAggregationThreshold(oc.getInt("aggregate-warnings"));
222  if (oc.getBool("no-warnings")) {
224  }
225  // build the logger if possible
226  if (oc.isSet("log", false)) {
227  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
228  getErrorInstance()->addRetriever(logFile);
229  if (!oc.getBool("no-warnings")) {
230  getWarningInstance()->addRetriever(logFile);
231  }
232  getMessageInstance()->addRetriever(logFile);
233  }
234  if (oc.isSet("message-log", false)) {
235  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
236  getMessageInstance()->addRetriever(logFile);
237  }
238  if (oc.isSet("error-log", false)) {
239  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
240  getErrorInstance()->addRetriever(logFile);
241  getWarningInstance()->addRetriever(logFile);
242  }
243  if (!oc.getBool("verbose")) {
245  }
246 }
247 
248 
249 void
251  delete myMessageInstance;
252  myMessageInstance = nullptr;
253  delete myWarningInstance;
254  myWarningInstance = nullptr;
255  delete myErrorInstance;
256  myErrorInstance = nullptr;
257  delete myDebugInstance;
258  myDebugInstance = nullptr;
259  delete myGLDebugInstance;
260  myGLDebugInstance = nullptr;
261 }
262 
263 
265  myType(type), myWasInformed(false), myAggregationThreshold(-1) {
266  if (type == MT_MESSAGE) {
268  } else {
270  }
271 }
272 
273 
275 }
276 
277 
278 bool
280  return myWasInformed;
281 }
282 
283 
284 /****************************************************************************/
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
MsgHandler::MsgType
MsgType
Definition: MsgHandler.h:45
MsgHandler::MT_ERROR
@ MT_ERROR
The message is an error.
Definition: MsgHandler.h:51
OptionsCont::getInt
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
Definition: OptionsCont.cpp:215
MsgHandler::setAggregationThreshold
void setAggregationThreshold(const int thresh)
Definition: MsgHandler.h:208
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:216
MsgHandler::wasInformed
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:279
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
MsgHandler::removeRetrieverFromAllInstances
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
Definition: MsgHandler.cpp:197
OptionsCont.h
MsgHandler::MT_MESSAGE
@ MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:47
MsgHandler.h
MsgHandler::MT_WARNING
@ MT_WARNING
The message is a warning.
Definition: MsgHandler.h:49
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:201
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
MsgHandler::isRetriever
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:191
MsgHandler::beginProcessMsg
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:135
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
MsgHandler::build
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:166
MsgHandler::myWasInformed
bool myWasInformed
information whether an output occurred at all
Definition: MsgHandler.h:245
MsgHandler::myAggregationCount
std::map< const std::string, int > myAggregationCount
count for messages of the same type
Definition: MsgHandler.h:251
MsgHandler::cleanupOnEnd
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:250
MsgHandler::myErrorInstance
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:229
MsgHandler::myAmProcessingProcess
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:238
MsgHandler::clear
virtual void clear()
Clears information whether an error occurred previously.
Definition: MsgHandler.cpp:160
MsgHandler::enableDebugGLMessages
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:113
MsgHandler::Factory
MsgHandler *(* Factory)(MsgType)
Definition: MsgHandler.h:59
MsgHandler::myWarningInstance
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:232
MsgHandler
Definition: MsgHandler.h:38
MsgHandler::getWarningInstance
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:68
MsgHandler::endProcessMsg
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:148
OutputDevice.h
MsgHandler::MsgHandler
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:264
MsgHandler::myWriteDebugGLMessages
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:268
MsgHandler::getGLDebugInstance
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:99
MsgHandler::myFactory
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition: MsgHandler.h:220
UtilExceptions.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
MsgHandler::myMessageInstance
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:235
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:54
MsgHandler::myDebugInstance
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:223
MsgHandler::myAggregationThreshold
int myAggregationThreshold
do not output more messages of the same type if the count exceeds this threshold
Definition: MsgHandler.h:248
MsgHandler::getDebugInstance
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:90
MsgHandler::MT_DEBUG
@ MT_DEBUG
The message is an debug.
Definition: MsgHandler.h:53
MsgHandler::addRetriever
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:174
config.h
MsgHandler::myRetrievers
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:254
MsgHandler::MT_GLDEBUG
@ MT_GLDEBUG
The message is an debug.
Definition: MsgHandler.h:55
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
MsgHandler::myGLDebugInstance
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:226
MsgHandler::myWriteDebugMessages
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:267
MsgHandler::~MsgHandler
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:274
MsgHandler::removeRetriever
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:182
MsgHandler::enableDebugMessages
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:108
MsgHandler::getMessageInstance
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:55