SUMO - Simulation of Urban MObility

MsgHandler.h

Go to the documentation of this file.
00001 /****************************************************************************/
00008 // Retrieves messages about the process and gives them further to output
00009 /****************************************************************************/
00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00012 /****************************************************************************/
00013 //
00014 //   This file is part of SUMO.
00015 //   SUMO is free software: you can redistribute it and/or modify
00016 //   it under the terms of the GNU General Public License as published by
00017 //   the Free Software Foundation, either version 3 of the License, or
00018 //   (at your option) any later version.
00019 //
00020 /****************************************************************************/
00021 #ifndef MsgHandler_h
00022 #define MsgHandler_h
00023 
00024 
00025 // ===========================================================================
00026 // included modules
00027 // ===========================================================================
00028 #ifdef _MSC_VER
00029 #include <windows_config.h>
00030 #else
00031 #include <config.h>
00032 #endif
00033 
00034 #include <string>
00035 #include <vector>
00036 #include <iostream>
00037 
00038 
00039 // ===========================================================================
00040 // class declarations
00041 // ===========================================================================
00042 class AbstractMutex;
00043 class OutputDevice;
00044 
00045 
00046 // ===========================================================================
00047 // global variable definitions
00048 // ===========================================================================
00049 extern bool gSuppressWarnings;
00050 extern bool gSuppressMessages;
00051 
00052 
00053 // ===========================================================================
00054 // class definitions
00055 // ===========================================================================
00059 class MsgHandler {
00060 public:
00066     enum MsgType {
00068         MT_MESSAGE,
00070         MT_WARNING,
00072         MT_ERROR
00073     };
00074 
00076     static MsgHandler* getMessageInstance();
00077 
00079     static MsgHandler* getWarningInstance();
00080 
00082     static MsgHandler* getErrorInstance();
00083 
00084     static void initOutputOptions(bool gui = false);
00085 
00087     static void cleanupOnEnd();
00088 
00090     void inform(std::string msg, bool addType = true);
00091 
00099     void beginProcessMsg(std::string msg, bool addType = true);
00100 
00102     void endProcessMsg(std::string msg);
00103 
00108     void progressMsg(std::string msg, bool addType = true);
00109 
00111     void clear();
00112 
00114     void addRetriever(OutputDevice* retriever);
00115 
00117     void removeRetriever(OutputDevice* retriever);
00118 
00120     void report2cout(bool value);
00121 
00123     void report2cerr(bool value);
00124 
00126     bool wasInformed() const;
00127 
00130     static void assignLock(AbstractMutex* lock);
00131 
00135     template <class T>
00136     MsgHandler& operator<<(const T& t) {
00137         if (myReport2COUT) {
00138             std::cout << t;
00139         }
00140         // report to cerr if wished
00141         if (myReport2CERR) {
00142             std::cerr << t;
00143         }
00144         // inform all other receivers
00145         for (RetrieverVector::iterator i = myRetrievers.begin(); i != myRetrievers.end(); i++) {
00146             (*(*i)) << t;
00147         }
00148         return *this;
00149     }
00150 
00151 protected:
00153     inline std::string build(const std::string& msg, bool addType) {
00154         if (addType) {
00155             switch (myType) {
00156                 case MT_MESSAGE:
00157                     break;
00158                 case MT_WARNING:
00159                     return "Warning: " + msg;
00160                     break;
00161                 case MT_ERROR:
00162                     return "Error: " + msg;
00163                     break;
00164                 default:
00165                     break;
00166             }
00167         }
00168         return msg;
00169     }
00170 
00171 
00172 private:
00174     MsgHandler(MsgType type);
00175 
00177     ~MsgHandler();
00178 
00179 private:
00181     static MsgHandler* myErrorInstance;
00182 
00184     static MsgHandler* myWarningInstance;
00185 
00187     static MsgHandler* myMessageInstance;
00188 
00190     static bool myAmProcessingProcess;
00191 
00194     static AbstractMutex* myLock;
00195 
00196 private:
00198     MsgType myType;
00199 
00201     bool myWasInformed;
00202 
00204     bool myReport2COUT;
00205 
00207     bool myReport2CERR;
00208 
00210     typedef std::vector<OutputDevice*> RetrieverVector;
00211 
00213     RetrieverVector myRetrievers;
00214 
00215 private:
00217     MsgHandler(const MsgHandler& s);
00218 
00220     MsgHandler& operator=(const MsgHandler& s);
00221 
00222 };
00223 
00224 
00225 // ===========================================================================
00226 // global definitions
00227 // ===========================================================================
00228 #define WRITE_WARNING(msg) MsgHandler::getWarningInstance()->inform(msg);
00229 #define WRITE_MESSAGE(msg) MsgHandler::getMessageInstance()->inform(msg);
00230 #define PROGRESS_BEGIN_MESSAGE(msg) MsgHandler::getMessageInstance()->beginProcessMsg(msg + std::string("..."));
00231 #define PROGRESS_DONE_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("done.");
00232 #define PROGRESS_FAILED_MESSAGE() MsgHandler::getMessageInstance()->endProcessMsg("failed.");
00233 #define WRITE_ERROR(msg)   MsgHandler::getErrorInstance()->inform(msg);
00234 
00235 #endif
00236 
00237 /****************************************************************************/
00238 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines