CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Log.hh
Go to the documentation of this file.
1 #ifndef CASM_Log
2 #define CASM_Log
3 
4 #include <iostream>
5 #include "casm/external/boost.hh"
6 
7 namespace CASM {
8 
9  class Log {
10 
11  public:
12 
13  static const int none = 0;
14  static const int quiet = 5;
15  static const int standard = 10;
16  static const int verbose = 20;
17  static const int debug = 100;
18 
28  Log(std::ostream &_ostream = std::cout, int _verbosity = standard, bool _show_clock = false);
29 
30  template<int _required_verbosity = standard>
31  void calculate(const std::string &what) {
32  _add<_required_verbosity>("Calculate", what);
33  }
34 
35  template<int _required_verbosity = standard>
36  void construct(const std::string &what) {
37  _add<_required_verbosity>("Construct", what);
38  }
39 
40  template<int _required_verbosity = standard>
41  void generate(const std::string &what) {
42  _add<_required_verbosity>("Generate", what);
43  }
44 
45  template<int _required_verbosity = standard>
46  void set(const std::string &what) {
47  _add<_required_verbosity>("Set", what);
48  }
49 
50  template<int _required_verbosity = standard>
51  void check(const std::string &what) {
52  _add<_required_verbosity>("Check", what);
53  }
54 
55  template<int _required_verbosity = standard>
56  void results(const std::string &what) {
57  _add<_required_verbosity>("Results", what);
58  }
59 
60  template<int _required_verbosity = standard>
61  void read(const std::string &what) {
62  _add<_required_verbosity>("Read", what);
63  }
64 
65  template<int _required_verbosity = standard>
66  void write(const std::string &what) {
67  _add<_required_verbosity>("Write", what);
68  }
69 
70  template<int _required_verbosity = standard>
71  void begin(const std::string &what) {
72  _add<_required_verbosity>("Begin", what);
73  }
74 
75  template<int _required_verbosity = standard>
76  void end(const std::string &what) {
77  _add<_required_verbosity>("End", what);
78  }
79 
80  template<int _required_verbosity = standard>
81  void warning(const std::string &what) {
82  _add<_required_verbosity>("Warning", what);
83  }
84 
85  template<int _required_verbosity = standard>
86  void error(const std::string &what) {
87  _add<_required_verbosity>("Error", what);
88  }
89 
90  template<int _required_verbosity = standard>
91  void compiling(const std::string &what) {
92  _add<_required_verbosity>("Compiling", what);
93  }
94 
95  template<int _required_verbosity = standard>
96  void custom(const std::string &what) {
97  static_assert(_required_verbosity >= none && _required_verbosity <= debug, "CASM::Log _required_verbosity must be <= 100");
98  m_print = (m_verbosity >= _required_verbosity);
99  if(_print()) {
100  *m_stream << "-- " << what << " -- ";
101  _add_time();
102  *m_stream << std::endl;
103  }
104  }
105 
106  template<int _required_verbosity = standard>
107  void custom(const std::string &type, const std::string &what) {
108  _add<_required_verbosity>(type, what);
109  }
110 
111 
112  void restart_clock();
113 
114  void show_clock();
115 
116  void hide_clock();
117 
118  double time_s() const;
119 
120 
121  void begin_lap();
122 
123  double lap_time() const;
124 
125 
126  int verbosity() const;
127 
128  void set_verbosity(int _verbosity);
129 
130  template<int _required_verbosity>
132  static_assert(_required_verbosity >= none && _required_verbosity <= debug, "CASM::Log _required_verbosity must be <= 100");
133  m_print = (m_verbosity >= _required_verbosity);
134  return *this;
135  }
136 
137 
138  void reset(std::ostream &_ostream = std::cout, int _verbosity = standard, bool _show_clock = false);
139 
140 
141  template<typename T>
142  friend Log &operator<<(Log &log, const T &msg_details);
143 
144  friend Log &operator<<(Log &log, std::ostream & (*fptr)(std::ostream &));
145 
146  operator std::ostream &();
147 
148  explicit operator bool () {
149  return m_print;
150  }
151 
153  static std::pair<bool, int> verbosity_level(std::string s);
154 
155 
156  private:
157 
158  template<int _required_verbosity = standard>
159  void _add(const std::string &type, const std::string &what) {
160  static_assert(_required_verbosity >= none && _required_verbosity <= debug, "CASM::Log _required_verbosity must be <= 100");
161  m_print = (m_verbosity >= _required_verbosity);
162  if(_print()) {
163  *m_stream << "-- " << type << ": " << what << " -- ";
164  _add_time();
165  *m_stream << std::endl;
166  }
167  }
168 
169  void _add_time();
170 
171  bool _print() const;
172 
173 
176 
178  bool m_print;
179 
181 
182  boost::chrono::steady_clock::time_point m_start_time;
183 
184  boost::chrono::steady_clock::time_point m_lap_start_time;
185 
186  std::ostream *m_stream;
187 
188  };
189 
190  template<typename T>
191  Log &operator<<(Log &log, const T &msg_details) {
192  if(log._print()) {
193  static_cast<std::ostream &>(log) << msg_details;
194  }
195  return log;
196  }
197 
198  Log &operator<<(Log &log, std::ostream & (*fptr)(std::ostream &));
199 
200 
201  inline Log &default_log() {
202  static Log log;
203  return log;
204  }
205 
206  inline Log &default_err_log() {
207  static Log log(std::cerr);
208  return log;
209  }
210 
211  inline Log &null_log() {
212  static std::ostream nullout(nullptr);
213  static Log log(nullout);
214  return log;
215  }
216 
217  class OStringStreamLog : public Log {
218 
219  public:
220 
229  OStringStreamLog(int _verbosity = standard, bool _show_clock = false) :
230  Log(m_ss, _verbosity, _show_clock) {}
231 
232  std::ostringstream &ss() {
233  return m_ss;
234  };
235 
236  const std::ostringstream &ss() const {
237  return m_ss;
238  };
239 
240  private:
241 
242  std::ostringstream m_ss;
243  };
244 
245 
246  class Logging {
247 
248  public:
249 
251  m_log(&log),
253  m_err_log(&err_log) {}
254 
255  Log &log() const {
256  return *m_log;
257  }
258 
259  Log &debug_log() const {
260  return *m_debug_log;
261  }
262 
263  Log &err_log() const {
264  return *m_err_log;
265  }
266 
267  static Logging null() {
268  return Logging(null_log(), null_log(), null_log());
269  }
270 
271  private:
272 
276 
277  };
278 
279 }
280 
281 #endif
static const int quiet
Definition: Log.hh:14
Log(std::ostream &_ostream=std::cout, int _verbosity=standard, bool _show_clock=false)
Construct a Log.
Definition: Log.cc:5
void results(const std::string &what)
Definition: Log.hh:56
static const int debug
Definition: Log.hh:17
void write(const std::string &what)
Definition: Log.hh:66
boost::chrono::steady_clock::time_point m_start_time
Definition: Log.hh:182
boost::chrono::steady_clock::time_point m_lap_start_time
Definition: Log.hh:184
static const int none
Definition: Log.hh:13
void custom(const std::string &type, const std::string &what)
Definition: Log.hh:107
void show_clock()
Definition: Log.cc:13
void _add(const std::string &type, const std::string &what)
Definition: Log.hh:159
Log * m_debug_log
Definition: Log.hh:274
void begin_lap()
Definition: Log.cc:28
std::ostringstream m_ss
Definition: Log.hh:238
Main CASM namespace.
Definition: complete.cpp:8
bool m_show_clock
Definition: Log.hh:180
Log & log
Definition: settings.cc:105
Log & null_log()
Definition: Log.hh:211
void read(const std::string &what)
Definition: Log.hh:61
Log & log() const
Definition: Log.hh:255
void set(const std::string &what)
Definition: Log.hh:46
double lap_time() const
Definition: Log.cc:32
static std::pair< bool, int > verbosity_level(std::string s)
Read verbosity level from a string.
Definition: Log.cc:66
const std::ostringstream & ss() const
Definition: Log.hh:236
friend Log & operator<<(Log &log, const T &msg_details)
Definition: Log.hh:191
std::ostream * m_stream
Definition: Log.hh:186
void begin(const std::string &what)
Definition: Log.hh:71
void construct(const std::string &what)
Definition: Log.hh:36
void custom(const std::string &what)
Definition: Log.hh:96
static const int verbose
Definition: Log.hh:16
int verbosity() const
Definition: Log.cc:38
void generate(const std::string &what)
Definition: Log.hh:41
Logging(Log &log=default_log(), Log &debug_log=default_log(), Log &err_log=default_err_log())
Definition: Log.hh:250
std::ostream & operator<<(std::ostream &_stream, const FormattedPrintable &_formatted)
static const int standard
Definition: Log.hh:15
Log & debug_log() const
Definition: Log.hh:259
double time_s() const
Definition: Log.cc:21
void calculate(const std::string &what)
Definition: Log.hh:31
void hide_clock()
Definition: Log.cc:17
static Logging null()
Definition: Log.hh:267
void reset(std::ostream &_ostream=std::cout, int _verbosity=standard, bool _show_clock=false)
Definition: Log.cc:47
Log & require()
Definition: Log.hh:131
void _add_time()
Definition: Log.cc:104
void compiling(const std::string &what)
Definition: Log.hh:91
Log & default_log()
Definition: Log.hh:201
std::ostringstream & ss()
Definition: Log.hh:232
void end(const std::string &what)
Definition: Log.hh:76
void set_verbosity(int _verbosity)
Definition: Log.cc:42
Log * m_log
Definition: Log.hh:273
void warning(const std::string &what)
Definition: Log.hh:81
Log & default_err_log()
Definition: Log.hh:206
void error(const std::string &what)
Definition: Log.hh:86
bool m_print
Whether to print.
Definition: Log.hh:178
Definition: Log.hh:9
Log * m_err_log
Definition: Log.hh:275
void check(const std::string &what)
Definition: Log.hh:51
OStringStreamLog(int _verbosity=standard, bool _show_clock=false)
Construct a StringStreamLog.
Definition: Log.hh:229
void restart_clock()
Definition: Log.cc:9
int m_verbosity
If m_verbosity >= required verbosity, then print.
Definition: Log.hh:175
bool _print() const
Definition: Log.cc:110
Log & err_log() const
Definition: Log.hh:263