Reference documentation for deal.II version 8.1.0
logstream.h
1 // ---------------------------------------------------------------------
2 // @f$Id: logstream.h 31932 2013-12-08 02:15:54Z heister @f$
3 //
4 // Copyright (C) 1998 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__logstream_h
18 #define __deal2__logstream_h
19 
20 #include <deal.II/base/config.h>
22 #include <deal.II/base/smartpointer.h>
23 #include <deal.II/base/std_cxx1x/shared_ptr.h>
24 #include <deal.II/base/thread_local_storage.h>
25 
26 #include <string>
27 #include <stack>
28 #include <map>
29 #include <cmath>
30 #include <sstream>
31 
32 #ifdef HAVE_SYS_TIMES_H
33 # include <sys/times.h>
34 #else
35 struct tms
36 {
37  int tms_utime, tms_stime, tms_cutime, tms_cstime;
38 };
39 #endif
40 
41 
43 
117 class LogStream : public Subscriptor
118 {
119 public:
137  class Prefix
138  {
139  public:
144  Prefix(const std::string &text);
145 
150  Prefix(const std::string &text,
151  LogStream &stream);
152 
156  ~Prefix ();
157 
158  private:
160  };
161 
162 
168  LogStream ();
169 
170 
174  ~LogStream();
175 
176 
182  void attach (std::ostream &o,
183  const bool print_job_id = true);
184 
185 
191  void detach ();
192 
193 
204  void test_mode (bool on=true);
205 
206 
210  std::ostream &get_console ();
211 
212 
216  std::ostream &get_file_stream ();
217 
218 
222  bool has_file () const;
223 
224 
229  void log_cerr ();
230 
231 
235  const std::string &get_prefix () const;
236 
237 
245  void push (const std::string &text);
246 
247 
251  void pop ();
252 
253 
262  unsigned int depth_console (const unsigned int n);
263 
264 
273  unsigned int depth_file (const unsigned int n);
274 
275 
282  bool log_execution_time (const bool flag);
283 
284 
297  bool log_time_differences (const bool flag);
298 
299 
303  void timestamp();
304 
305 
309  bool log_thread_id (const bool flag);
310 
311 
331  void threshold_double(const double t);
332 
333 
337  void threshold_float(const float t);
338 
339 
345  std::streamsize precision (const std::streamsize prec);
346 
347 
353  std::streamsize width (const std::streamsize wide);
354 
355 
361  std::ios::fmtflags flags(const std::ios::fmtflags f);
362 
363 
370  LogStream &operator << (const double t);
371 
372 
379  LogStream &operator << (const float t);
380 
381 
397  LogStream &operator<< (std::ostream& (*p) (std::ostream &));
398 
399 
407  std::size_t memory_consumption () const;
408 
409 
413  DeclException0(ExcNoFileStreamGiven);
414 
415 private:
416 
417 
425  std::stack<std::string> &get_prefixes() const;
426 
432 
438  std::ostream *std_out;
439 
446  std::ostream *file;
447 
454  unsigned int std_depth;
455 
459  unsigned int file_depth;
460 
465 
470 
474  double last_time;
475 
481 
487 
505  double offset;
506 
511 
516 
520  struct tms reference_tms;
521 
528  std::streambuf *old_cerr;
529 
534 
539  void print_line_head ();
540 
546  std::ostringstream &get_stream();
547 
553 
554  template <typename T> friend LogStream &operator << (LogStream &log, const T &t);
555 };
556 
557 
558 /* ----------------------------- Inline functions and templates ---------------- */
559 
560 
568 template <typename T>
569 inline
570 LogStream &operator<< (LogStream &log, const T &t)
571 {
572  // print to the internal stringstream
573  log.get_stream() << t;
574  return log;
575 }
576 
577 
578 inline
579 std::ostringstream &
581 {
582  // see if we have already created this stream. if not, do so and
583  // set the default flags (why we set these flags is lost to
584  // history, but this is what we need to keep several hundred tests
585  // from producing different output)
586  //
587  // note that in all of this we need not worry about thread-safety
588  // because we operate on a thread-local object and by definition
589  // there can only be one access at a time
590  if (outstreams.get().get() == 0)
591  {
592  outstreams.get().reset (new std::ostringstream);
593  outstreams.get()->setf(std::ios::showpoint | std::ios::left);
594  }
595 
596  // then return the stream
597  return *outstreams.get();
598 }
599 
600 
601 
602 
603 inline
604 LogStream &
605 LogStream::operator<< (const double t)
606 {
607  std::ostringstream &stream = get_stream();
608 
609  // we have to make sure that we don't catch NaN's and +-Inf's with the
610  // test, because for these denormals all comparisons are always false.
611  // thus, for a NaN, both t<=0 and t>=0 are false at the same time, which
612  // can't be said for any other number
613  if (! (t<=0) && !(t>=0))
614  stream << t;
615  else if (std::fabs(t) < double_threshold)
616  stream << '0';
617  else
618  stream << t*(1.+offset);
619 
620  return *this;
621 }
622 
623 
624 
625 inline
626 LogStream &
628 {
629  std::ostringstream &stream = get_stream();
630 
631  // we have to make sure that we don't catch NaN's and +-Inf's with the
632  // test, because for these denormals all comparisons are always false.
633  // thus, for a NaN, both t<=0 and t>=0 are false at the same time, which
634  // can't be said for any other number
635  if (! (t<=0) && !(t>=0))
636  stream << t;
637  else if (std::fabs(t) < float_threshold)
638  stream << '0';
639  else
640  stream << t*(1.+offset);
641 
642  return *this;
643 }
644 
645 
646 inline
647 LogStream::Prefix::Prefix(const std::string &text, LogStream &s)
648  :
649  stream(&s)
650 {
651  stream->push(text);
652 }
653 
654 
655 inline
657 {
658  stream->pop();
659 }
660 
661 
667 extern LogStream deallog;
668 
669 
670 inline
671 LogStream::Prefix::Prefix(const std::string &text)
672  :
673  stream(&deallog)
674 {
675  stream->push(text);
676 }
677 
678 
679 DEAL_II_NAMESPACE_CLOSE
680 
681 #endif
float float_threshold
Definition: logstream.h:486
Threads::ThreadLocalStorage< std_cxx1x::shared_ptr< std::ostringstream > > outstreams
Definition: logstream.h:552
bool print_utime
Definition: logstream.h:464
A class that provides a separate storage location on each thread that accesses the object...
std::ostream * std_out
Definition: logstream.h:438
Threads::ThreadLocalStorage< std::stack< std::string > > prefixes
Definition: logstream.h:431
double offset
Definition: logstream.h:505
bool print_thread_id
Definition: logstream.h:510
unsigned int std_depth
Definition: logstream.h:454
std::streambuf * old_cerr
Definition: logstream.h:528
LogStream & operator<<(const double t)
Definition: logstream.h:605
double last_time
Definition: logstream.h:474
#define DeclException0(Exception0)
Definition: exceptions.h:505
bool diff_utime
Definition: logstream.h:469
std::ostream * file
Definition: logstream.h:446
double double_threshold
Definition: logstream.h:480
Prefix(const std::string &text)
Definition: logstream.h:671
std::ostream & operator<<(std::ostream &os, const Vector< number > &v)
Definition: vector.h:1546
Definition: logstream.h:35
double reference_time_val
Definition: logstream.h:515
bool at_newline
Definition: logstream.h:533
std::ostringstream & get_stream()
Definition: logstream.h:580
unsigned int file_depth
Definition: logstream.h:459