SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MsgHandler.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Retrieves messages about the process and gives them further to output
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2003-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef MsgHandler_h
23 #define MsgHandler_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
36 #include <vector>
37 #include <iostream>
38 
39 
40 // ===========================================================================
41 // class declarations
42 // ===========================================================================
43 class AbstractMutex;
44 class OutputDevice;
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
53 class MsgHandler {
54 public:
60  enum MsgType {
67  };
68 
71 
74 
76  static MsgHandler* getErrorInstance();
77 
78  static void initOutputOptions();
79 
81  static void cleanupOnEnd();
82 
84  void inform(std::string msg, bool addType = true);
85 
93  void beginProcessMsg(std::string msg, bool addType = true);
94 
96  void endProcessMsg(std::string msg);
97 
99  void clear();
100 
102  void addRetriever(OutputDevice* retriever);
103 
105  void removeRetriever(OutputDevice* retriever);
106 
108  bool isRetriever(OutputDevice* retriever) const;
109 
111  bool wasInformed() const;
112 
115  static void assignLock(AbstractMutex* lock);
116 
120  template <class T>
121  MsgHandler& operator<<(const T& t) {
122  // inform all other receivers
123  for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
124  (*(*i)) << t;
125  }
126  return *this;
127  }
128 
129 protected:
131  inline std::string build(const std::string& msg, bool addType) {
132  if (addType) {
133  switch (myType) {
134  case MT_MESSAGE:
135  break;
136  case MT_WARNING:
137  return "Warning: " + msg;
138  break;
139  case MT_ERROR:
140  return "Error: " + msg;
141  break;
142  default:
143  break;
144  }
145  }
146  return msg;
147  }
148 
149 
150 private:
152  MsgHandler(MsgType type);
153 
155  ~MsgHandler();
156 
157 private:
160 
163 
166 
169 
173 
174 private:
177 
180 
182  typedef std::vector<OutputDevice*> RetrieverVector;
183 
186 
187 private:
189  MsgHandler(const MsgHandler& s);
190 
192  MsgHandler& operator=(const MsgHandler& s);
193 
194 };
195 
196 
197 // ===========================================================================
198 // global definitions
199 // ===========================================================================
200 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
201 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
202 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg((msg) + std::string("..."));
203 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
204 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
205 #define WRITE_ERROR(msg) MsgHandler::getErrorInstance()->inform(msg);
206 
207 #endif
208 
209 /****************************************************************************/
210