From: Wolfgang Bangerth Date: Thu, 24 Sep 2009 02:35:21 +0000 (+0000) Subject: Flush content of logstream during its destruction if anything was left. X-Git-Tag: v8.0.0~7053 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=15c7fefa7259704766696dd6dff0383196cfefa9;p=dealii.git Flush content of logstream during its destruction if anything was left. git-svn-id: https://svn.dealii.org/trunk@19524 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/source/log.cc b/deal.II/base/source/log.cc index 19f74df5df..4790aadb0d 100644 --- a/deal.II/base/source/log.cc +++ b/deal.II/base/source/log.cc @@ -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& sptr = outstreams[id]; if (sptr == 0) { sptr = std_cxx1x::shared_ptr (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; } diff --git a/tests/base/Makefile b/tests/base/Makefile index 932f13da0c..4ccae89d94 100644 --- a/tests/base/Makefile +++ b/tests/base/Makefile @@ -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 index 0000000000..8dfe59806e --- /dev/null +++ b/tests/base/logstream_end.cc @@ -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 +#include +#include +#include + + +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 index 0000000000..ccf3e2ff22 --- /dev/null +++ b/tests/base/logstream_end/cmp/generic @@ -0,0 +1,4 @@ + + +DEAL::This should be printed! +DEAL::OK