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
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 "CodePatterns/MemDebug.hpp"
21
22#include <fstream>
23#include <iostream>
24#include <sstream>
25
26#include <boost/thread/locks.hpp>
27
28#include "CodePatterns/Observer/ObserverLog.hpp"
29#include "CodePatterns/Singleton_impl.hpp"
30
31std::ostream* ObserverLog::nullstream = NULL;
32std::ostream* ObserverLog::outstream = NULL;
33
34ObserverLog::ObserverLog() :
35 count(0)
36{
37 boost::lock_guard<boost::recursive_mutex> guard(mutex);
38 nullstream = new std::ofstream("/dev/null");
39 outstream = nullstream;
40}
41
42ObserverLog::~ObserverLog()
43{
44 boost::lock_guard<boost::recursive_mutex> guard(mutex);
45 outstream = NULL;
46 delete nullstream;
47}
48
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)
58{}
59
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{
75 boost::lock_guard<boost::recursive_mutex> guard(mutex);
76 outstream = nullstream;
77}
78
79void ObserverLog::enableLogging()
80{
81 boost::lock_guard<boost::recursive_mutex> guard(mutex);
82 outstream = &std::cout;
83}
84
85std::string ObserverLog::getLog() { return log.str(); }
86
87std::string ObserverLog::getName(void* obj){
88 boost::lock_guard<boost::recursive_mutex> guard(mutex);
89 return names[obj];
90}
91
92bool ObserverLog::isObservable(void* obj){
93 boost::lock_guard<boost::recursive_mutex> guard(mutex);
94 return observables.count(obj);
95}
96
97void ObserverLog::addName(void* obj , std::string name){
98 boost::lock_guard<boost::recursive_mutex> guard(mutex);
99 std::stringstream sstr;
100 sstr << name << "_" << count++;
101 names[obj] = sstr.str();
102}
103
104void ObserverLog::addObservable(void* obj){
105 boost::lock_guard<boost::recursive_mutex> guard(mutex);
106 observables.insert(obj);
107}
108
109void ObserverLog::deleteName(void* obj){
110 boost::lock_guard<boost::recursive_mutex> guard(mutex);
111 names.erase(obj);
112}
113
114void ObserverLog::deleteObservable(void* obj){
115 boost::lock_guard<boost::recursive_mutex> guard(mutex);
116 observables.erase(obj);
117}
118
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){
128 boost::lock_guard<boost::recursive_mutex> guard(mutex);
129 boost::shared_ptr<ObserverLog::Log> L(new Log(this));
130 for(int i=depth;i--;)
131 L->log << " ";
132 return L;
133}
134
135ObserverLog &observerLog()
136{
137 return ObserverLog::getInstance();
138}
139
140CONSTRUCT_SINGLETON(ObserverLog)
Note: See TracBrowser for help on using the repository browser.