SUMO - Simulation of Urban MObility
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
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-2014 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
"
38
#include <
utils/options/OptionsCont.h
>
39
#include <
utils/iodevices/OutputDevice.h
>
40
#include <
utils/common/UtilExceptions.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
// ===========================================================================
51
MsgHandler
*
MsgHandler::myErrorInstance
= 0;
52
MsgHandler
*
MsgHandler::myWarningInstance
= 0;
53
MsgHandler
*
MsgHandler::myMessageInstance
= 0;
54
bool
MsgHandler::myAmProcessingProcess
=
false
;
55
AbstractMutex
*
MsgHandler::myLock
= 0;
56
57
58
// ===========================================================================
59
// method definitions
60
// ===========================================================================
61
MsgHandler
*
62
MsgHandler::getMessageInstance
() {
63
if
(
myMessageInstance
== 0) {
64
myMessageInstance
=
new
MsgHandler
(
MT_MESSAGE
);
65
}
66
return
myMessageInstance
;
67
}
68
69
70
MsgHandler
*
71
MsgHandler::getWarningInstance
() {
72
if
(
myWarningInstance
== 0) {
73
myWarningInstance
=
new
MsgHandler
(
MT_WARNING
);
74
}
75
return
myWarningInstance
;
76
}
77
78
79
MsgHandler
*
80
MsgHandler::getErrorInstance
() {
81
if
(
myErrorInstance
== 0) {
82
myErrorInstance
=
new
MsgHandler
(
MT_ERROR
);
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
94
if
(
myAmProcessingProcess
) {
95
myAmProcessingProcess
=
false
;
96
MsgHandler::getMessageInstance
()->
inform
(
""
);
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
149
MsgHandler::clear
() {
150
if
(
myLock
!= 0) {
151
myLock
->
lock
();
152
}
153
myWasInformed
=
false
;
154
if
(
myLock
!= 0) {
155
myLock
->
unlock
();
156
}
157
}
158
159
160
void
161
MsgHandler::addRetriever
(
OutputDevice
* retriever) {
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
175
MsgHandler::removeRetriever
(
OutputDevice
* retriever) {
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
191
MsgHandler::isRetriever
(
OutputDevice
* retriever)
const
{
192
return
find(
myRetrievers
.begin(),
myRetrievers
.end(), retriever) !=
myRetrievers
.end();
193
}
194
195
196
void
197
MsgHandler::initOutputOptions
() {
198
// initialize console properly
199
OutputDevice::getDevice
(
"stdout"
);
200
OutputDevice::getDevice
(
"stderr"
);
201
OptionsCont
& oc =
OptionsCont::getOptions
();
202
if
(oc.
getBool
(
"no-warnings"
)) {
203
getWarningInstance
()->
removeRetriever
(&
OutputDevice::getDevice
(
"stderr"
));
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"
)) {
236
getMessageInstance
()->
removeRetriever
(&
OutputDevice::getDevice
(
"stdout"
));
237
}
238
}
239
240
241
void
242
MsgHandler::cleanupOnEnd
() {
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
258
MsgHandler::MsgHandler
(
MsgType
type)
259
: myType(type), myWasInformed(false) {
260
if
(type ==
MT_MESSAGE
) {
261
addRetriever
(&
OutputDevice::getDevice
(
"stdout"
));
262
}
else
{
263
addRetriever
(&
OutputDevice::getDevice
(
"stderr"
));
264
}
265
}
266
267
268
MsgHandler::~MsgHandler
() {
269
}
270
271
272
bool
273
MsgHandler::wasInformed
()
const
{
274
return
myWasInformed
;
275
}
276
277
278
void
279
MsgHandler::assignLock
(
AbstractMutex
* lock) {
280
assert(
myLock
== 0);
281
myLock
= lock;
282
}
283
284
285
286
/****************************************************************************/
287
tmp
buildd
sumo-0.21.0+dfsg
src
utils
common
MsgHandler.cpp
Generated on Thu Nov 20 2014 19:49:55 for SUMO - Simulation of Urban MObility by
1.8.1.2