source: src/CodePatterns/Observer/ObserverLog.hpp@ 454bc54

Last change on this file since 454bc54 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
RevLine 
[e2e035e]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
[2c11c1]16#include <boost/shared_ptr.hpp>
[e2e035e]17#include <map>
18#include <set>
[2c11c1]19#include <iosfwd>
[e2e035e]20#include <string>
[2c11c1]21#include <sstream>
[e2e035e]22
[9b8fa4]23#include "CodePatterns/Observer/Observable.hpp"
24#include "CodePatterns/Observer/Observer.hpp"
25#include "CodePatterns/Singleton.hpp"
[e2e035e]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 */
[2c11c1]32class ObserverLog : public Singleton<ObserverLog> {
[e2e035e]33 friend class Observable;
34 friend class Observer;
[2c11c1]35 friend class Relay;
[e2e035e]36 template <typename> friend class Cacheable;
[2c11c1]37 friend class Singleton<ObserverLog>;
[e2e035e]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*);
[2c11c1]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
[5d1550]57 boost::shared_ptr<ObserverLog::Log> addMessage(int depth=0); // Add a Message to the logging
58
[e2e035e]59private:
[2c11c1]60 ObserverLog();
61 ~ObserverLog();
[e2e035e]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*);
[2c11c1]69 std::stringstream log;
70 static std::ostream *nullstream; // stream that is not displayed
71 static std::ostream *outstream; // stream that is currently used
[e2e035e]72};
73
74ObserverLog &observerLog();
75
[2c11c1]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}
[e2e035e]82
83#endif /* OBSERVERLOG_HPP_ */
Note: See TracBrowser for help on using the repository browser.