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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Retrieves messages about the process and gives them further to output
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <cassert>
33 #include <vector>
34 #include <algorithm>
35 #include <iostream>
36 #include "MsgHandler.h"
40 #include "AbstractMutex.h"
41 
42 
43 // ===========================================================================
44 // static member variables
45 // ===========================================================================
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
58  if (myMessageInstance == 0) {
60  }
61  return myMessageInstance;
62 }
63 
64 
67  if (myWarningInstance == 0) {
69  }
70  return myWarningInstance;
71 }
72 
73 
76  if (myErrorInstance == 0) {
78  }
79  return myErrorInstance;
80 }
81 
82 
83 void
84 MsgHandler::inform(std::string msg, bool addType) {
85  if (myLock != 0) {
86  myLock->lock();
87  }
88  // beautify progress output
90  myAmProcessingProcess = false;
92  }
93  msg = build(msg, addType);
94  // inform all receivers
95  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
96  (*i)->inform(msg);
97  }
98  // set the information that something occured
99  myWasInformed = true;
100  if (myLock != 0) {
101  myLock->unlock();
102  }
103 }
104 
105 
106 void
107 MsgHandler::beginProcessMsg(std::string msg, bool addType) {
108  if (myLock != 0) {
109  myLock->lock();
110  }
111  msg = build(msg, addType);
112  // inform all other receivers
113  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
114  (*i)->inform(msg, ' ');
115  myAmProcessingProcess = true;
116  }
117  // set the information that something occured
118  myWasInformed = true;
119  if (myLock != 0) {
120  myLock->unlock();
121  }
122 }
123 
124 
125 void
126 MsgHandler::endProcessMsg(std::string msg) {
127  if (myLock != 0) {
128  myLock->lock();
129  }
130  // inform all other receivers
131  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
132  (*i)->inform(msg);
133  }
134  // set the information that something occured
135  myWasInformed = true;
136  myAmProcessingProcess = false;
137  if (myLock != 0) {
138  myLock->unlock();
139  }
140 }
141 
142 
143 void
145  if (myLock != 0) {
146  myLock->lock();
147  }
148  myWasInformed = false;
149  if (myLock != 0) {
150  myLock->unlock();
151  }
152 }
153 
154 
155 void
157  if (myLock != 0) {
158  myLock->lock();
159  }
160  if (!isRetriever(retriever)) {
161  myRetrievers.push_back(retriever);
162  }
163  if (myLock != 0) {
164  myLock->unlock();
165  }
166 }
167 
168 
169 void
171  if (myLock != 0) {
172  myLock->lock();
173  }
174  RetrieverVector::iterator i =
175  find(myRetrievers.begin(), myRetrievers.end(), retriever);
176  if (i != myRetrievers.end()) {
177  myRetrievers.erase(i);
178  }
179  if (myLock != 0) {
180  myLock->unlock();
181  }
182 }
183 
184 
185 bool
187  return find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
188 }
189 
190 
191 void
193  // initialize console properly
194  OutputDevice::getDevice("stdout");
195  OutputDevice::getDevice("stderr");
197  if (oc.getBool("no-warnings")) {
199  }
200  // build the logger if possible
201  if (oc.isSet("log", false)) {
202  try {
203  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
204  getErrorInstance()->addRetriever(logFile);
205  if (!oc.getBool("no-warnings")) {
206  getWarningInstance()->addRetriever(logFile);
207  }
208  getMessageInstance()->addRetriever(logFile);
209  } catch (IOError&) {
210  throw ProcessError("Could not build logging file '" + oc.getString("log") + "'");
211  }
212  }
213  if (oc.isSet("message-log", false)) {
214  try {
215  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
216  getMessageInstance()->addRetriever(logFile);
217  } catch (IOError&) {
218  throw ProcessError("Could not build logging file '" + oc.getString("message-log") + "'");
219  }
220  }
221  if (oc.isSet("error-log", false)) {
222  try {
223  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
224  getErrorInstance()->addRetriever(logFile);
225  getWarningInstance()->addRetriever(logFile);
226  } catch (IOError&) {
227  throw ProcessError("Could not build logging file '" + oc.getString("error-log") + "'");
228  }
229  }
230  if (!oc.getBool("verbose")) {
232  }
233 }
234 
235 
236 void
238  if (myLock != 0) {
239  myLock->lock();
240  }
241  delete myMessageInstance;
242  myMessageInstance = 0;
243  delete myWarningInstance;
244  myWarningInstance = 0;
245  delete myErrorInstance;
246  myErrorInstance = 0;
247  if (myLock != 0) {
248  myLock->unlock();
249  }
250 }
251 
252 
254  : myType(type), myWasInformed(false) {
255  if (type == MT_MESSAGE) {
257  } else {
259  }
260 }
261 
262 
264 }
265 
266 
267 bool
269  return myWasInformed;
270 }
271 
272 
273 void
275  assert(myLock == 0);
276  myLock = lock;
277 }
278 
279 
280 
281 /****************************************************************************/
282 
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:66
The message is only something to show.
Definition: MsgHandler.h:61
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:75
MsgType myType
The type of the instance.
Definition: MsgHandler.h:175
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:186
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:253
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:161
An abstract class for encapsulating mutex implementations.
Definition: AbstractMutex.h:48
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:156
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:274
RetrieverVector myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:184
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:268
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:237
~MsgHandler()
destructor
Definition: MsgHandler.cpp:263
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:171
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:170
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:57
void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:107
The message is a warning.
Definition: MsgHandler.h:63
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:164
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:167
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:84
A storage for options typed value containers)
Definition: OptionsCont.h:98
virtual void unlock()=0
Unlocks the mutex.
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:130
bool myWasInformed
information wehther an error occured at all
Definition: MsgHandler.h:178
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:158
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:144
static void initOutputOptions()
Definition: MsgHandler.cpp:192
The message is an error.
Definition: MsgHandler.h:65
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:126