SIde-Channel Analysis toolKit (SICAK)
Software toolkit for side-channel analysis
global_calls.hpp
Go to the documentation of this file.
1 /*
2 * SICAK - SIde-Channel Analysis toolKit
3 * Copyright (C) 2018 Petr Socha, FIT, CTU in Prague
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 * (at your option) 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 <https://www.gnu.org/licenses/>.
17 */
18 
29 #ifndef GLOBAL_CALLS_HPP
30 #define GLOBAL_CALLS_HPP
31 
32 #include <QElapsedTimer>
33 #include <iostream>
34 
41 class CoutProgress {
42 
43 public:
44 
46  static CoutProgress & get(){
47  static CoutProgress instance;
48  return instance;
49 
50  }
51 
53  void start(size_t workSize){
54  m_workSize = workSize > 1 ? workSize : 1;
55  m_workProgress = 0;
56  m_lastPercentage = 0;
57  m_startTime.start();
58  std::cout << '\r' << "0% done... remaining time not yet available" << std::flush;
59  }
60 
62  void update(size_t workProgress){
63  m_workProgress = workProgress;
64  int percentage = ((float)m_workProgress / (float)m_workSize) * 100.0;
65  if(percentage >= 100) percentage = 99;
66  if(percentage > m_lastPercentage){
67  m_lastPercentage = percentage;
68  std::cout << '\r' << percentage << "% done... approx. ";
69  printFormattedTime((((float)m_startTime.elapsed() / (float)m_workProgress) * (float)(m_workSize - m_workProgress)) / 1000.0);
70  std::cout << " remaining " << std::flush;
71  }
72  }
73 
75  void finish(){
76  m_lastPercentage = 100;
77  std::cout << '\r' << "100% done... ";
78  printFormattedTime((float)m_startTime.elapsed() / 1000.0);
79  std::cout << " elapsed. " << std::endl << std::flush;
80  }
81 
82  static void printFormattedTime(size_t sec){
83  if(!sec){
84  std::cout << "<1s";
85  return;
86  }
87  size_t days = sec / 86400;
88  sec %= 86400;
89  size_t hours = sec / 3600;
90  sec %= 3600;
91  size_t minutes = sec / 60;
92  size_t seconds = sec % 60;
93  if(days) std::cout << days << "d, ";
94  if(days || hours) std::cout << hours << "h, ";
95  if(days || hours || minutes) std::cout << minutes << "m, ";
96  if(days || hours || minutes || seconds) std::cout << seconds << "s";
97  }
98 
99 protected:
100 
101  size_t m_workSize;
102  size_t m_workProgress;
103  int m_lastPercentage;
104  QElapsedTimer m_startTime;
105 
106 private:
107 
108  CoutProgress(): m_workSize(1), m_workProgress(0), m_lastPercentage(0) {}
109  CoutProgress(CoutProgress const&);
110  void operator=(CoutProgress const&);
111 
112 };
113 
114 #endif /* GLOBAL_CALLS_HPP */
static CoutProgress & get()
Singleton instance getter.
Definition: global_calls.hpp:46
A singleton class providing a standard output progress bar, including remaining time estimate.
Definition: global_calls.hpp:41
void start(size_t workSize)
Start displaying the progress bar and define the amout of work that's left to be done.
Definition: global_calls.hpp:53
void update(size_t workProgress)
Update the progress bar, 0 <= workProgress <= workSize.
Definition: global_calls.hpp:62
void finish()
Call when the work is done to stop the progress bar.
Definition: global_calls.hpp:75