Reference documentation for deal.II version 8.5.1
logstream.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__logstream_h
17 #define dealii__logstream_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 #include <deal.II/base/smartpointer.h>
22 #include <deal.II/base/std_cxx11/shared_ptr.h>
23 #include <deal.II/base/thread_local_storage.h>
24 
25 #include <string>
26 #include <stack>
27 #include <map>
28 #include <cmath>
29 #include <sstream>
30 
31 #ifdef DEAL_II_HAVE_SYS_TIMES_H
32 # include <sys/times.h>
33 #else
34 struct tms
35 {
36  int tms_utime, tms_stime, tms_cutime, tms_cstime;
37 };
38 #endif
39 
40 
41 DEAL_II_NAMESPACE_OPEN
42 
113 class LogStream : public Subscriptor
114 {
115 public:
133  class Prefix
134  {
135  public:
140  Prefix(const std::string &text);
141 
146  Prefix(const std::string &text,
147  LogStream &stream);
148 
152  ~Prefix ();
153 
154  private:
156  };
157 
158 
164  LogStream ();
165 
166 
170  ~LogStream();
171 
172 
178  void attach (std::ostream &o,
179  const bool print_job_id = true);
180 
181 
186  void detach ();
187 
188 
199  void test_mode (const bool on = true,
200  const double double_threshold = 1.e-10,
201  const float float_threshold = 1.e-7f,
202  const double offset = 1.e-7);
203 
204 
208  std::ostream &get_console ();
209 
210 
214  std::ostream &get_file_stream ();
215 
216 
220  bool has_file () const;
221 
222 
227  void log_cerr ();
228 
229 
233  const std::string &get_prefix () const;
234 
235 
243  void push (const std::string &text);
244 
245 
249  void pop ();
250 
251 
262  unsigned int depth_console (const unsigned int n);
263 
264 
272  unsigned int depth_file (const unsigned int n);
273 
274 
281  bool log_execution_time (const bool flag);
282 
283 
296  bool log_time_differences (const bool flag);
297 
298 
302  void timestamp();
303 
304 
308  bool log_thread_id (const bool flag);
309 
310 
329  void threshold_double(const double t);
330 
331 
335  void threshold_float(const float t);
336 
337 
343  std::streamsize precision (const std::streamsize prec);
344 
345 
351  std::streamsize width (const std::streamsize wide);
352 
353 
359  std::ios::fmtflags flags(const std::ios::fmtflags f);
360 
361 
368  LogStream &operator << (const double t);
369 
370 
377  LogStream &operator << (const float t);
378 
379 
394  LogStream &operator<< (std::ostream& (*p) (std::ostream &));
395 
396 
402  std::size_t memory_consumption () const;
403 
404 private:
405 
406 
414  std::stack<std::string> &get_prefixes() const;
415 
421 
427  std::ostream *std_out;
428 
435  std::ostream *file;
436 
443  unsigned int std_depth;
444 
448  unsigned int file_depth;
449 
454 
459 
463  double last_time;
464 
470 
476 
494  double offset;
495 
500 
505 
509  struct tms reference_tms;
510 
516  std::streambuf *old_cerr;
517 
522 
527  void print_line_head ();
528 
533  std::ostringstream &get_stream();
534 
540 
541  template <typename T> friend LogStream &operator << (LogStream &log, const T &t);
542 };
543 
544 
545 /* ----------------------------- Inline functions and templates ---------------- */
546 
547 
555 template <typename T>
556 inline
558 {
559  // print to the internal stringstream
560  log.get_stream() << t;
561  return log;
562 }
563 
564 
565 inline
566 std::ostringstream &
568 {
569  // see if we have already created this stream. if not, do so and
570  // set the default flags (why we set these flags is lost to
571  // history, but this is what we need to keep several hundred tests
572  // from producing different output)
573  //
574  // note that in all of this we need not worry about thread-safety
575  // because we operate on a thread-local object and by definition
576  // there can only be one access at a time
577  if (outstreams.get().get() == 0)
578  {
579  outstreams.get().reset (new std::ostringstream);
580  outstreams.get()->setf(std::ios::showpoint | std::ios::left);
581  }
582 
583  // then return the stream
584  return *outstreams.get();
585 }
586 
587 
588 
589 
590 inline
591 LogStream &
592 LogStream::operator<< (const double t)
593 {
594  std::ostringstream &stream = get_stream();
595 
596  // drop small numbers or skew them away from zero.
597  // we have to make sure that we don't catch NaN's and +-Inf's with the
598  // test, because for these denormals all comparisons are always false.
599  if (! numbers::is_finite(t))
600  stream << t;
601  else if (std::fabs(t) < double_threshold)
602  stream << '0';
603  else
604  stream << t*(1.+offset);
605 
606  return *this;
607 }
608 
609 
610 
611 inline
612 LogStream &
614 {
615  std::ostringstream &stream = get_stream();
616 
617  // we have to make sure that we don't catch NaN's and +-Inf's with the
618  // test, because for these denormals all comparisons are always false.
619  // thus, for a NaN, both t<=0 and t>=0 are false at the same time, which
620  // can't be said for any other number
621  if (! (t<=0) && !(t>=0))
622  stream << t;
623  else if (std::fabs(t) < float_threshold)
624  stream << '0';
625  else
626  stream << t*(1.+offset);
627 
628  return *this;
629 }
630 
631 
632 inline
633 LogStream::Prefix::Prefix(const std::string &text, LogStream &s)
634  :
635  stream(&s)
636 {
637  stream->push(text);
638 }
639 
640 
641 inline
643 {
644  stream->pop();
645 }
646 
647 
653 extern LogStream deallog;
654 
655 
656 inline
657 LogStream::Prefix::Prefix(const std::string &text)
658  :
659  stream(&deallog)
660 {
661  stream->push(text);
662 }
663 
664 
665 DEAL_II_NAMESPACE_CLOSE
666 
667 #endif
void pop()
Definition: logstream.cc:306
float float_threshold
Definition: logstream.h:475
std::streamsize precision(const std::streamsize prec)
Definition: logstream.cc:321
void timestamp()
Definition: logstream.cc:490
bool print_utime
Definition: logstream.h:453
Threads::ThreadLocalStorage< std_cxx11::shared_ptr< std::ostringstream > > outstreams
Definition: logstream.h:539
VectorizedArray< Number > log(const ::VectorizedArray< Number > &x)
A class that provides a separate storage location on each thread that accesses the object...
std::ostream * std_out
Definition: logstream.h:427
Threads::ThreadLocalStorage< std::stack< std::string > > prefixes
Definition: logstream.h:420
const std::string & get_prefix() const
Definition: logstream.cc:282
double offset
Definition: logstream.h:494
bool print_thread_id
Definition: logstream.h:499
void threshold_double(const double t)
Definition: logstream.cc:355
unsigned int depth_file(const unsigned int n)
Definition: logstream.cc:345
unsigned int std_depth
Definition: logstream.h:443
std::streamsize width(const std::streamsize wide)
Definition: logstream.cc:328
bool log_thread_id(const bool flag)
Definition: logstream.cc:391
std::ostream & get_file_stream()
Definition: logstream.cc:265
bool is_finite(const double x)
Definition: numbers.h:275
std::streambuf * old_cerr
Definition: logstream.h:516
std::ios::fmtflags flags(const std::ios::fmtflags f)
Definition: logstream.cc:314
LogStream & operator<<(const double t)
Definition: logstream.h:592
bool has_file() const
Definition: logstream.cc:275
~LogStream()
Definition: logstream.cc:73
double last_time
Definition: logstream.h:463
void test_mode(const bool on=true, const double double_threshold=1.e-10, const float float_threshold=1.e-7f, const double offset=1.e-7)
Definition: logstream.cc:113
std::stack< std::string > & get_prefixes() const
Definition: logstream.cc:400
bool diff_utime
Definition: logstream.h:458
void log_cerr()
Definition: logstream.cc:242
void threshold_float(const float t)
Definition: logstream.cc:363
bool log_time_differences(const bool flag)
Definition: logstream.cc:381
unsigned int depth_console(const unsigned int n)
Definition: logstream.cc:335
void print_line_head()
Definition: logstream.cc:433
std::ostream * file
Definition: logstream.h:435
double double_threshold
Definition: logstream.h:469
void attach(std::ostream &o, const bool print_job_id=true)
Definition: logstream.cc:224
Prefix(const std::string &text)
Definition: logstream.h:657
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:154
std::ostream & get_console()
Definition: logstream.cc:258
double reference_time_val
Definition: logstream.h:504
bool at_newline
Definition: logstream.h:521
bool log_execution_time(const bool flag)
Definition: logstream.cc:371
void push(const std::string &text)
Definition: logstream.cc:294
void detach()
Definition: logstream.cc:235
std::ostringstream & get_stream()
Definition: logstream.h:567
unsigned int file_depth
Definition: logstream.h:448
struct tms reference_tms
Definition: logstream.h:509
std::size_t memory_consumption() const
Definition: logstream.cc:514