| 1 | /* | 
|---|
| 2 | * ObserverLog.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Dec 1, 2011 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef OBSERVERLOG_HPP_ | 
|---|
| 9 | #define OBSERVERLOG_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 | // include config.h | 
|---|
| 12 | #ifdef HAVE_CONFIG_H | 
|---|
| 13 | #include <config.h> | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 | #include <boost/shared_ptr.hpp> | 
|---|
| 17 | #include <map> | 
|---|
| 18 | #include <set> | 
|---|
| 19 | #include <iosfwd> | 
|---|
| 20 | #include <string> | 
|---|
| 21 | #include <sstream> | 
|---|
| 22 |  | 
|---|
| 23 | #include "CodePatterns/Observer/Observable.hpp" | 
|---|
| 24 | #include "CodePatterns/Observer/Observer.hpp" | 
|---|
| 25 | #include "CodePatterns/Singleton.hpp" | 
|---|
| 26 |  | 
|---|
| 27 | /** | 
|---|
| 28 | * This class is used to log the working of the observer mechanism | 
|---|
| 29 | * | 
|---|
| 30 | * TODO: make this conditional dependent on compiler Flag. | 
|---|
| 31 | */ | 
|---|
| 32 | class ObserverLog : public Singleton<ObserverLog> { | 
|---|
| 33 | friend class Observable; | 
|---|
| 34 | friend class Observer; | 
|---|
| 35 | friend class Relay; | 
|---|
| 36 | template <typename> friend class Cacheable; | 
|---|
| 37 | friend class Singleton<ObserverLog>; | 
|---|
| 38 | public: | 
|---|
| 39 | std::string getLog();                        // get everything that has been logged | 
|---|
| 40 | std::string getName(void*);                  // get the name of an actor | 
|---|
| 41 | bool isObservable(void*); | 
|---|
| 42 | void disableLogging(); | 
|---|
| 43 | void enableLogging(); | 
|---|
| 44 |  | 
|---|
| 45 | /** tiny helper class to allow for both capturing and printing of messages. | 
|---|
| 46 | * | 
|---|
| 47 | */ | 
|---|
| 48 | class Log { | 
|---|
| 49 | public: | 
|---|
| 50 | Log(ObserverLog *_callback_ref); | 
|---|
| 51 | ~Log(); | 
|---|
| 52 |  | 
|---|
| 53 | std::stringstream log;                      // the internal stream that later gets appended | 
|---|
| 54 | ObserverLog *callback_ref;                  // internal stringstream to capture messages | 
|---|
| 55 | }; | 
|---|
| 56 |  | 
|---|
| 57 | private: | 
|---|
| 58 | ObserverLog(); | 
|---|
| 59 | ~ObserverLog(); | 
|---|
| 60 | int count;                                   // number to reference each actor in this framework | 
|---|
| 61 | std::map<void*,std::string> names;           // List of names assigned to actors | 
|---|
| 62 | std::set<void*> observables;                 // List of pointers to Observables. Needed to distinguish Observers and Observables | 
|---|
| 63 | void addName(void*, std::string);            // Assign a name to an Actor | 
|---|
| 64 | void addObservable(void*); | 
|---|
| 65 | void deleteName(void*);                      // delete the name of an Actor | 
|---|
| 66 | void deleteObservable(void*); | 
|---|
| 67 | boost::shared_ptr<ObserverLog::Log> addMessage(int depth=0);       // Add a Message to the logging | 
|---|
| 68 | std::stringstream log; | 
|---|
| 69 | static std::ostream *nullstream;             // stream that is not displayed | 
|---|
| 70 | static std::ostream *outstream;              // stream that is currently used | 
|---|
| 71 | }; | 
|---|
| 72 |  | 
|---|
| 73 | ObserverLog &observerLog(); | 
|---|
| 74 |  | 
|---|
| 75 | template <class T> | 
|---|
| 76 | boost::shared_ptr<ObserverLog::Log> operator<<(boost::shared_ptr<ObserverLog::Log> L, const T msg) | 
|---|
| 77 | { | 
|---|
| 78 | L->log << msg; | 
|---|
| 79 | return L; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | #endif /* OBSERVERLOG_HPP_ */ | 
|---|