autoppl  v0.8
A C++ template library for probabilistic programming
logging.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 
6 namespace ppl {
7 namespace util {
8 
19 {
20 
28  ProgressLogger(size_t max,
29  const std::string& name,
30  std::ostream& os = std::cout)
31  : max_(max)
32  , name_(name)
33  , os_(os)
34  {};
35 
39  ~ProgressLogger() { os_ << std::endl; }
40 
41  void printProgress(size_t step) {
42  ++step;
43  size_t denom = max_ <= 100 ? max_ : 100;
44  if (step % (max_ / denom) == 0) {
45  int percent = static_cast<double>(step * 100) /
46  static_cast<double>(max_);
47  os_ << '\r' << name_ << " ["
48  << std::string(percent, '=')
49  << std::string(100 - percent, ' ')
50  << "] (" << std::setw(2)
51  << percent
52  << "%)" << std::flush;
53  }
54  }
55 
56 private:
57  size_t max_; // maximum vaue to count progress towards
58  std::string name_; // name of the algorithm being measured, printed with progress bar
59  std::ostream& os_; // output stream to print to
60 };
61 
62 } // namespace util
63 } // namespace ppl
ppl::util::ProgressLogger::printProgress
void printProgress(size_t step)
Definition: logging.hpp:41
ppl::util::ProgressLogger::ProgressLogger
ProgressLogger(size_t max, const std::string &name, std::ostream &os=std::cout)
Definition: logging.hpp:28
ppl::util::ProgressLogger
Definition: logging.hpp:19
ppl::util::ProgressLogger::~ProgressLogger
~ProgressLogger()
Definition: logging.hpp:39
ppl
Definition: bounded.hpp:11