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.cpp

    r40f2e6 r2c11c1  
    2020#include "MemDebug.hpp"
    2121
     22#include <fstream>
     23#include <iostream>
     24#include <sstream>
     25
    2226#include "ObserverLog.hpp"
     27#include "Singleton_impl.hpp"
    2328
     29std::ostream* ObserverLog::nullstream = NULL;
     30std::ostream* ObserverLog::outstream = NULL;
    2431
    25 ObserverLog::ObserverLog() :
    26   count (0)
     32ObserverLog::ObserverLog()
     33{
     34  nullstream = new std::ofstream("/dev/null");
     35  outstream = nullstream;
     36}
     37
     38ObserverLog::~ObserverLog()
     39{
     40  outstream = NULL;
     41  delete nullstream;
     42}
     43
     44/** Constructor of class Log.
     45 *
     46 * Log is just a tiny helper to bring the log message to screen and to the
     47 * ObserverLog's log.
     48 *
     49 * @param _callback_ref
     50 */
     51ObserverLog::Log::Log(ObserverLog *_callback_ref) :
     52    callback_ref(_callback_ref)
    2753{}
    2854
    29 ObserverLog::~ObserverLog(){}
     55/** Destructor of class Log.
     56 *
     57 * Prints to both ObserverLog::log and to ObserverLog::outstream.
     58 *
     59 */
     60ObserverLog::Log::~Log()
     61{
     62  callback_ref->log << log.str() << std::endl;
     63  *callback_ref->outstream << log.str() << std::endl;
    3064
    31 std::string ObserverLog::getLog(){return log.str();}
     65  callback_ref = NULL;
     66}
     67
     68void ObserverLog::disableLogging()
     69{
     70  outstream = nullstream;
     71}
     72
     73void ObserverLog::enableLogging()
     74{
     75  outstream = &std::cout;
     76}
     77
     78std::string ObserverLog::getLog() { return log.str(); }
    3279
    3380std::string ObserverLog::getName(void* obj){
     
    57104}
    58105
    59 std::stringstream &ObserverLog::addMessage(int depth){
     106/** Obtain Log reference to place another message (line) in log.
     107 *
     108 * \warning Don't append std::endl to the message, won't work and is done
     109 * automatically.
     110 *
     111 * @param depth depth (indenting) of the message
     112 * @return ref to Log class which can be ostreamed to
     113 */
     114boost::shared_ptr<ObserverLog::Log> ObserverLog::addMessage(int depth){
     115  boost::shared_ptr<ObserverLog::Log> L(new Log(this));
    60116  for(int i=depth;i--;)
    61     log << "  ";
    62   return log;
     117    L->log << "  ";
     118  return L;
    63119}
    64120
    65 // The log needs to exist fairly early, so we make it construct on first use,
    66 // and never destroy it
    67 ObserverLog &observerLog(){
    68   // yes, this memory is never freed... we need it around for the whole programm,
    69   // so no freeing is possible
    70   static ObserverLog *theLog = Memory::ignore(new ObserverLog());
    71   return *theLog;
     121ObserverLog &observerLog()
     122{
     123  return ObserverLog::getInstance();
    72124}
    73125
     126CONSTRUCT_SINGLETON(ObserverLog)
Note: See TracChangeset for help on using the changeset viewer.