cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
ProgressBar.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2023 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program 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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <cmath>
20
22#include "CepGen/Utils/Logger.h"
24#include "CepGen/Utils/String.h"
25#include "CepGen/Utils/Timer.h"
26
27namespace cepgen {
28 namespace utils {
29 ProgressBar::ProgressBar(size_t tot, size_t freq)
30 : tmr_(new Timer),
31 bar_length_(std::stoi(env::get("COLUMNS", "60")) - 10),
32 bar_pattern_(bar_length_, '='),
33 enabled_(env::get("CG_CI").empty() && Logger::get().isTTY()),
34 total_(tot),
35 frequency_(freq) {}
36
38 const std::string message = format("[Finished in %g s]", tmr_->elapsed());
39 fprintf(stderr, "\r%s%.*s%*s\n", message.data(), 0, "", (int)bar_length_, "");
40 fflush(stderr);
41 }
42
43 void ProgressBar::update(size_t iter) const {
44 if (!enabled_)
45 return;
46 const size_t percent = iter * 100. / total_;
47 if (percent % frequency_ == 0 || iter == total_) {
48 int lpad = int(percent / 100. * bar_length_);
49 int rpad = bar_length_ - lpad;
50 fprintf(stderr, "\r%3zu%% [%.*s%*s]", percent, lpad, bar_pattern_.c_str(), rpad, "");
51 fflush(stderr);
52 }
53 }
54 } // namespace utils
55} // namespace cepgen
General purposes logger.
Definition Logger.h:30
ProgressBar(size_t tot, size_t freq=10)
Progress bar constructor.
void update(size_t iter) const
Broadcast the current progress to the bar.
A generic timer to extract the processing time between two steps in this software's flow.
Definition Timer.h:30
std::string format(const std::string &fmt, Args... args)
Format a string using a printf style format descriptor.
Definition String.h:61
Common namespace for this Monte Carlo generator.