Joedb 10.4.3
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 bool Progress_Bar::print_progress() noexcept
10 //////////////////////////////////////////////////////////////////////////
11 {
12 bool actually_printed = false;
13
14 try
15 {
16 if (done > printed)
17 {
18 const auto now = std::chrono::steady_clock::now();
19 std::chrono::duration<double> duration_since_last = now - last_print_time;
20 if (done == total || duration_since_last.count() >= gap)
21 {
22 actually_printed = true;
23 std::chrono::duration<double> duration_since_start = now - start;
24 last_print_time = now;
25
26 const double duration = duration_since_start.count();
27
28 {
29 std::ostringstream line;
30
31 line << std::fixed << std::setprecision(1);
32 const int width = int(std::to_string(total).size());
33 line << std::setw(width) << done << " / " << total << ' ';
34 line << std::setw(5) << 100.0f*float(done)/float(total) << '%';
35
36 if (done > 0)
37 {
38 line << "; ";
39
40 if (duration > 0.0)
41 {
42 double n = double(done) / duration;
43 char unit = ' ';
44
45 if (n > 1000000.0)
46 {
47 n /= 1000000.0;
48 unit = 'M';
49 }
50 else if (n > 1000.0)
51 {
52 n /= 1000.0;
53 unit = 'k';
54 }
55
56 line << std::fixed << std::setprecision(1) << n << unit << "/s";
57 }
58
59 if (done < total)
60 {
61 const double time_left = double(total - done) * duration / double(done);
62 line << "; " << time_left << "s left";
63 }
64 }
65
66 logger.log(line.str());
67 }
68
69 printed = done;
70 gap *= 1.1;
71 if (gap > 10.0)
72 gap = 10.0;
73 }
74 }
75 }
76 catch (...)
77 {
78 }
79
80 return actually_printed;
81 }
82
83 //////////////////////////////////////////////////////////////////////////
85 //////////////////////////////////////////////////////////////////////////
86 (
87 const int64_t total,
88 Logger &logger
89 ):
90 total(total),
91 done(0),
92 printed(0),
93 gap(1.0),
94 logger(logger),
95 start(std::chrono::steady_clock::now()),
96 last_print_time(start)
97 {
98 }
99
100 //////////////////////////////////////////////////////////////////////////
101 bool Progress_Bar::print(const int64_t current)
102 //////////////////////////////////////////////////////////////////////////
103 {
104 done = current;
105 return print_progress();
106 }
107
108 //////////////////////////////////////////////////////////////////////////
110 //////////////////////////////////////////////////////////////////////////
111 {
112 print_progress();
113 }
114}
virtual void log(beman::cstring_view message) noexcept
Definition Logger.h:12
Progress_Bar(int64_t total, Logger &logger)
bool print(int64_t current)
Definition ids.h:50