DOLFIN
DOLFIN C++ interface
Logger.h
1 // Copyright (C) 2003-2016 Anders Logg, 2015 Jan Blechta
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Thanks to Jim Tilander for many helpful hints.
19 //
20 // Modified by Ola Skavhaug 2007, 2009
21 
22 #ifndef __LOGGER_H
23 #define __LOGGER_H
24 
25 #include <map>
26 #include <memory>
27 #include <ostream>
28 #include <string>
29 #include <set>
30 #include <thread>
31 #include <tuple>
32 
33 #include <dolfin/common/timing.h>
34 #include <dolfin/common/MPI.h>
35 #include "Table.h"
36 #include "LogLevel.h"
37 
38 namespace dolfin
39 {
40 
42 
43  class Logger
44  {
45  public:
46 
48  Logger();
49 
51  ~Logger();
52 
54  void log(std::string msg, int log_level=INFO) const;
55 
57  void log_underline(std::string msg, int log_level=INFO) const;
58 
60  void warning(std::string msg) const;
61 
63  void error(std::string msg) const;
64 
67  void dolfin_error(std::string location,
68  std::string task,
69  std::string reason,
70  int mpi_rank=-1) const;
71 
73  void deprecation(std::string feature,
74  std::string version_deprecated,
75  std::string message) const;
76 
78  void begin(std::string msg, int log_level=INFO);
79 
81  void end();
82 
84  void progress (std::string title, double p) const;
85 
87  void set_output_stream(std::ostream& stream);
88 
90  std::ostream& get_output_stream() { return *logstream; }
91 
93  void set_log_active(bool active);
94 
96  inline bool is_active() { return _active; }
97 
99  void set_log_level(int log_level);
100 
102  inline int get_log_level() const { return _log_level; }
103 
105  void set_indentation_level(std::size_t indentation_level);
106 
108  void register_timing(std::string task,
109  std::tuple<double, double, double> elapsed);
110 
113  Table timings(TimingClear clear, std::set<TimingType> type);
114 
118  void list_timings(TimingClear clear, std::set<TimingType> type);
119 
124  void dump_timings_to_xml(std::string filename, TimingClear clear);
125 
129  std::tuple<std::size_t, double, double, double>
130  timing(std::string task, TimingClear clear);
131 
135  void monitor_memory_usage();
136 
138  MPI_Comm mpi_comm()
139  { return _mpi_comm; }
140 
142  void _report_memory_usage(size_t num_mb);
143 
145  void __debug(std::string msg) const;
146 
148  void __dolfin_assert(std::string file, unsigned long line,
149  std::string function, std::string check) const;
150 
151  private:
152 
153  // Write message
154  void write(int log_level, std::string msg) const;
155 
156  // True iff logging is active
157  bool _active;
158 
159  // Current log level
160  int _log_level;
161 
162  // Current indentation level
163  int _indentation_level;
164 
165  // Optional stream for logging
166  std::ostream* logstream;
167 
168  // List of timings for tasks, map from string to
169  // (num_timings, total_wall_time, total_user_time, total_system_time)
170  std::map<std::string, std::tuple<std::size_t, double, double, double>>
171  _timings;
172 
173  // Thread used for monitoring memory usage
174  std::unique_ptr<std::thread> _thread_monitor_memory_usage;
175 
176  // Maximum memory usage so far
177  long int _maximum_memory_usage;
178 
179  // Map for stringifying TimingType
180  static std::map<TimingType, std::string> _TimingType_descr;
181 
182  // FIXME: This should be a dolfin::MPI::Comm, the MPI-awareness of
183  // the logging needs to be fixed.
184  // MPI Communicator
185  MPI_Comm _mpi_comm;
186 
187  };
188 
189 }
190 
191 #endif
void error(std::string msg) const
Print error message and throw exception.
Definition: Logger.cpp:136
Definition: Table.h:49
void warning(std::string msg) const
Print warning.
Definition: Logger.cpp:130
void progress(std::string title, double p) const
Draw progress bar.
Definition: Logger.cpp:224
void __dolfin_assert(std::string file, unsigned long line, std::string function, std::string check) const
Helper function for dolfin_dolfin_assert macro.
Definition: Logger.cpp:429
Handling of error messages, logging and informational display.
Definition: Logger.h:43
void log_underline(std::string msg, int log_level=INFO) const
Print underlined message.
Definition: Logger.cpp:114
Definition: adapt.h:29
void set_log_level(int log_level)
Set log level.
Definition: Logger.cpp:256
void dolfin_error(std::string location, std::string task, std::string reason, int mpi_rank=-1) const
Definition: Logger.cpp:142
Logger()
Constructor.
Definition: Logger.cpp:95
void register_timing(std::string task, std::tuple< double, double, double > elapsed)
Register timing (for later summary)
Definition: Logger.cpp:266
void __debug(std::string msg) const
Helper function for dolfin_debug macro.
Definition: Logger.cpp:423
void set_output_stream(std::ostream &stream)
Set output stream.
Definition: Logger.cpp:246
int get_log_level() const
Get log level.
Definition: Logger.h:102
void begin(std::string msg, int log_level=INFO)
Begin task (increase indentation level)
Definition: Logger.cpp:212
void deprecation(std::string feature, std::string version_deprecated, std::string message) const
Issue deprecation warning for removed feature.
Definition: Logger.cpp:188
void end()
End task (decrease indentation level)
Definition: Logger.cpp:219
void dump_timings_to_xml(std::string filename, TimingClear clear)
Definition: Logger.cpp:317
~Logger()
Destructor.
Definition: Logger.cpp:102
bool is_active()
Return true iff logging is active.
Definition: Logger.h:96
std::ostream & get_output_stream()
Get output stream.
Definition: Logger.h:90
MPI_Comm mpi_comm()
Return MPI Communicator of Logger.
Definition: Logger.h:138
std::tuple< std::size_t, double, double, double > timing(std::string task, TimingClear clear)
Definition: Logger.cpp:371
void set_indentation_level(std::size_t indentation_level)
Set indentation level.
Definition: Logger.cpp:261
Table timings(TimingClear clear, std::set< TimingType > type)
Definition: Logger.cpp:340
void log(std::string msg, int log_level=INFO) const
Print message.
Definition: Logger.cpp:109
void list_timings(TimingClear clear, std::set< TimingType > type)
Definition: Logger.cpp:297
void _report_memory_usage(size_t num_mb)
Helper function for reporting memory usage.
Definition: Logger.cpp:414
void set_log_active(bool active)
Turn logging on or off.
Definition: Logger.cpp:251
void monitor_memory_usage()
Definition: Logger.cpp:393
TimingClear
Definition: timing.h:33