Eclipse SUMO - Simulation of Urban MObility
MsgHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Retrieves messages about the process and gives them further to output
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <cassert>
27 #include <vector>
28 #include <algorithm>
29 #include <iostream>
30 #ifdef HAVE_FOX
31 #include <fx.h>
32 #endif
36 #include "MsgHandler.h"
37 
38 
39 // ===========================================================================
40 // static member variables
41 // ===========================================================================
42 
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 
60  if (myMessageInstance == nullptr) {
61  if (myFactory == nullptr) {
63  } else {
65  }
66  }
67  return myMessageInstance;
68 }
69 
70 
73  if (myWarningInstance == nullptr) {
75  }
76  return myWarningInstance;
77 }
78 
79 
82  if (myErrorInstance == nullptr) {
84  }
85  return myErrorInstance;
86 }
87 
88 
91  if (myDebugInstance == nullptr) {
93  }
94  return myDebugInstance;
95 }
96 
97 
100  if (myGLDebugInstance == nullptr) {
102  }
103  return myGLDebugInstance;
104 }
105 
106 
107 void
109  myWriteDebugMessages = enable;
110 }
111 
112 void
114  myWriteDebugGLMessages = enable;
115 }
116 
117 void
118 MsgHandler::inform(std::string msg, bool addType) {
119  // beautify progress output
120  if (myAmProcessingProcess) {
121  myAmProcessingProcess = false;
123  }
124  msg = build(msg, addType);
125  // inform all receivers
126  for (auto i : myRetrievers) {
127  i->inform(msg);
128  }
129  // set the information that something occurred
130  myWasInformed = true;
131 }
132 
133 
134 void
135 MsgHandler::beginProcessMsg(std::string msg, bool addType) {
136  msg = build(msg, addType);
137  // inform all other receivers
138  for (auto i : myRetrievers) {
139  i->inform(msg, ' ');
140  myAmProcessingProcess = true;
141  }
142  // set the information that something occurred
143  myWasInformed = true;
144 }
145 
146 
147 void
148 MsgHandler::endProcessMsg(std::string msg) {
149  // inform all other receivers
150  for (auto i : myRetrievers) {
151  i->inform(msg);
152  }
153  // set the information that something occurred
154  myWasInformed = true;
155  myAmProcessingProcess = false;
156 }
157 
158 
159 void
161  myWasInformed = false;
162 }
163 
164 
165 void
167  if (!isRetriever(retriever)) {
168  myRetrievers.push_back(retriever);
169  }
170 }
171 
172 
173 void
175  std::vector<OutputDevice*>::iterator i = find(myRetrievers.begin(), myRetrievers.end(), retriever);
176  if (i != myRetrievers.end()) {
177  myRetrievers.erase(i);
178  }
179 }
180 
181 
182 bool
184  return std::find(myRetrievers.begin(), myRetrievers.end(), retriever) != myRetrievers.end();
185 }
186 
187 
188 void
190  if (myDebugInstance != nullptr) {
192  }
193  if (myGLDebugInstance != nullptr) {
195  }
196  if (myErrorInstance != nullptr) {
198  }
199  if (myWarningInstance != nullptr) {
201  }
202  if (myMessageInstance != nullptr) {
204  }
205 }
206 
207 void
209  // initialize console properly
210  OutputDevice::getDevice("stdout");
211  OutputDevice::getDevice("stderr");
213  if (oc.getBool("no-warnings")) {
215  }
216  // build the logger if possible
217  if (oc.isSet("log", false)) {
218  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("log"));
219  getErrorInstance()->addRetriever(logFile);
220  if (!oc.getBool("no-warnings")) {
221  getWarningInstance()->addRetriever(logFile);
222  }
223  getMessageInstance()->addRetriever(logFile);
224  }
225  if (oc.isSet("message-log", false)) {
226  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("message-log"));
227  getMessageInstance()->addRetriever(logFile);
228  }
229  if (oc.isSet("error-log", false)) {
230  OutputDevice* logFile = &OutputDevice::getDevice(oc.getString("error-log"));
231  getErrorInstance()->addRetriever(logFile);
232  getWarningInstance()->addRetriever(logFile);
233  }
234  if (!oc.getBool("verbose")) {
236  }
237 }
238 
239 
240 void
242  delete myMessageInstance;
243  myMessageInstance = nullptr;
244  delete myWarningInstance;
245  myWarningInstance = nullptr;
246  delete myErrorInstance;
247  myErrorInstance = nullptr;
248  delete myDebugInstance;
249  myDebugInstance = nullptr;
250  delete myGLDebugInstance;
251  myGLDebugInstance = nullptr;
252 }
253 
254 
256  myType(type), myWasInformed(false) {
257  if (type == MT_MESSAGE) {
259  } else {
261  }
262 }
263 
264 
266 }
267 
268 
269 bool
271  return myWasInformed;
272 }
273 
274 
275 /****************************************************************************/
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:136
MsgHandler::MsgType
MsgType
Definition: MsgHandler.h:51
MsgHandler::MT_ERROR
The message is an error.
Definition: MsgHandler.h:57
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:208
MsgHandler::wasInformed
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:270
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
MsgHandler::removeRetrieverFromAllInstances
static void removeRetrieverFromAllInstances(OutputDevice *out)
ensure that that given output device is no longer used as retriever by any instance
Definition: MsgHandler.cpp:189
OptionsCont.h
MsgHandler::MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:53
MsgHandler.h
MsgHandler::MT_WARNING
The message is a warning.
Definition: MsgHandler.h:55
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:202
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
MsgHandler::isRetriever
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:183
MsgHandler::beginProcessMsg
virtual void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:135
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:223
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
MsgHandler::build
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:157
MsgHandler::myWasInformed
bool myWasInformed
information wehther an error occurred at all
Definition: MsgHandler.h:215
MsgHandler::cleanupOnEnd
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:241
MsgHandler::myErrorInstance
static MsgHandler * myErrorInstance
The instance to handle errors.
Definition: MsgHandler.h:199
MsgHandler::myAmProcessingProcess
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:208
MsgHandler::clear
virtual void clear()
Clears information whether an error occurred previously.
Definition: MsgHandler.cpp:160
MsgHandler::enableDebugGLMessages
static void enableDebugGLMessages(bool enable)
enable/disable gl-debug messages
Definition: MsgHandler.cpp:113
MsgHandler::Factory
MsgHandler *(* Factory)(MsgType)
Definition: MsgHandler.h:65
MsgHandler::myWarningInstance
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:202
MsgHandler
Definition: MsgHandler.h:44
MsgHandler::getWarningInstance
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:72
MsgHandler::endProcessMsg
virtual void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:148
OutputDevice.h
MsgHandler::MsgHandler
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:255
MsgHandler::myWriteDebugGLMessages
static bool myWriteDebugGLMessages
Definition: MsgHandler.h:232
MsgHandler::getGLDebugInstance
static MsgHandler * getGLDebugInstance()
Returns the instance to add GLdebug to.
Definition: MsgHandler.cpp:99
MsgHandler::myFactory
static Factory myFactory
The function to call for new MsgHandlers, nullptr means use default constructor.
Definition: MsgHandler.h:190
UtilExceptions.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
MsgHandler::myMessageInstance
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:205
OutputDevice::getDevice
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Definition: OutputDevice.cpp:55
MsgHandler::myDebugInstance
static MsgHandler * myDebugInstance
The instance to handle debug.
Definition: MsgHandler.h:193
MsgHandler::getDebugInstance
static MsgHandler * getDebugInstance()
Returns the instance to add debug to.
Definition: MsgHandler.cpp:90
MsgHandler::MT_DEBUG
The message is an debug.
Definition: MsgHandler.h:59
MsgHandler::addRetriever
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:166
config.h
MsgHandler::myRetrievers
std::vector< OutputDevice * > myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:218
MsgHandler::MT_GLDEBUG
The message is an debug.
Definition: MsgHandler.h:61
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
MsgHandler::myGLDebugInstance
static MsgHandler * myGLDebugInstance
The instance to handle glDebug.
Definition: MsgHandler.h:196
MsgHandler::myWriteDebugMessages
static bool myWriteDebugMessages
Flag to enable or disable debug GL Functions.
Definition: MsgHandler.h:231
MsgHandler::~MsgHandler
virtual ~MsgHandler()
destructor
Definition: MsgHandler.cpp:265
MsgHandler::removeRetriever
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:174
MsgHandler::enableDebugMessages
static void enableDebugMessages(bool enable)
enable/disable debug messages
Definition: MsgHandler.cpp:108
MsgHandler::getMessageInstance
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:59