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-2018 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 /****************************************************************************/
16 // Retrieves messages about the process and gives them further to output
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <cassert>
27 #include <vector>
28 #include <algorithm>
29 #include <iostream>
33 
34 #include "MsgHandler.h"
35 #include "AbstractMutex.h"
36 
37 
38 // ===========================================================================
39 // static member variables
40 // ===========================================================================
41 
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 
59  if (myMessageInstance == nullptr) {
61  }
62  return myMessageInstance;
63 }
64 
65 
68  if (myWarningInstance == nullptr) {
70  }
71  return myWarningInstance;
72 }
73 
74 
77  if (myErrorInstance == nullptr) {
79  }
80  return myErrorInstance;
81 }
82 
83 
86  if (myDebugInstance == nullptr) {
88  }
89  return myDebugInstance;
90 }
91 
92 
95  if (myGLDebugInstance == nullptr) {
97  }
98  return myGLDebugInstance;
99 }
100 
101 
102 void
104  myWriteDebugMessages = enable;
105 }
106 
107 void
109  myWriteDebugGLMessages = enable;
110 }
111 
112 void
113 MsgHandler::inform(std::string msg, bool addType) {
114  if (myLock != nullptr) {
115  myLock->lock();
116  }
117  // beautify progress output
118  if (myAmProcessingProcess) {
119  myAmProcessingProcess = false;
121  }
122  msg = build(msg, addType);
123  // inform all receivers
124  for (auto i : myRetrievers) {
125  i->inform(msg);
126  }
127  // set the information that something occurred
128  myWasInformed = true;
129  if (myLock != nullptr) {
130  myLock->unlock();
131  }
132 }
133 
134 
135 void
136 MsgHandler::beginProcessMsg(std::string msg, bool addType) {
137  if (myLock != nullptr) {
138  myLock->lock();
139  }
140  msg = build(msg, addType);
141  // inform all other receivers
142  for (auto i : myRetrievers) {
143  i->inform(msg, ' ');
144  myAmProcessingProcess = true;
145  }
146  // set the information that something occurred
147  myWasInformed = true;
148  if (myLock != nullptr) {
149  myLock->unlock();
150  }
151 }
152 
153 
154 void
155 MsgHandler::endProcessMsg(std::string msg) {
156  if (myLock != nullptr) {
157  myLock->lock();
158  }
159  // inform all other receivers
160  for (auto i : myRetrievers) {
161  i->inform(msg);
162  }
163  // set the information that something occurred
164  myWasInformed = true;
165  myAmProcessingProcess = false;
166  if (myLock != nullptr) {
167  myLock->unlock();
168  }
169 }
170 
171 
172 void
174  if (myLock != nullptr) {
175  myLock->lock();
176  }
177  myWasInformed = false;
178  if (myLock != nullptr) {
179  myLock->unlock();
180  }
181 }
182 
183 
184 void
186  if (myLock != nullptr) {
187  myLock->lock();
188  }
189  if (!isRetriever(retriever)) {
190  myRetrievers.push_back(retriever);
191  }
192  if (myLock != nullptr) {
193  myLock->unlock();
194  }
195 }
196 
197 
198 void
200  if (myLock != nullptr) {
201  myLock->lock();
202  }
203  RetrieverVector::iterator i = find(myRetrievers.begin(), myRetrievers.end(), retriever);
204  if (i != myRetrievers.end()) {
205  myRetrievers.erase(i);
206  }
207  if (myLock != nullptr) {
208  myLock->unlock();
209  }
210 }
211 
212 
213 bool
215  return find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
216 }
217 
218 
219 void
221  if (myDebugInstance != nullptr) {
223  }
224  if (myGLDebugInstance != nullptr) {
226  }
227  if (myErrorInstance != nullptr) {
229  }
230  if (myWarningInstance != nullptr) {
232  }
233  if (myMessageInstance != nullptr) {
235  }
236 }
237 
238 void
240  // initialize console properly
241  OutputDevice::getDevice("stdout");
242  OutputDevice::getDevice("stderr");
244  if (oc.getBool("no-warnings")) {
246  }
247  // build the logger if possible
248  if (oc.isSet("log", false)) {
249  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
250  getErrorInstance()->addRetriever(logFile);
251  if (!oc.getBool("no-warnings")) {
252  getWarningInstance()->addRetriever(logFile);
253  }
254  getMessageInstance()->addRetriever(logFile);
255  }
256  if (oc.isSet("message-log", false)) {
257  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
258  getMessageInstance()->addRetriever(logFile);
259  }
260  if (oc.isSet("error-log", false)) {
261  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
262  getErrorInstance()->addRetriever(logFile);
263  getWarningInstance()->addRetriever(logFile);
264  }
265  if (!oc.getBool("verbose")) {
267  }
268 }
269 
270 
271 void
273  if (myLock != nullptr) {
274  myLock->lock();
275  }
276  delete myMessageInstance;
277  myMessageInstance = nullptr;
278  delete myWarningInstance;
279  myWarningInstance = nullptr;
280  delete myErrorInstance;
281  myErrorInstance = nullptr;
282  delete myDebugInstance;
283  myDebugInstance = nullptr;
284  delete myGLDebugInstance;
285  myGLDebugInstance = nullptr;
286  if (myLock != nullptr) {
287  myLock->unlock();
288  }
289 }
290 
291 
293  myType(type), myWasInformed(false) {
294  if (type == MT_MESSAGE) {
296  } else {
298  }
299 }
300 
301 
303 }
304 
305 
306 bool
308  return myWasInformed;
309 }
310 
311 
312 void
314  assert(myLock == 0);
315  myLock = lock;
316 }
317 
318 /****************************************************************************/
319 
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:67
The message is only something to show.
Definition: MsgHandler.h:55
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:231
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
MsgType myType
The type of the instance.
Definition: MsgHandler.h:208
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:214
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:292
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:94
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:186
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:195
An abstract class for encapsulating mutex implementations.
Definition: AbstractMutex.h:42
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:185
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static void assignLock(AbstractMutex *lock)
Sets the lock to use The lock will not be deleted.
Definition: MsgHandler.cpp:313
RetrieverVector myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:217
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:307
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance ...
Definition: MsgHandler.cpp:220
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:230
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:272
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:189
~MsgHandler()
destructor
Definition: MsgHandler.cpp:302
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:85
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
virtual void lock()=0
Locks the mutex.
static AbstractMutex * myLock
The lock if any has to be used. The lock will not be deleted.
Definition: MsgHandler.h:204
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:199
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:58
void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:136
The message is a warning.
Definition: MsgHandler.h:57
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:198
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:201
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:113
A storage for options typed value containers)
Definition: OptionsCont.h:92
virtual void unlock()=0
Unlocks the mutex.
The message is an debug.
Definition: MsgHandler.h:61
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:153
bool myWasInformed
information wehther an error occurred at all
Definition: MsgHandler.h:211
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:108
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:103
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:192
void clear()
Clears information whether an error occurred previously.
Definition: MsgHandler.cpp:173
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:239
The message is an debug.
Definition: MsgHandler.h:63
The message is an error.
Definition: MsgHandler.h:59
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:155