00001 /****************************************************************************/ 00007 // Retrieves messages about the process and gives them further to output 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This program is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 /****************************************************************************/ 00019 #ifndef MsgHandler_h 00020 #define MsgHandler_h 00021 00022 00023 // =========================================================================== 00024 // included modules 00025 // =========================================================================== 00026 #ifdef _MSC_VER 00027 #include <windows_config.h> 00028 #else 00029 #include <config.h> 00030 #endif 00031 00032 #include <string> 00033 #include <vector> 00034 #include <iostream> 00035 00036 00037 // =========================================================================== 00038 // class declarations 00039 // =========================================================================== 00040 class AbstractMutex; 00041 class OutputDevice; 00042 00043 00044 // =========================================================================== 00045 // global variable definitions 00046 // =========================================================================== 00047 extern bool gSuppressWarnings; 00048 extern bool gSuppressMessages; 00049 00050 00051 // =========================================================================== 00052 // class definitions 00053 // =========================================================================== 00057 class MsgHandler { 00058 public: 00064 enum MsgType { 00066 MT_MESSAGE, 00068 MT_WARNING, 00070 MT_ERROR 00071 }; 00072 00074 static MsgHandler *getMessageInstance(); 00075 00077 static MsgHandler *getWarningInstance(); 00078 00080 static MsgHandler *getErrorInstance(); 00081 00082 static void initOutputOptions(bool gui=false); 00083 00085 static void cleanupOnEnd(); 00086 00088 void inform(std::string msg, bool addType=true); 00089 00097 void beginProcessMsg(std::string msg, bool addType=true); 00098 00100 void endProcessMsg(std::string msg); 00101 00106 void progressMsg(std::string msg, bool addType=true); 00107 00109 void clear(); 00110 00112 void addRetriever(OutputDevice *retriever); 00113 00115 void removeRetriever(OutputDevice *retriever); 00116 00118 void report2cout(bool value); 00119 00121 void report2cerr(bool value); 00122 00124 bool wasInformed() const; 00125 00128 static void assignLock(AbstractMutex *lock); 00129 00133 template <class T> 00134 MsgHandler &operator<<(const T &t) { 00135 if (myReport2COUT) { 00136 std::cout << t; 00137 } 00138 // report to cerr if wished 00139 if (myReport2CERR) { 00140 std::cerr << t; 00141 } 00142 // inform all other receivers 00143 for (RetrieverVector::iterator i=myRetrievers.begin(); i!=myRetrievers.end(); i++) { 00144 (*(*i)) << t; 00145 } 00146 return *this; 00147 } 00148 00149 protected: 00151 inline std::string build(const std::string &msg, bool addType) { 00152 if (addType) { 00153 switch (myType) { 00154 case MT_MESSAGE: 00155 break; 00156 case MT_WARNING: 00157 return "Warning: " + msg; 00158 break; 00159 case MT_ERROR: 00160 return "Error: " + msg; 00161 break; 00162 default: 00163 break; 00164 } 00165 } 00166 return msg; 00167 } 00168 00169 00170 private: 00172 MsgHandler(MsgType type); 00173 00175 ~MsgHandler(); 00176 00177 private: 00179 static MsgHandler *myErrorInstance; 00180 00182 static MsgHandler *myWarningInstance; 00183 00185 static MsgHandler *myMessageInstance; 00186 00188 static bool myAmProcessingProcess; 00189 00192 static AbstractMutex *myLock; 00193 00194 private: 00196 MsgType myType; 00197 00199 bool myWasInformed; 00200 00202 bool myReport2COUT; 00203 00205 bool myReport2CERR; 00206 00208 typedef std::vector<OutputDevice*> RetrieverVector; 00209 00211 RetrieverVector myRetrievers; 00212 00213 private: 00215 MsgHandler(const MsgHandler &s); 00216 00218 MsgHandler &operator=(const MsgHandler &s); 00219 00220 }; 00221 00222 00223 // =========================================================================== 00224 // global definitions 00225 // =========================================================================== 00226 #define WRITE_WARNING(command) if(!gSuppressWarnings) { MsgHandler::getWarningInstance()->inform(command); } 00227 #define WRITE_MESSAGE(command) if(!gSuppressMessages) { MsgHandler::getMessageInstance()->inform(command); } 00228 #define WRITE_ERROR(command) MsgHandler::getErrorInstance()->inform(command); 00229 #define MSG_OUT (*MsgHandler::getMessageInstance()) 00230 00231 #endif 00232 00233 /****************************************************************************/ 00234
1.5.6