]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Flush content of logstream during its destruction if anything was left.
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Thu, 24 Sep 2009 02:35:21 +0000 (02:35 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Thu, 24 Sep 2009 02:35:21 +0000 (02:35 +0000)
git-svn-id: https://svn.dealii.org/trunk@19524 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/source/log.cc
tests/base/Makefile
tests/base/logstream_end.cc [new file with mode: 0644]
tests/base/logstream_end/cmp/generic [new file with mode: 0644]

index 19f74df5df429889fae9eb6bfae90e67196adfd1..4790aadb0dba556eeaa43e6641b937d7816e93d4 100644 (file)
@@ -32,7 +32,7 @@
 // on SunOS 4.x, getrusage is stated in the man pages and exists, but
 // is not declared in resource.h. declare it ourselves
 #ifdef NO_HAVE_GETRUSAGE
-extern "C" { 
+extern "C" {
   int getrusage(int who, struct rusage* ru);
 }
 #endif
@@ -41,7 +41,7 @@ extern "C" {
 // another thread tries to do the same.
 DEAL_II_NAMESPACE_OPEN
 
-namespace 
+namespace
 {
   Threads::ThreadMutex log_lock;
   Threads::ThreadMutex write_lock;
@@ -65,6 +65,20 @@ LogStream::LogStream()
 
 LogStream::~LogStream()
 {
+                                  // if there was anything left in
+                                  // the stream that is current to
+                                  // this thread, make sure we flush
+                                  // it before it gets lost
+  {
+    const unsigned int id = Threads::this_thread_id();
+    if ((outstreams.find(id) != outstreams.end())
+       &&
+       (*outstreams[id] != 0)
+       &&
+       (outstreams[id]->str().length() > 0))
+      *this << std::endl;
+  }
+
   if (old_cerr)
     std::cerr.rdbuf(old_cerr);
 
@@ -80,7 +94,7 @@ LogStream::~LogStream()
                                   // deliberate memory leak and
                                   // instead destroying an empty
                                   // object
-#ifdef DEAL_II_USE_TRILINOS  
+#ifdef DEAL_II_USE_TRILINOS
   if (this == &deallog)
     (new stream_map_type())->swap (outstreams);
 #endif
@@ -105,10 +119,10 @@ LogStream::operator<< (std::ostream& (*p) (std::ostream&))
       std::ostringstream& stream = get_stream();
       if (prefixes.size() <= std_depth)
        *std_out << stream.str();
-      
+
       if (file && (prefixes.size() <= file_depth))
        *file << stream.str() << std::flush;
-      
+
                                       // Start a new string
       stream.str("");
     }
@@ -120,14 +134,14 @@ std::ostringstream&
 LogStream::get_stream()
 {
   Threads::ThreadMutex::ScopedLock lock(log_lock);
-  unsigned int id = Threads::this_thread_id();
+  const unsigned int id = Threads::this_thread_id();
 
   std_cxx1x::shared_ptr<std::ostringstream>& sptr = outstreams[id];
   if (sptr == 0)
     {
       sptr = std_cxx1x::shared_ptr<std::ostringstream> (new std::ostringstream());
       sptr->setf(std::ios::showpoint | std::ios::left);
-    } 
+    }
   return *sptr;
 }
 
@@ -300,10 +314,10 @@ LogStream::print_line_head()
  * When we have more information about the kernel, this should be
  * incorporated properly. Suggestions are welcome!
  */
-  
+
 #ifdef DEALII_MEMORY_DEBUG
   static const pid_t id = getpid();
-  
+
   std::ostringstream statname;
   statname << "/proc/" << id << "/stat";
 
@@ -316,10 +330,10 @@ LogStream::print_line_head()
     dummy >> dummy >> dummy >> dummy >> dummy >> dummy >>
     dummy >> dummy >> dummy >> dummy >> dummy >> size;
 #endif
-  
+
   const std::string& head = get_prefix();
   const unsigned int thread = Threads::this_thread_id();
-  
+
   if (prefixes.size() <= std_depth)
     {
       if (print_utime)
@@ -333,10 +347,10 @@ LogStream::print_line_head()
        }
       if (print_thread_id)
        *std_out << '[' << thread << ']';
-      
+
       *std_out <<  head << ':';
     }
-  
+
   if (file && (prefixes.size() <= file_depth))
     {
       if (print_utime)
@@ -347,10 +361,10 @@ LogStream::print_line_head()
          *file << size << ':';
 #endif
          file->width(p);
-       }  
+       }
       if (print_thread_id)
        *file << '[' << thread << ']';
-      
+
       *file << head << ':';
     }
 }
@@ -370,7 +384,7 @@ LogStream::memory_consumption () const
       mem += MemoryConsumption::memory_consumption (tmp.top());
       tmp.pop ();
     };
-  
+
   return mem;
 }
 
index 932f13da0c93148ce7975e0b1ff412c74c3a51de..4ccae89d946d3e9c28fa070916f70d9defb168cf 100644 (file)
@@ -14,7 +14,7 @@ debug-mode = on
 libraries += $(lib-lac.g)  \
              $(lib-base.g)
 
-default: run-tests 
+default: run-tests
 
 ############################################################
 
@@ -62,6 +62,7 @@ task_%/output : task_%/exe
 ############################################################
 tests_x =  geometry_info_* \
           logtest \
+          logstream_end \
           log_* \
           reference \
           quadrature_* qprojector \
diff --git a/tests/base/logstream_end.cc b/tests/base/logstream_end.cc
new file mode 100644 (file)
index 0000000..8dfe598
--- /dev/null
@@ -0,0 +1,47 @@
+//----------------------------  logstream_end.cc  ---------------------------
+//    $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2009 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  logstream_end.cc  ---------------------------
+
+
+// it used to happen that if we destroyed logstream (and presumably
+// all objects of the same type) that whatever we had put into with
+// operator<< after the last use of std::endl was lost. make sure that
+// that isn't the case anymore: logstream should flush whatever it has
+// left over when it is destroyed
+
+
+#include "../tests.h"
+#include <base/logstream.h>
+#include <fstream>
+#include <iomanip>
+#include <limits>
+
+
+int main()
+{
+  std::ofstream logfile("logstream_end/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  {
+    LogStream log;
+
+    log.attach(logfile);
+    log.depth_console(0);
+    log.threshold_double(1.e-10);
+    log.log_thread_id (false);
+
+    log << "This should be printed!";
+  }
+//  deallog << "OK" << std::endl;
+}
diff --git a/tests/base/logstream_end/cmp/generic b/tests/base/logstream_end/cmp/generic
new file mode 100644 (file)
index 0000000..ccf3e2f
--- /dev/null
@@ -0,0 +1,4 @@
+
+
+DEAL::This should be printed!
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.