Joedb 10.3.2
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Progress_Bar.cpp
Go to the documentation of this file.
2
3#include <sstream>
4#include <iomanip>
5
6namespace joedb
7{
8 //////////////////////////////////////////////////////////////////////////
9 void Progress_Bar::print_progress() noexcept
10 //////////////////////////////////////////////////////////////////////////
11 {
12 try
13 {
14 if (done > printed)
15 {
16 const auto now = std::chrono::steady_clock::now();
17 std::chrono::duration<double> duration_since_last = now - last_print_time;
18 if (done == total || duration_since_last.count() >= gap)
19 {
20 std::chrono::duration<double> duration_since_start = now - start;
21 last_print_time = now;
22
23 const double duration = duration_since_start.count();
24
25 {
26 std::ostringstream line;
27
28 line << std::fixed << std::setprecision(1);
29 const int width = int(std::to_string(total).size());
30 line << std::setw(width) << done << " / " << total << ' ';
31 line << std::setw(5) << 100.0f*float(done)/float(total) << '%';
32
33 if (done > 0)
34 {
35 line << "; ";
36
37 if (duration > 0.0)
38 {
39 double n = double(done) / duration;
40 char unit = ' ';
41
42 if (n > 1000000.0)
43 {
44 n /= 1000000.0;
45 unit = 'M';
46 }
47 else if (n > 1000.0)
48 {
49 n /= 1000.0;
50 unit = 'k';
51 }
52
53 line << std::fixed << std::setprecision(1) << n << unit << "/s";
54 }
55
56 if (done < total)
57 {
58 const double time_left = double(total - done) * duration / double(done);
59 line << "; " << time_left << "s left";
60 }
61 }
62
63 logger.log(line.str());
64 }
65
66 printed = done;
67 gap *= 1.1;
68 if (gap > 10.0)
69 gap = 10.0;
70 }
71 }
72 }
73 catch (...)
74 {
75 }
76 }
77
78 //////////////////////////////////////////////////////////////////////////
80 //////////////////////////////////////////////////////////////////////////
81 (
82 const int64_t total,
83 Logger &logger
84 ):
85 total(total),
86 done(0),
87 printed(0),
88 gap(1.0),
89 logger(logger),
90 start(std::chrono::steady_clock::now()),
91 last_print_time(start)
92 {
93 }
94
95 //////////////////////////////////////////////////////////////////////////
96 void Progress_Bar::print(const int64_t current)
97 //////////////////////////////////////////////////////////////////////////
98 {
99 done = current;
100 print_progress();
101 }
102
103 //////////////////////////////////////////////////////////////////////////
105 //////////////////////////////////////////////////////////////////////////
106 {
107 print_progress();
108 }
109}
virtual void log(const std::string &message) noexcept
Definition Logger.h:12
void print(int64_t current)
Progress_Bar(int64_t total, Logger &logger)