include/deal.II/base/logstream.h

00001 //---------------------------------------------------------------------------
00002 //    @f$Id: logstream.h 25460 2012-04-27 09:19:06Z bangerth @f$
00003 //
00004 //    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2011, 2012 by the deal.II authors
00005 //
00006 //    This file is subject to QPL and may not be  distributed
00007 //    without copyright and license information. Please refer
00008 //    to the file deal.II/doc/license.html for the  text  and
00009 //    further information on this license.
00010 //
00011 //---------------------------------------------------------------------------
00012 #ifndef __deal2__logstream_h
00013 #define __deal2__logstream_h
00014 
00015 #include <deal.II/base/config.h>
00016 #include <deal.II/base/smartpointer.h>
00017 #include <deal.II/base/exceptions.h>
00018 #include <deal.II/base/std_cxx1x/shared_ptr.h>
00019 
00020 #include <string>
00021 #include <stack>
00022 #include <map>
00023 #include <cmath>
00024 #include <sstream>
00025 
00026 #ifdef HAVE_SYS_TIMES_H
00027 #include <sys/times.h>
00028 #else
00029 struct tms
00030 {
00031     int tms_utime, tms_stime, tms_cutime, tms_cstime;
00032 };
00033 #endif
00034 
00035 DEAL_II_NAMESPACE_OPEN
00036 
00086 class LogStream : public Subscriptor
00087 {
00088   public:
00103     class Prefix
00104     {
00105       public:
00112         Prefix(const std::string& text);
00113 
00120         Prefix(const std::string& text, LogStream& stream);
00121 
00127         ~Prefix ();
00128 
00129       private:
00130         SmartPointer<LogStream,LogStream::Prefix> stream;
00131     };
00132 
00139     LogStream ();
00140 
00144     ~LogStream();
00145 
00150     void attach (std::ostream& o);
00151 
00158     void detach ();
00159 
00178     void test_mode (bool on=true);
00179 
00183     std::ostream& get_console ();
00184 
00188     std::ostream& get_file_stream ();
00189 
00194     bool has_file () const;
00195 
00203     void log_cerr ();
00204 
00208     const std::string& get_prefix () const;
00209 
00219     void push (const std::string& text);
00220 
00226     void pop ();
00227 
00242     unsigned int depth_console (const unsigned int n);
00243 
00256     unsigned int depth_file (const unsigned int n);
00257 
00267     bool log_execution_time (const bool flag);
00268 
00289     bool log_time_differences (const bool flag);
00290 
00297     void timestamp();
00298 
00302     bool log_thread_id (const bool flag);
00303 
00344     void threshold_double(const double t);
00350     void threshold_float(const float t);
00351 
00356     template <typename T>
00357     LogStream & operator << (const T &t);
00358 
00370     LogStream & operator << (const double t);
00371 
00383     LogStream & operator << (const float t);
00384 
00412     LogStream & operator<< (std::ostream& (*p) (std::ostream&));
00413 
00430     std::size_t memory_consumption () const;
00431 
00435     DeclException0(ExcNoFileStreamGiven);
00436 
00437   private:
00438 
00445     std::stack<std::string> prefixes;
00446 
00453     std::ostream  *std_out;
00454 
00463     std::ostream  *file;
00464 
00475     unsigned int std_depth;
00476 
00481     unsigned int file_depth;
00482 
00486     bool print_utime;
00487 
00491     bool diff_utime;
00492 
00496     double last_time;
00497 
00504     double double_threshold;
00505 
00512     float float_threshold;
00513 
00547     double offset;
00548 
00552     bool print_thread_id;
00553 
00558     double reference_time_val;
00559 
00564     struct tms reference_tms;
00565 
00576     std::streambuf *old_cerr;
00577 
00583     void print_line_head ();
00584 
00592     template <typename T>
00593     void print (const T &t);
00599     std::ostringstream& get_stream();
00600 
00604     typedef std::map<unsigned int, std_cxx1x::shared_ptr<std::ostringstream> > stream_map_type;
00605 
00611     stream_map_type outstreams;
00612 
00613 };
00614 
00615 
00616 /* ----------------------------- Inline functions and templates ---------------- */
00617 
00618 
00619 template <class T>
00620 inline
00621 void
00622 LogStream::print (const T &t)
00623 {
00624                                    // if the previous command was an
00625                                    // <tt>std::endl</tt>, print the topmost
00626                                    // prefix and a colon
00627   std::ostringstream& stream = get_stream();
00628   stream << t;
00629                                    // print the rest of the message
00630 //   if (prefixes.size() <= std_depth)
00631 //     *std_out << t;
00632 
00633 //   if (file && (prefixes.size() <= file_depth))
00634 //     *file << t;
00635 }
00636 
00637 
00638 
00639 template <class T>
00640 inline
00641 LogStream &
00642 LogStream::operator<< (const T& t)
00643 {
00644                                    // do the work that is common to
00645                                    // the operator<< functions
00646   print (t);
00647   return *this;
00648 }
00649 
00650 
00651 
00652 inline
00653 LogStream &
00654 LogStream::operator<< (const double t)
00655 {
00656                                    // we have to make sure that we
00657                                    // don't catch NaN's and +-Inf's
00658                                    // with the test, because for these
00659                                    // denormals all comparisons are
00660                                    // always false. thus, for a NaN,
00661                                    // both t<=0 and t>=0 are false at
00662                                    // the same time, which can't be
00663                                    // said for any other number
00664   if (! (t<=0) && !(t>=0))
00665     print (t);
00666   else
00667     if (std::fabs(t) < double_threshold)
00668       print ('0');
00669     else
00670       print(t*(1.+offset));
00671 
00672   return *this;
00673 }
00674 
00675 
00676 
00677 inline
00678 LogStream &
00679 LogStream::operator<< (const float t)
00680 {
00681                                    // we have to make sure that we
00682                                    // don't catch NaN's and +-Inf's
00683                                    // with the test, because for these
00684                                    // denormals all comparisons are
00685                                    // always false. thus, for a NaN,
00686                                    // both t<=0 and t>=0 are false at
00687                                    // the same time, which can't be
00688                                    // said for any other number
00689   if (! (t<=0) && !(t>=0))
00690     print (t);
00691   else
00692     if (std::fabs(t) < float_threshold)
00693       print ('0');
00694     else
00695       print(t*(1.+offset));
00696 
00697   return *this;
00698 }
00699 
00700 
00701 inline
00702 LogStream::Prefix::Prefix(const std::string& text, LogStream& s)
00703                 :
00704                 stream(&s)
00705 {
00706   stream->push(text);
00707 }
00708 
00709 
00710 inline
00711 LogStream::Prefix::~Prefix()
00712 {
00713   stream->pop();
00714 }
00715 
00716 
00717 
00723 extern LogStream deallog;
00724 
00725 
00726 inline
00727 LogStream::Prefix::Prefix(const std::string& text)
00728                 :
00729                 stream(&deallog)
00730 {
00731   stream->push(text);
00732 }
00733 
00734 
00735 
00736 DEAL_II_NAMESPACE_CLOSE
00737 
00738 #endif
00739 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

deal.II documentation generated on Tue May 22 2012 12:06:05 by doxygen 1.7.3