source: src/Patterns/Observer/ObserverLog.cpp@ 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.5 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * ObserverLog.cpp
10 *
11 * Created on: Dec 1, 2011
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "MemDebug.hpp"
21
22#include <fstream>
23#include <iostream>
24#include <sstream>
25
26#include "ObserverLog.hpp"
27#include "Singleton_impl.hpp"
28
29std::ostream* ObserverLog::nullstream = NULL;
30std::ostream* ObserverLog::outstream = NULL;
31
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)
53{}
54
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;
64
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(); }
79
80std::string ObserverLog::getName(void* obj){
81 return names[obj];
82}
83
84bool ObserverLog::isObservable(void* obj){
85 return observables.count(obj);
86}
87
88void ObserverLog::addName(void* obj , std::string name){
89 std::stringstream sstr;
90 sstr << name << "_" << count++;
91 names[obj] = sstr.str();
92}
93
94void ObserverLog::addObservable(void* obj){
95 observables.insert(obj);
96}
97
98void ObserverLog::deleteName(void* obj){
99 names.erase(obj);
100}
101
102void ObserverLog::deleteObservable(void* obj){
103 observables.erase(obj);
104}
105
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));
116 for(int i=depth;i--;)
117 L->log << " ";
118 return L;
119}
120
121ObserverLog &observerLog()
122{
123 return ObserverLog::getInstance();
124}
125
126CONSTRUCT_SINGLETON(ObserverLog)
Note: See TracBrowser for help on using the repository browser.