| 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 |  | 
|---|
| 31 | std::ostream* ObserverLog::nullstream = NULL; | 
|---|
| 32 | std::ostream* ObserverLog::outstream = NULL; | 
|---|
| 33 |  | 
|---|
| 34 | ObserverLog::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 |  | 
|---|
| 42 | ObserverLog::~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 | */ | 
|---|
| 56 | ObserverLog::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 | */ | 
|---|
| 65 | ObserverLog::Log::~Log() | 
|---|
| 66 | { | 
|---|
| 67 | boost::lock_guard<boost::recursive_mutex> guard(callback_ref->mutex); | 
|---|
| 68 | callback_ref->log << log.str() << std::endl; | 
|---|
| 69 | *callback_ref->outstream << log.str() << std::endl; | 
|---|
| 70 |  | 
|---|
| 71 | callback_ref = NULL; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | void ObserverLog::disableLogging() | 
|---|
| 75 | { | 
|---|
| 76 | boost::lock_guard<boost::recursive_mutex> guard(mutex); | 
|---|
| 77 | outstream = nullstream; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | void ObserverLog::enableLogging() | 
|---|
| 81 | { | 
|---|
| 82 | boost::lock_guard<boost::recursive_mutex> guard(mutex); | 
|---|
| 83 | outstream = &std::cout; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | std::string ObserverLog::getLog() { return log.str(); } | 
|---|
| 87 |  | 
|---|
| 88 | std::string ObserverLog::getName(void* obj){ | 
|---|
| 89 | boost::lock_guard<boost::recursive_mutex> guard(mutex); | 
|---|
| 90 | return names[obj]; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | bool ObserverLog::isObservable(void* obj){ | 
|---|
| 94 | boost::lock_guard<boost::recursive_mutex> guard(mutex); | 
|---|
| 95 | return observables.count(obj); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void ObserverLog::addName(void* obj , std::string name){ | 
|---|
| 99 | boost::lock_guard<boost::recursive_mutex> guard(mutex); | 
|---|
| 100 | std::stringstream sstr; | 
|---|
| 101 | sstr << name << "_" << count++; | 
|---|
| 102 | names[obj] = sstr.str(); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void ObserverLog::addObservable(void* obj){ | 
|---|
| 106 | boost::lock_guard<boost::recursive_mutex> guard(mutex); | 
|---|
| 107 | observables.insert(obj); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void ObserverLog::deleteName(void* obj){ | 
|---|
| 111 | boost::lock_guard<boost::recursive_mutex> guard(mutex); | 
|---|
| 112 | names.erase(obj); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | void ObserverLog::deleteObservable(void* obj){ | 
|---|
| 116 | boost::lock_guard<boost::recursive_mutex> guard(mutex); | 
|---|
| 117 | observables.erase(obj); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | /** Obtain Log reference to place another message (line) in log. | 
|---|
| 121 | * | 
|---|
| 122 | * \warning Don't append std::endl to the message, won't work and is done | 
|---|
| 123 | * automatically. | 
|---|
| 124 | * | 
|---|
| 125 | * @param depth depth (indenting) of the message | 
|---|
| 126 | * @return ref to Log class which can be ostreamed to | 
|---|
| 127 | */ | 
|---|
| 128 | boost::shared_ptr<ObserverLog::Log> ObserverLog::addMessage(int depth){ | 
|---|
| 129 | boost::lock_guard<boost::recursive_mutex> guard(mutex); | 
|---|
| 130 | boost::shared_ptr<ObserverLog::Log> L(new Log(this)); | 
|---|
| 131 | for(int i=depth;i--;) | 
|---|
| 132 | L->log << "  "; | 
|---|
| 133 | return L; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | ObserverLog &observerLog() | 
|---|
| 137 | { | 
|---|
| 138 | return ObserverLog::getInstance(); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | CONSTRUCT_SINGLETON(ObserverLog) | 
|---|