SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // static member variables
50 // ===========================================================================
56 
57 
58 // ===========================================================================
59 // method definitions
60 // ===========================================================================
63  if (myMessageInstance == 0) {
65  }
66  return myMessageInstance;
67 }
68 
69 
72  if (myWarningInstance == 0) {
74  }
75  return myWarningInstance;
76 }
77 
78 
81  if (myErrorInstance == 0) {
83  }
84  return myErrorInstance;
85 }
86 
87 
88 void
89 MsgHandler::inform(std::string msg, bool addType) {
90  if (myLock != 0) {
91  myLock->lock();
92  }
93  // beautify progress output
95  myAmProcessingProcess = false;
97  }
98  msg = build(msg, addType);
99  // inform all receivers
100  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
101  (*i)->inform(msg);
102  }
103  // set the information that something occured
104  myWasInformed = true;
105  if (myLock != 0) {
106  myLock->unlock();
107  }
108 }
109 
110 
111 void
112 MsgHandler::beginProcessMsg(std::string msg, bool addType) {
113  if (myLock != 0) {
114  myLock->lock();
115  }
116  msg = build(msg, addType);
117  // inform all other receivers
118  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
119  (*i)->inform(msg, ' ');
120  myAmProcessingProcess = true;
121  }
122  // set the information that something occured
123  myWasInformed = true;
124  if (myLock != 0) {
125  myLock->unlock();
126  }
127 }
128 
129 
130 void
131 MsgHandler::endProcessMsg(std::string msg) {
132  if (myLock != 0) {
133  myLock->lock();
134  }
135  // inform all other receivers
136  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
137  (*i)->inform(msg);
138  }
139  // set the information that something occured
140  myWasInformed = true;
141  myAmProcessingProcess = false;
142  if (myLock != 0) {
143  myLock->unlock();
144  }
145 }
146 
147 
148 void
150  if (myLock != 0) {
151  myLock->lock();
152  }
153  myWasInformed = false;
154  if (myLock != 0) {
155  myLock->unlock();
156  }
157 }
158 
159 
160 void
162  if (myLock != 0) {
163  myLock->lock();
164  }
165  if (!isRetriever(retriever)) {
166  myRetrievers.push_back(retriever);
167  }
168  if (myLock != 0) {
169  myLock->unlock();
170  }
171 }
172 
173 
174 void
176  if (myLock != 0) {
177  myLock->lock();
178  }
179  RetrieverVector::iterator i =
180  find(myRetrievers.begin(), myRetrievers.end(), retriever);
181  if (i != myRetrievers.end()) {
182  myRetrievers.erase(i);
183  }
184  if (myLock != 0) {
185  myLock->unlock();
186  }
187 }
188 
189 
190 bool
192  return find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
193 }
194 
195 
196 void
198  // initialize console properly
199  OutputDevice::getDevice("stdout");
200  OutputDevice::getDevice("stderr");
202  if (oc.getBool("no-warnings")) {
204  }
205  // build the logger if possible
206  if (oc.isSet("log", false)) {
207  try {
208  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
209  getErrorInstance()->addRetriever(logFile);
210  if (!oc.getBool("no-warnings")) {
211  getWarningInstance()->addRetriever(logFile);
212  }
213  getMessageInstance()->addRetriever(logFile);
214  } catch (IOError&) {
215  throw ProcessError("Could not build logging file '" + oc.getString("log") + "'");
216  }
217  }
218  if (oc.isSet("message-log", false)) {
219  try {
220  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
221  getMessageInstance()->addRetriever(logFile);
222  } catch (IOError&) {
223  throw ProcessError("Could not build logging file '" + oc.getString("message-log") + "'");
224  }
225  }
226  if (oc.isSet("error-log", false)) {
227  try {
228  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
229  getErrorInstance()->addRetriever(logFile);
230  getWarningInstance()->addRetriever(logFile);
231  } catch (IOError&) {
232  throw ProcessError("Could not build logging file '" + oc.getString("error-log") + "'");
233  }
234  }
235  if (!oc.getBool("verbose")) {
237  }
238 }
239 
240 
241 void
243  if (myLock != 0) {
244  myLock->lock();
245  }
246  delete myMessageInstance;
247  myMessageInstance = 0;
248  delete myWarningInstance;
249  myWarningInstance = 0;
250  delete myErrorInstance;
251  myErrorInstance = 0;
252  if (myLock != 0) {
253  myLock->unlock();
254  }
255 }
256 
257 
259  : myType(type), myWasInformed(false) {
260  if (type == MT_MESSAGE) {
262  } else {
264  }
265 }
266 
267 
269 }
270 
271 
272 bool
274  return myWasInformed;
275 }
276 
277 
278 void
280  assert(myLock == 0);
281  myLock = lock;
282 }
283 
284 
285 
286 /****************************************************************************/
287 
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:191
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
The message is only something to show.
Definition: MsgHandler.h:62
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:258
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
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:161
static void assignLock(AbstractMutex *lock)
Sets the lock to use The lock will not be deleted.
Definition: MsgHandler.cpp:279
RetrieverVector myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:185
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:242
~MsgHandler()
destructor
Definition: MsgHandler.cpp:268
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:175
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:62
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:273
void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:112
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:89
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:149
static void initOutputOptions()
Definition: MsgHandler.cpp:197
The message is an error.
Definition: MsgHandler.h:66
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:131