10 #include <framework/modules/core/ProgressBarModule.h>
12 #include <framework/core/Environment.h>
13 #include <framework/utilities/Utils.h>
28 Display a progress bar and an estimate of remaining time when number of
29 events is known (e.g. reading from file, or -n switch used).
31 The progress bar uses stderr for its output, so it works best when stdout
32 is piped to a file. However it should also work when printing direct to a
35 .. versionchanged:: release-03-00-00
36 the module now detects if it outputs to a terminal or into a file and
37 will only update the bar if it has changed and not use any control
38 characters to make log files much more readable than before.
42 void ProgressBarModule::initialize()
45 m_nTotal = Environment::Instance().getNumberOfEvents();
51 m_isTTY = isatty(STDERR_FILENO);
54 void ProgressBarModule::event()
59 double clockSec = Utils::getClock() / 1e9;
63 if (m_startTime == 0) {
65 m_startTime = clockSec;
66 m_lastPrint = m_startTime;
70 if (clockSec - m_lastPrint > 1) {
71 double elapsedSec = clockSec - m_startTime;
72 double ratio = double(m_evtNr) / m_nTotal;
73 double time_per_event = elapsedSec / m_evtNr;
74 auto remainingSeconds = (int)std::round((m_nTotal - m_evtNr) * time_per_event);
76 if (remainingSeconds >= 0) {
77 const int bar_length = 50;
80 if (!m_isTTY &&
int(ratio * bar_length) <= m_progress)
return;
82 m_progress = ratio * bar_length;
85 for (
int i = 0; i < m_progress; ++i) cerr <<
'=';
87 for (int i = m_progress + 1; i < bar_length; ++i) cerr << ' ';
88 cerr << "] " << setw(3) << int(ratio * 100) << "% ";
89 if (remainingSeconds > 3600) {
90 int hours = remainingSeconds / 3600;
91 remainingSeconds %= 3600;
94 if (remainingSeconds > 60) {
95 int minutes = remainingSeconds / 60;
96 remainingSeconds %= 60;
97 cerr << minutes << "m";
99 cerr << remainingSeconds << "s remaining";
100 //some spaces to overwrite other stuff
103 //carriage return to go back to beginning of the line
106 //not a terminal, just make it an output line
109 // make sure it's printed
112 m_lastPrint = clockSec;
119 void ProgressBarModule::terminate()