CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Log.cc
Go to the documentation of this file.
1 #include "casm/casm_io/Log.hh"
2 
3 namespace CASM {
4 
5  Log::Log(std::ostream &_ostream, int _verbosity, bool _show_clock) {
6  reset(_ostream, _verbosity, _show_clock);
7  }
8 
10  m_start_time = boost::chrono::steady_clock::now();
11  }
12 
13  void Log::show_clock() {
14  m_show_clock = true;
15  }
16 
17  void Log::hide_clock() {
18  m_show_clock = false;
19  }
20 
21  double Log::time_s() const {
22  using namespace boost::chrono;
23  auto curr_time = steady_clock::now();
24  return duration_cast<duration<double> >(curr_time - m_start_time).count();
25  }
26 
27 
28  void Log::begin_lap() {
29  m_lap_start_time = boost::chrono::steady_clock::now();
30  }
31 
32  double Log::lap_time() const {
33  using namespace boost::chrono;
34  auto curr_time = steady_clock::now();
35  return duration_cast<duration<double> >(curr_time - m_lap_start_time).count();
36  }
37 
38  int Log::verbosity() const {
39  return m_verbosity;
40  }
41 
42  void Log::set_verbosity(int _verbosity) {
43  m_verbosity = _verbosity;
44  }
45 
46 
47  void Log::reset(std::ostream &_ostream, int _verbosity, bool _show_clock) {
48  m_verbosity = _verbosity;
49  m_print = true;
50  m_show_clock = _show_clock;
51  m_start_time = boost::chrono::steady_clock::now();
52  m_stream = &_ostream;
53  }
54 
55 
56  Log::operator std::ostream &() {
57  return *m_stream;
58  }
59 
66  std::pair<bool, int> Log::verbosity_level(std::string s) {
67 
68  auto is_int = [](std::string s) {
69  int val;
70  if(s.empty() || !isdigit(s[0])) {
71  return std::make_pair(false, val);
72  }
73  char *p;
74  val = strtol(s.c_str(), &p, 10);
75  return std::make_pair(*p == 0 && val >= 0 && val <= 100, val);
76  };
77 
78  auto res = is_int(s);
79  if(res.first) {
80  return res;
81  }
82  else if(s == "none") {
83  return std::make_pair(true, 0);
84  }
85  else if(s == "quiet") {
86  return std::make_pair(true, 5);
87  }
88  else if(s == "standard") {
89  return std::make_pair(true, 10);
90  }
91  else if(s == "verbose") {
92  return std::make_pair(true, 20);
93  }
94  else if(s == "debug") {
95  return std::make_pair(true, 100);
96  }
97  else {
98  return std::make_pair(false, 0);
99  }
100 
101  };
102 
103 
104  void Log::_add_time() {
105  if(m_show_clock) {
106  std::cout << "Time: " << time_s() << " (s)";
107  }
108  }
109 
110  bool Log::_print() const {
111  return m_print;
112  }
113 
114 
115  Log &operator<<(Log &log, std::ostream & (*fptr)(std::ostream &)) {
116  if(log._print()) {
117  fptr(static_cast<std::ostream &>(log));
118  }
119  return log;
120  }
121 
122 }
Log(std::ostream &_ostream=std::cout, int _verbosity=standard, bool _show_clock=false)
Construct a Log.
Definition: Log.cc:5
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
void show_clock()
Definition: Log.cc:13
void begin_lap()
Definition: Log.cc:28
Main CASM namespace.
Definition: complete.cpp:8
bool m_show_clock
Definition: Log.hh:180
Log & log
Definition: settings.cc:105
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
friend Log & operator<<(Log &log, const T &msg_details)
Definition: Log.hh:191
std::ostream * m_stream
Definition: Log.hh:186
int verbosity() const
Definition: Log.cc:38
double time_s() const
Definition: Log.cc:21
void hide_clock()
Definition: Log.cc:17
void reset(std::ostream &_ostream=std::cout, int _verbosity=standard, bool _show_clock=false)
Definition: Log.cc:47
void _add_time()
Definition: Log.cc:104
void set_verbosity(int _verbosity)
Definition: Log.cc:42
bool m_print
Whether to print.
Definition: Log.hh:178
Definition: Log.hh:9
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