/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * ObserverLog.cpp * * Created on: Dec 1, 2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif //#include "CodePatterns/MemDebug.hpp" #include #include #include #include #include "CodePatterns/Observer/ObserverLog.hpp" #include "CodePatterns/Singleton_impl.hpp" std::ostream* ObserverLog::nullstream = NULL; std::ostream* ObserverLog::outstream = NULL; ObserverLog::ObserverLog() : count(0) { boost::lock_guard guard(mutex); nullstream = new std::ofstream("/dev/null"); outstream = nullstream; } ObserverLog::~ObserverLog() { boost::lock_guard guard(mutex); outstream = NULL; delete nullstream; } /** Constructor of class Log. * * Log is just a tiny helper to bring the log message to screen and to the * ObserverLog's log. * * @param _callback_ref */ ObserverLog::Log::Log(ObserverLog *_callback_ref) : callback_ref(_callback_ref) {} /** Destructor of class Log. * * Prints to both ObserverLog::log and to ObserverLog::outstream. * */ ObserverLog::Log::~Log() { boost::lock_guard guard(callback_ref->mutex); callback_ref->log << log.str() << std::endl; *callback_ref->outstream << log.str() << std::endl; callback_ref = NULL; } void ObserverLog::disableLogging() { boost::lock_guard guard(mutex); outstream = nullstream; } void ObserverLog::enableLogging() { boost::lock_guard guard(mutex); outstream = &std::cout; } std::string ObserverLog::getLog() { return log.str(); } std::string ObserverLog::getName(void* obj){ boost::lock_guard guard(mutex); return names[obj]; } bool ObserverLog::isObservable(void* obj){ boost::lock_guard guard(mutex); return observables.count(obj); } void ObserverLog::addName(void* obj , std::string name){ boost::lock_guard guard(mutex); std::stringstream sstr; sstr << name << "_" << count++; names[obj] = sstr.str(); } void ObserverLog::addObservable(void* obj){ boost::lock_guard guard(mutex); observables.insert(obj); } void ObserverLog::deleteName(void* obj){ boost::lock_guard guard(mutex); names.erase(obj); } void ObserverLog::deleteObservable(void* obj){ boost::lock_guard guard(mutex); observables.erase(obj); } /** Obtain Log reference to place another message (line) in log. * * \warning Don't append std::endl to the message, won't work and is done * automatically. * * @param depth depth (indenting) of the message * @return ref to Log class which can be ostreamed to */ boost::shared_ptr ObserverLog::addMessage(int depth){ boost::lock_guard guard(mutex); boost::shared_ptr L(new Log(this)); for(int i=depth;i--;) L->log << " "; return L; } ObserverLog &observerLog() { return ObserverLog::getInstance(); } CONSTRUCT_SINGLETON(ObserverLog)