Ignore:
Timestamp:
Dec 13, 2011, 10:45:00 AM (14 years ago)
Author:
Frederik Heber <heber@…>
Children:
d85532
Parents:
40f2e6
git-author:
Frederik Heber <heber@…> (12/02/11 13:05:49)
git-committer:
Frederik Heber <heber@…> (12/13/11 10:45:00)
Message:

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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer/ObserverLog.hpp

    r40f2e6 r2c11c1  
    1414#endif
    1515
     16#include <boost/shared_ptr.hpp>
    1617#include <map>
    1718#include <set>
     19#include <iosfwd>
     20#include <string>
    1821#include <sstream>
    19 #include <string>
    2022
    2123#include "Observable.hpp"
    2224#include "Observer.hpp"
    23 
     25#include "Singleton.hpp"
    2426
    2527/**
     
    2830 * TODO: make this conditional dependent on compiler Flag.
    2931 */
    30 class ObserverLog{
     32class ObserverLog : public Singleton<ObserverLog> {
    3133  friend class Observable;
    3234  friend class Observer;
     35  friend class Relay;
    3336  template <typename> friend class Cacheable;
     37  friend class Singleton<ObserverLog>;
    3438public:
    35   ObserverLog();
    36   ~ObserverLog();
    3739  std::string getLog();                        // get everything that has been logged
    3840  std::string getName(void*);                  // get the name of an actor
    3941  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
    4057private:
     58  ObserverLog();
     59  ~ObserverLog();
    4160  int count;                                   // number to reference each actor in this framework
    4261  std::map<void*,std::string> names;           // List of names assigned to actors
     
    4665  void deleteName(void*);                      // delete the name of an Actor
    4766  void deleteObservable(void*);
    48   std::stringstream &addMessage(int depth=0);  // Add a Message to the logging
    49   std::stringstream log;                       // The internal log object
     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
    5071};
    5172
    5273ObserverLog &observerLog();
    5374
    54 
     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}
    5581
    5682#endif /* OBSERVERLOG_HPP_ */
Note: See TracChangeset for help on using the changeset viewer.