source: src/Patterns/Observer/ObserverLog.hpp@ d85532

Last change on this file since d85532 was 2c11c1, checked in by Frederik Heber <heber@…>, 14 years ago

Modified ObserverLog: Singleton and both output to screen and internal Log.

  • ObserverLog can now either be printed concurrently or later requested as string (e.g. on exit as has been done before). This should ease debugging Observer code.
  • enable/disableLogging() (dis)activate printing to screen.
  • ObserverLog is now a true singleton.

Details:

  • Implemented helper class ObserverLog::Log which is returned as boost::shared_ptr on addMessage() and can be streamed to.
  • On dstor the logged message is appended with endl, placed into ObserverLog's internal log and printed on screen if enableLogging().
  • NOTE: streaming std::endl is not working, and not necessary.
  • 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
23#include "Observable.hpp"
24#include "Observer.hpp"
[2c11c1]25#include "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
[e2e035e]57private:
[2c11c1]58 ObserverLog();
59 ~ObserverLog();
[e2e035e]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*);
[2c11c1]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
[e2e035e]71};
72
73ObserverLog &observerLog();
74
[2c11c1]75template <class T>
76boost::shared_ptr<ObserverLog::Log> operator<<(boost::shared_ptr<ObserverLog::Log> L, const T msg)
77{
78 L->log << msg;
79 return L;
80}
[e2e035e]81
82#endif /* OBSERVERLOG_HPP_ */
Note: See TracBrowser for help on using the repository browser.