source: src/Observer/ObserverLog.cpp@ 3681dd

Last change on this file since 3681dd was 6e2f3b, checked in by Frederik Heber <heber@…>, 10 years ago

ObserverLog is also protected with mutexes.

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[e2e035e]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
[9b8fa4]20#include "CodePatterns/MemDebug.hpp"
[e2e035e]21
[2c11c1]22#include <fstream>
23#include <iostream>
24#include <sstream>
25
[6e2f3b]26#include <boost/thread/locks.hpp>
27
[9b8fa4]28#include "CodePatterns/Observer/ObserverLog.hpp"
29#include "CodePatterns/Singleton_impl.hpp"
[2c11c1]30
31std::ostream* ObserverLog::nullstream = NULL;
32std::ostream* ObserverLog::outstream = NULL;
[e2e035e]33
[adedb4]34ObserverLog::ObserverLog() :
35 count(0)
[2c11c1]36{
[6e2f3b]37 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[2c11c1]38 nullstream = new std::ofstream("/dev/null");
39 outstream = nullstream;
40}
41
42ObserverLog::~ObserverLog()
43{
[6e2f3b]44 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[2c11c1]45 outstream = NULL;
46 delete nullstream;
47}
[e2e035e]48
[2c11c1]49/** Constructor of class Log.
50 *
51 * Log is just a tiny helper to bring the log message to screen and to the
52 * ObserverLog's log.
53 *
54 * @param _callback_ref
55 */
56ObserverLog::Log::Log(ObserverLog *_callback_ref) :
57 callback_ref(_callback_ref)
[e2e035e]58{}
59
[2c11c1]60/** Destructor of class Log.
61 *
62 * Prints to both ObserverLog::log and to ObserverLog::outstream.
63 *
64 */
65ObserverLog::Log::~Log()
66{
67 callback_ref->log << log.str() << std::endl;
68 *callback_ref->outstream << log.str() << std::endl;
69
70 callback_ref = NULL;
71}
72
73void ObserverLog::disableLogging()
74{
[6e2f3b]75 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[2c11c1]76 outstream = nullstream;
77}
78
79void ObserverLog::enableLogging()
80{
[6e2f3b]81 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[2c11c1]82 outstream = &std::cout;
83}
[e2e035e]84
[2c11c1]85std::string ObserverLog::getLog() { return log.str(); }
[e2e035e]86
87std::string ObserverLog::getName(void* obj){
[6e2f3b]88 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[e2e035e]89 return names[obj];
90}
91
92bool ObserverLog::isObservable(void* obj){
[6e2f3b]93 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[e2e035e]94 return observables.count(obj);
95}
96
97void ObserverLog::addName(void* obj , std::string name){
[6e2f3b]98 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[e2e035e]99 std::stringstream sstr;
100 sstr << name << "_" << count++;
101 names[obj] = sstr.str();
102}
103
104void ObserverLog::addObservable(void* obj){
[6e2f3b]105 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[e2e035e]106 observables.insert(obj);
107}
108
109void ObserverLog::deleteName(void* obj){
[6e2f3b]110 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[e2e035e]111 names.erase(obj);
112}
113
114void ObserverLog::deleteObservable(void* obj){
[6e2f3b]115 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[e2e035e]116 observables.erase(obj);
117}
118
[2c11c1]119/** Obtain Log reference to place another message (line) in log.
120 *
121 * \warning Don't append std::endl to the message, won't work and is done
122 * automatically.
123 *
124 * @param depth depth (indenting) of the message
125 * @return ref to Log class which can be ostreamed to
126 */
127boost::shared_ptr<ObserverLog::Log> ObserverLog::addMessage(int depth){
[6e2f3b]128 boost::lock_guard<boost::recursive_mutex> guard(mutex);
[2c11c1]129 boost::shared_ptr<ObserverLog::Log> L(new Log(this));
[e2e035e]130 for(int i=depth;i--;)
[2c11c1]131 L->log << " ";
132 return L;
[e2e035e]133}
134
[2c11c1]135ObserverLog &observerLog()
136{
137 return ObserverLog::getInstance();
[e2e035e]138}
139
[2c11c1]140CONSTRUCT_SINGLETON(ObserverLog)
Note: See TracBrowser for help on using the repository browser.