From 8032345cbf2dc8364f9699453ef811f399bd6342 Mon Sep 17 00:00:00 2001
From: andreas <andreas@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Fri, 23 Jul 2010 21:32:35 +0000
Subject: [PATCH] readded redirection of logging to file

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2589 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/base/pymod/export_logger.cc | 12 +++++++++++
 modules/base/src/log.cc             | 32 +++++++++++++++++++++++++----
 modules/base/src/log.hh             |  8 +++++++-
 3 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/modules/base/pymod/export_logger.cc b/modules/base/pymod/export_logger.cc
index deefb408c..52eff9114 100644
--- a/modules/base/pymod/export_logger.cc
+++ b/modules/base/pymod/export_logger.cc
@@ -39,6 +39,16 @@ void pop_verb()
   Logger::Instance().PopVerbosityLevel();
 }
 
+void pop_verb_file() 
+{
+  Logger::Instance().PopFile();
+}
+
+void push_verb_file(const String& filename) 
+{
+  Logger::Instance().PushFile(filename);
+}
+
 void log_error(const String& m) {LOGN_ERROR(m);}
 void log_message(const String& m) {LOGN_MESSAGE(m);}
 void log_verbose(const String& m) {LOGN_VERBOSE(m);}
@@ -51,6 +61,8 @@ void export_Logger()
   
   def("PushVerbosityLevel",push_verb);
   def("PopVerbosityLevel",pop_verb);
+  def("PushVerbosityFile",push_verb_file);
+  def("PopVerbosityFile",pop_verb_file);
 
   def("LogError",log_error);
   def("LogMessage",log_message);
diff --git a/modules/base/src/log.cc b/modules/base/src/log.cc
index 10d0f677a..1a1534260 100644
--- a/modules/base/src/log.cc
+++ b/modules/base/src/log.cc
@@ -17,6 +17,7 @@
 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 //------------------------------------------------------------------------------
 #include <iostream>
+#include <fstream>
 
 
 #include <ost/log.hh>
@@ -45,15 +46,24 @@ Logger& Logger::Instance()
 Logger::Logger():
   level_(0),
   level_stack_(),
-  null_(new DevNull())
+  null_(new DevNull()),
+  stream_(0),
+  ostream_stack_()
 {
+  ostream_stack_.push(new std::ostream(std::cerr.rdbuf()));
+  stream_.rdbuf(ostream_stack_.top()->rdbuf());
 }
 
 Logger::Logger(const Logger&):
   level_(0),
   level_stack_(),
-  null_(0)
-{}
+  null_(0),
+  stream_(0),
+  ostream_stack_()
+{
+  ostream_stack_.push(new std::ostream(std::cerr.rdbuf()));
+  stream_.rdbuf(ostream_stack_.top()->rdbuf());
+}
 
 Logger& Logger::operator=(const Logger&)
 {
@@ -81,11 +91,25 @@ void Logger::PopVerbosityLevel()
 std::ostream& Logger::operator()(enum LogLevel l)
 {
   if(l<=level_) {
-    return std::cerr;
+    return stream_;
   }
   return null_;
 }
 
+void Logger::PushFile(const String& fn)
+{
+  ostream_stack_.push(new std::ofstream(fn.c_str()));
+  stream_.rdbuf(ostream_stack_.top()->rdbuf());
+}
+
+void Logger::PopFile()
+{
+  if(ostream_stack_.size()>1) {
+    delete ostream_stack_.top();
+    ostream_stack_.pop();
+    stream_.rdbuf(ostream_stack_.top()->rdbuf());
+  }
+}
 
 
 } // ns
diff --git a/modules/base/src/log.hh b/modules/base/src/log.hh
index ac86280a2..a6b3912a7 100644
--- a/modules/base/src/log.hh
+++ b/modules/base/src/log.hh
@@ -26,6 +26,8 @@
 
 namespace ost {
 
+typedef std::stack<std::ostream*> OStreamStack;
+
   // singleton
 class DLLEXPORT_OST_BASE Logger {
 public:
@@ -40,6 +42,8 @@ public:
   
   void PushVerbosityLevel(int level);
   void PopVerbosityLevel();
+  void PushFile(const String& filename);
+  void PopFile();
 
   std::ostream& operator()(enum LogLevel);
 
@@ -51,11 +55,13 @@ protected:
   Logger();
   Logger(const Logger&);
   Logger& operator=(const Logger&);
-
+  
 private:
   int level_;
   std::stack<int> level_stack_;
   std::ostream null_;
+  std::ostream stream_;
+  OStreamStack ostream_stack_;
 };
 
 #define PUSH_VERBOSITY(n) ::ost::Logger::Instance().PushVerbosityLevel(n)
-- 
GitLab