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.sourceforge.net/
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("verbose")) {
200  }
201  if (oc.getBool("no-warnings")) {
203  }
204  // build the logger if possible
205  if (oc.isSet("log", false)) {
206  try {
207  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
208  getErrorInstance()->addRetriever(logFile);
209  if (!oc.getBool("no-warnings")) {
210  getWarningInstance()->addRetriever(logFile);
211  }
212  getMessageInstance()->addRetriever(logFile);
213  } catch (IOError&) {
214  throw ProcessError("Could not build logging file '" + oc.getString("log") + "'");
215  }
216  }
217  if (oc.isSet("message-log", false)) {
218  try {
219  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
220  getMessageInstance()->addRetriever(logFile);
221  } catch (IOError&) {
222  throw ProcessError("Could not build logging file '" + oc.getString("message-log") + "'");
223  }
224  }
225  if (oc.isSet("error-log", false)) {
226  try {
227  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
228  getErrorInstance()->addRetriever(logFile);
229  getWarningInstance()->addRetriever(logFile);
230  } catch (IOError&) {
231  throw ProcessError("Could not build logging file '" + oc.getString("error-log") + "'");
232  }
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