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