9 #include <framework/modules/core/ProgressBarModule.h>
11 #include <framework/core/Environment.h>
12 #include <framework/utilities/Utils.h>
27 Display a progress bar and an estimate of remaining time when number of
28 events is known (e.g. reading from file, or -n switch used).
30 The progress bar uses stderr for its output, so it works best when stdout
31 is piped to a file. However it should also work when printing direct to a
34 .. versionchanged:: release-03-00-00
35 the module now detects if it outputs to a terminal or into a file and
36 will only update the bar if it has changed and not use any control
37 characters to make log files much more readable than before.
41 void ProgressBarModule::initialize()
44 m_nTotal = Environment::Instance().getNumberOfEvents();
50 m_isTTY = isatty(STDERR_FILENO);
53 void ProgressBarModule::event()
58 double clockSec = Utils::getClock() / 1e9;
62 if (m_startTime == 0) {
64 m_startTime = clockSec;
65 m_lastPrint = m_startTime;
69 if (clockSec - m_lastPrint > 1) {
70 double elapsedSec = clockSec - m_startTime;
71 double ratio = double(m_evtNr) / m_nTotal;
72 double time_per_event = elapsedSec / m_evtNr;
73 auto remainingSeconds = (int)std::round((m_nTotal - m_evtNr) * time_per_event);
75 if (remainingSeconds >= 0) {
76 const int bar_length = 50;
79 if (!m_isTTY &&
int(ratio * bar_length) <= m_progress)
return;
81 m_progress = ratio * bar_length;
84 for (
int i = 0; i < m_progress; ++i) cerr <<
'=';
86 for (int i = m_progress + 1; i < bar_length; ++i) cerr << ' ';
87 cerr << "] " << setw(3) << int(ratio * 100) << "% ";
88 if (remainingSeconds > 3600) {
89 int hours = remainingSeconds / 3600;
90 remainingSeconds %= 3600;
93 if (remainingSeconds > 60) {
94 int minutes = remainingSeconds / 60;
95 remainingSeconds %= 60;
96 cerr << minutes << "m";
98 cerr << remainingSeconds << "s remaining";
99 //some spaces to overwrite other stuff
102 //carriage return to go back to beginning of the line
105 //not a terminal, just make it an output line
108 // make sure it's printed
111 m_lastPrint = clockSec;
118 void ProgressBarModule::terminate()
Display a progress bar and an estimate of remaining time when number of events is known (e....
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.