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