SUMO - Simulation of Urban MObility
MsgHandler.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2003-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Retrieves messages about the process and gives them further to output
20 /****************************************************************************/
21 #ifndef MsgHandler_h
22 #define MsgHandler_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <vector>
36 #include <iostream>
37 
38 
39 // ===========================================================================
40 // class declarations
41 // ===========================================================================
42 class AbstractMutex;
43 class OutputDevice;
44 
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
52 class MsgHandler {
53 public:
59  enum MsgType {
66  };
67 
70 
73 
75  static MsgHandler* getErrorInstance();
76 
77  static void initOutputOptions();
78 
80  static void cleanupOnEnd();
81 
83  void inform(std::string msg, bool addType = true);
84 
92  void beginProcessMsg(std::string msg, bool addType = true);
93 
95  void endProcessMsg(std::string msg);
96 
98  void clear();
99 
101  void addRetriever(OutputDevice* retriever);
102 
104  void removeRetriever(OutputDevice* retriever);
105 
107  bool isRetriever(OutputDevice* retriever) const;
108 
110  bool wasInformed() const;
111 
114  static void assignLock(AbstractMutex* lock);
115 
119  template <class T>
120  MsgHandler& operator<<(const T& t) {
121  // inform all other receivers
122  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
123  (*(*i)) << t;
124  }
125  return *this;
126  }
127 
128 protected:
130  inline std::string build(const std::string& msg, bool addType) {
131  if (addType) {
132  switch (myType) {
133  case MT_MESSAGE:
134  break;
135  case MT_WARNING:
136  return "Warning: " + msg;
137  break;
138  case MT_ERROR:
139  return "Error: " + msg;
140  break;
141  default:
142  break;
143  }
144  }
145  return msg;
146  }
147 
148 
149 private:
151  MsgHandler(MsgType type);
152 
154  ~MsgHandler();
155 
156 private:
159 
162 
165 
168 
172 
173 private:
176 
179 
181  typedef std::vector<OutputDevice*> RetrieverVector;
182 
184  RetrieverVector myRetrievers;
185 
186 private:
188  MsgHandler(const MsgHandler& s);
189 
191  MsgHandler& operator=(const MsgHandler& s);
192 
193 };
194 
195 
196 // ===========================================================================
197 // global definitions
198 // ===========================================================================
199 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
200 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
201 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string("..."));
202 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
203 #define PROGRESS_TIME_MESSAGE(before) MsgHandler::getMessageInstance()->endProcessMsg("done (" + toString(SysUtils::getCurrentMillis() - before) + "ms).");
204 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
205 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
206 
207 #endif
208 
209 /****************************************************************************/
210 
MsgHandler & operator<<(const T &t)
Generic output operator.
Definition: MsgHandler.h:120
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:66
std::vector< OutputDevice * > RetrieverVector
Definition of the list of retrievers to inform.
Definition: MsgHandler.h:181
The message is only something to show.
Definition: MsgHandler.h:61
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:75
MsgType myType
The type of the instance.
Definition: MsgHandler.h:175
bool isRetriever(OutputDevice *retriever) const
Returns whether the given output device retrieves messages from the handler.
Definition: MsgHandler.cpp:186
MsgHandler(MsgType type)
standard constructor
Definition: MsgHandler.cpp:253
MsgHandler & operator=(const MsgHandler &s)
static MsgHandler * myWarningInstance
The instance to handle warnings.
Definition: MsgHandler.h:161
An abstract class for encapsulating mutex implementations.
Definition: AbstractMutex.h:48
void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:156
static void assignLock(AbstractMutex *lock)
Sets the lock to use The lock will not be deleted.
Definition: MsgHandler.cpp:274
RetrieverVector myRetrievers
The list of retrievers that shall be informed about new messages or errors.
Definition: MsgHandler.h:184
bool wasInformed() const
Returns the information whether any messages were added.
Definition: MsgHandler.cpp:268
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:237
~MsgHandler()
destructor
Definition: MsgHandler.cpp:263
static AbstractMutex * myLock
The lock if any has to be used The lock will not be deleted.
Definition: MsgHandler.h:171
void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:170
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:57
void beginProcessMsg(std::string msg, bool addType=true)
Begins a process information.
Definition: MsgHandler.cpp:107
The message is a warning.
Definition: MsgHandler.h:63
static MsgHandler * myMessageInstance
The instance to handle normal messages.
Definition: MsgHandler.h:164
static bool myAmProcessingProcess
Information whether a process information is printed to cout.
Definition: MsgHandler.h:167
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:84
std::string build(const std::string &msg, bool addType)
Builds the string which includes the mml-message type.
Definition: MsgHandler.h:130
bool myWasInformed
information wehther an error occured at all
Definition: MsgHandler.h:178
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:158
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:144
static void initOutputOptions()
Definition: MsgHandler.cpp:192
The message is an error.
Definition: MsgHandler.h:65
void endProcessMsg(std::string msg)
Ends a process information.
Definition: MsgHandler.cpp:126