Reference documentation for deal.II version 8.4.2
logstream.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2016 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 (bool on=true);
200 
201 
205  std::ostream &get_console ();
206 
207 
211  std::ostream &get_file_stream ();
212 
213 
217  bool has_file () const;
218 
219 
224  void log_cerr ();
225 
226 
230  const std::string &get_prefix () const;
231 
232 
240  void push (const std::string &text);
241 
242 
246  void pop ();
247 
248 
259  unsigned int depth_console (const unsigned int n);
260 
261 
269  unsigned int depth_file (const unsigned int n);
270 
271 
278  bool log_execution_time (const bool flag);
279 
280 
293  bool log_time_differences (const bool flag);
294 
295 
299  void timestamp();
300 
301 
305  bool log_thread_id (const bool flag);
306 
307 
326  void threshold_double(const double t);
327 
328 
332  void threshold_float(const float t);
333 
334 
340  std::streamsize precision (const std::streamsize prec);
341 
342 
348  std::streamsize width (const std::streamsize wide);
349 
350 
356  std::ios::fmtflags flags(const std::ios::fmtflags f);
357 
358 
365  LogStream &operator << (const double t);
366 
367 
374  LogStream &operator << (const float t);
375 
376 
391  LogStream &operator<< (std::ostream& (*p) (std::ostream &));
392 
393 
399  std::size_t memory_consumption () const;
400 
401 
405  DeclException0(ExcNoFileStreamGiven);
406 
407 private:
408 
409 
417  std::stack<std::string> &get_prefixes() const;
418 
424 
430  std::ostream *std_out;
431 
438  std::ostream *file;
439 
446  unsigned int std_depth;
447 
451  unsigned int file_depth;
452 
457 
462 
466  double last_time;
467 
473 
479 
497  double offset;
498 
503 
508 
512  struct tms reference_tms;
513 
519  std::streambuf *old_cerr;
520 
525 
530  void print_line_head ();
531 
536  std::ostringstream &get_stream();
537 
543 
544  template <typename T> friend LogStream &operator << (LogStream &log, const T &t);
545 };
546 
547 
548 /* ----------------------------- Inline functions and templates ---------------- */
549 
550 
558 template <typename T>
559 inline
560 LogStream &operator<< (LogStream &log, const T &t)
561 {
562  // print to the internal stringstream
563  log.get_stream() << t;
564  return log;
565 }
566 
567 
568 inline
569 std::ostringstream &
571 {
572  // see if we have already created this stream. if not, do so and
573  // set the default flags (why we set these flags is lost to
574  // history, but this is what we need to keep several hundred tests
575  // from producing different output)
576  //
577  // note that in all of this we need not worry about thread-safety
578  // because we operate on a thread-local object and by definition
579  // there can only be one access at a time
580  if (outstreams.get().get() == 0)
581  {
582  outstreams.get().reset (new std::ostringstream);
583  outstreams.get()->setf(std::ios::showpoint | std::ios::left);
584  }
585 
586  // then return the stream
587  return *outstreams.get();
588 }
589 
590 
591 
592 
593 inline
594 LogStream &
595 LogStream::operator<< (const double t)
596 {
597  std::ostringstream &stream = get_stream();
598 
599  // drop small numbers or skew them away from zero.
600  // we have to make sure that we don't catch NaN's and +-Inf's with the
601  // test, because for these denormals all comparisons are always false.
602  if (! numbers::is_finite(t))
603  stream << t;
604  else if (std::fabs(t) < double_threshold)
605  stream << '0';
606  else
607  stream << t*(1.+offset);
608 
609  return *this;
610 }
611 
612 
613 
614 inline
615 LogStream &
617 {
618  std::ostringstream &stream = get_stream();
619 
620  // we have to make sure that we don't catch NaN's and +-Inf's with the
621  // test, because for these denormals all comparisons are always false.
622  // thus, for a NaN, both t<=0 and t>=0 are false at the same time, which
623  // can't be said for any other number
624  if (! (t<=0) && !(t>=0))
625  stream << t;
626  else if (std::fabs(t) < float_threshold)
627  stream << '0';
628  else
629  stream << t*(1.+offset);
630 
631  return *this;
632 }
633 
634 
635 inline
636 LogStream::Prefix::Prefix(const std::string &text, LogStream &s)
637  :
638  stream(&s)
639 {
640  stream->push(text);
641 }
642 
643 
644 inline
646 {
647  stream->pop();
648 }
649 
650 
656 extern LogStream deallog;
657 
658 
659 inline
660 LogStream::Prefix::Prefix(const std::string &text)
661  :
662  stream(&deallog)
663 {
664  stream->push(text);
665 }
666 
667 
668 DEAL_II_NAMESPACE_CLOSE
669 
670 #endif
float float_threshold
Definition: logstream.h:478
bool print_utime
Definition: logstream.h:456
Threads::ThreadLocalStorage< std_cxx11::shared_ptr< std::ostringstream > > outstreams
Definition: logstream.h:542
A class that provides a separate storage location on each thread that accesses the object...
std::ostream * std_out
Definition: logstream.h:430
Threads::ThreadLocalStorage< std::stack< std::string > > prefixes
Definition: logstream.h:423
double offset
Definition: logstream.h:497
bool print_thread_id
Definition: logstream.h:502
unsigned int std_depth
Definition: logstream.h:446
bool is_finite(const double x)
Definition: numbers.h:255
std::streambuf * old_cerr
Definition: logstream.h:519
LogStream & operator<<(const double t)
Definition: logstream.h:595
double last_time
Definition: logstream.h:466
#define DeclException0(Exception0)
Definition: exceptions.h:522
bool diff_utime
Definition: logstream.h:461
std::ostream * file
Definition: logstream.h:438
double double_threshold
Definition: logstream.h:472
Prefix(const std::string &text)
Definition: logstream.h:660
double reference_time_val
Definition: logstream.h:507
bool at_newline
Definition: logstream.h:524
std::ostringstream & get_stream()
Definition: logstream.h:570
unsigned int file_depth
Definition: logstream.h:451
StreamType & operator<<(StreamType &s, UpdateFlags u)