source: src/CodePatterns/Observer/ObserverLog.hpp@ 44f368

Last change on this file since 44f368 was 5d1550, checked in by Frederik Heber <heber@…>, 13 years ago

ObserverLog's addMessage() is now publicly available.

  • Property mode set to 100644
File size: 2.4 KB
Line 
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 */
32class 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>;
38public:
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 boost::shared_ptr<ObserverLog::Log> addMessage(int depth=0); // Add a Message to the logging
58
59private:
60 ObserverLog();
61 ~ObserverLog();
62 int count; // number to reference each actor in this framework
63 std::map<void*,std::string> names; // List of names assigned to actors
64 std::set<void*> observables; // List of pointers to Observables. Needed to distinguish Observers and Observables
65 void addName(void*, std::string); // Assign a name to an Actor
66 void addObservable(void*);
67 void deleteName(void*); // delete the name of an Actor
68 void deleteObservable(void*);
69 std::stringstream log;
70 static std::ostream *nullstream; // stream that is not displayed
71 static std::ostream *outstream; // stream that is currently used
72};
73
74ObserverLog &observerLog();
75
76template <class T>
77boost::shared_ptr<ObserverLog::Log> operator<<(boost::shared_ptr<ObserverLog::Log> L, const T msg)
78{
79 L->log << msg;
80 return L;
81}
82
83#endif /* OBSERVERLOG_HPP_ */
Note: See TracBrowser for help on using the repository browser.