LCOV - code coverage report
Current view: top level - src - ProgressBar.cpp (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 39 50 78.0 %
Date: 2024-04-29 14:43:01 Functions: 4 5 80.0 %

          Line data    Source code
       1             : #include "crpropa/ProgressBar.h"
       2             : 
       3             : #include <cstdio>
       4             : #include <iostream>
       5             : 
       6             : namespace crpropa {
       7             : 
       8             : /// Initialize a ProgressBar with [steps] number of steps, updated at [updateSteps] intervalls
       9           5 : ProgressBar::ProgressBar(unsigned long steps, unsigned long updateSteps) :
      10           5 :                 _steps(steps), _currentCount(0), _maxbarLength(10), _updateSteps(
      11           5 :                                 updateSteps), _nextStep(1), _startTime(0) {
      12           5 :         if (_updateSteps > _steps)
      13           3 :                 _updateSteps = _steps;
      14           5 :         arrow.append(">");
      15           5 : }
      16             : 
      17           1 : void ProgressBar::start(const std::string &title) {
      18           1 :         _startTime = time(NULL);
      19           1 :         std::string s = ctime(&_startTime);
      20           1 :         s.erase(s.end() - 1, s.end());
      21           1 :         stringTmpl = "  Started ";
      22             :         stringTmpl.append(s);
      23           1 :         stringTmpl.append(" : [%-10s] %3i%%    %s: %02i:%02i:%02i %s\r");
      24             :         std::cout << title << std::endl;
      25             : 
      26           1 : }
      27             : /// update the progressbar
      28             : /// should be called steps times in a loop
      29         100 : void ProgressBar::update() {
      30         100 :         _currentCount++;
      31         100 :         if (_currentCount == _nextStep || _currentCount == _steps
      32           0 :                         || _currentCount == 1000) {
      33         100 :                                 _nextStep += long(_steps / float(_updateSteps));
      34         100 :                         setPosition(_currentCount);
      35             :                         }
      36         100 : }
      37             : 
      38         100 : void ProgressBar::setPosition(unsigned long position) {
      39         100 :         int percentage = int(100 * (position / float(_steps)));
      40         100 :         time_t currentTime = time(NULL);
      41         100 :         if (position < _steps) {
      42          99 :                 if (arrow.size() <= (_maxbarLength) * (position) / (_steps))
      43           9 :                         arrow.insert(0, "=");
      44          99 :                 float tElapsed = currentTime - _startTime;
      45          99 :                 float tToGo = (_steps - position) * tElapsed / position;
      46          99 :                 std::printf(stringTmpl.c_str(), arrow.c_str(), percentage, "Finish in",
      47          99 :                                 int(tToGo / 3600), (int(tToGo) % 3600) / 60,
      48          99 :                                 int(tToGo) % 60, "");
      49          99 :                 fflush(stdout);
      50             :         } else {
      51           1 :                 float tElapsed = currentTime - _startTime;
      52           1 :                 std::string s = " - Finished at ";
      53           1 :                 s.append(ctime(&currentTime));
      54             :                 char fs[255];
      55             :                 std::sprintf(fs, "%c[%d;%dm Finished %c[%dm", 27, 1, 32, 27, 0);
      56           1 :                 std::printf(stringTmpl.c_str(), fs, 100, "Needed",
      57           1 :                                 int(tElapsed / 3600), (int(tElapsed) % 3600) / 60,
      58           1 :                                 int(tElapsed) % 60, s.c_str());
      59             :         }
      60         100 : }
      61             : 
      62             : 
      63             : /// Mark the progressbar with an error
      64           0 : void ProgressBar::setError() {
      65           0 :         time_t currentTime = time(NULL);
      66           0 :         _currentCount++;
      67           0 :         float tElapsed = currentTime - _startTime;
      68           0 :         std::string s = " - Finished at ";
      69           0 :         s.append(ctime(&currentTime));
      70             :         char fs[255];
      71             :         std::sprintf(fs, "%c[%d;%dm  ERROR   %c[%dm", 27, 1, 31, 27, 0);
      72           0 :         std::printf(stringTmpl.c_str(), fs, _currentCount, "Needed",
      73           0 :                         int(tElapsed / 3600), (int(tElapsed) % 3600) / 60,
      74           0 :                         int(tElapsed) % 60, s.c_str());
      75           0 : }
      76             : 
      77             : } // namespace crpropa

Generated by: LCOV version 1.14