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