From: kanschat Date: Fri, 9 Dec 2011 20:00:35 +0000 (+0000) Subject: Introduce LogStream::Prefix X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5e96ccdd01b3b4f9204453c0039034b6e7586262;p=dealii-svn.git Introduce LogStream::Prefix git-svn-id: https://svn.dealii.org/trunk@24812 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index fdcc0f7543..942679a560 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -74,6 +74,12 @@ enabled due to a missing include file in file
    +
  1. Improved: Objects of the type LogStream::Prefix can now be used +as a safe implementation of the push and pop mechanism for log +prefices. +
    +(Guido Kanschat, 2011/12/09) +
  2. New: IndexSet::add_indices(IndexSet).
    (Timo Heister, 2011/12/09) diff --git a/deal.II/include/deal.II/base/logstream.h b/deal.II/include/deal.II/base/logstream.h index ae631d08b7..78ddcf0620 100644 --- a/deal.II/include/deal.II/base/logstream.h +++ b/deal.II/include/deal.II/base/logstream.h @@ -13,6 +13,7 @@ #define __deal2__logstream_h #include +#include #include #include @@ -48,10 +49,10 @@ DEAL_II_NAMESPACE_OPEN *
  3. deallog.attach(std::ostream): write logging information into a file. *
  4. deallog.depth_console(n): restrict output on screen to outer loops. *
  5. Before entering a new phase of your program, e.g. a new loop, - * deallog.push("loopname"). + * LogStream::Prefix p("loopname"). *
  6. deallog << anything << std::endl; to write logging information * (Usage of std::endl is mandatory!). - *
  7. deallog.pop() when leaving that stage entered with push. + *
  8. Destructor of the prefix will pop the prefix text from the stack. * * *

    LogStream and reproducible regression test output

    @@ -82,9 +83,53 @@ DEAL_II_NAMESPACE_OPEN * @ingroup textoutput * @author Guido Kanschat, Wolfgang Bangerth, 1999, 2003, 2011 */ -class LogStream +class LogStream : public Subscriptor { public: + /** + * A subclass allowing for the + * safe generation and removal of + * prefices. + * + * Somewhere at the beginning of + * a block, create one of these + * objects, and it will appear as + * a prefix in LogStream output + * like #deallog. At the end of + * the block, the prefix will + * automatically be removed, when + * this object is destroyed. + */ + class Prefix + { + public: + /** + * Set a new prefix for + * #deallog, which will be + * removed when the variable + * is destroyed . + */ + Prefix(const std::string& text); + + /** + * Set a new prefix for the + * given stream, which will + * be removed when the + * variable is destroyed . + */ + Prefix(const std::string& text, LogStream& stream); + + /** + * Remove the prefix + * associated with this + * variable. + */ + ~Prefix (); + + private: + SmartPointer stream; + }; + /** * Standard constructor, since we * intend to provide an object @@ -163,6 +208,8 @@ class LogStream const std::string& get_prefix () const; /** + * @deprecated Use Prefix instead + * * Push another prefix on the * stack. Prefixes are * automatically separated by a @@ -172,6 +219,8 @@ class LogStream void push (const std::string& text); /** + * @deprecated Use Prefix instead + * * Remove the last prefix. */ void pop (); @@ -649,6 +698,22 @@ LogStream::operator<< (const float t) } +inline +LogStream::Prefix::Prefix(const std::string& text, LogStream& s) + : + stream(&s) +{ + stream->push(text); +} + + +inline +LogStream::Prefix::~Prefix() +{ + stream->pop(); +} + + /** * The standard log object of DEAL. @@ -657,6 +722,17 @@ LogStream::operator<< (const float t) */ extern LogStream deallog; + +inline +LogStream::Prefix::Prefix(const std::string& text) + : + stream(&deallog) +{ + stream->push(text); +} + + + DEAL_II_NAMESPACE_CLOSE #endif