SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-sim.org/
11 // Copyright (C) 2001-2013 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  RetrieverVector::iterator i =
166  find(myRetrievers.begin(), myRetrievers.end(), retriever);
167  if (i == myRetrievers.end()) {
168  myRetrievers.push_back(retriever);
169  }
170  if (myLock != 0) {
171  myLock->unlock();
172  }
173 }
174 
175 
176 void
178  if (myLock != 0) {
179  myLock->lock();
180  }
181  RetrieverVector::iterator i =
182  find(myRetrievers.begin(), myRetrievers.end(), retriever);
183  if (i != myRetrievers.end()) {
184  myRetrievers.erase(i);
185  }
186  if (myLock != 0) {
187  myLock->unlock();
188  }
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:71
The message is only something to show.
Definition: MsgHandler.h:61
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:254
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:158
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:275
RetrieverVector myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:181
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
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:238
~MsgHandler()
destructor
Definition: MsgHandler.cpp:264
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:168
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the.
Definition: MsgHandler.cpp:177
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:269
void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:112
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:161
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:164
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:108
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:127
bool myWasInformed
information wehther an error occured at all
Definition: MsgHandler.h:175
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:155
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:149
static void initOutputOptions()
Definition: MsgHandler.cpp:193
The message is an error.
Definition: MsgHandler.h:65
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