Joedb 10.3.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Progress_Bar.cpp
Go to the documentation of this file.
2
3namespace joedb
4{
5 //////////////////////////////////////////////////////////////////////////
6 void Progress_Bar::print_progress() noexcept
7 //////////////////////////////////////////////////////////////////////////
8 {
9 try
10 {
11 if (done > printed)
12 {
13 const auto now = std::chrono::steady_clock::now();
14 std::chrono::duration<double> duration_since_last = now - last_print_time;
15 if (done == total || duration_since_last.count() >= gap)
16 {
17 std::chrono::duration<double> duration_since_start = now - start;
18 last_print_time = now;
19
20 const auto done_string = std::to_string(done);
21 const auto total_string = std::to_string(total);
22 const auto padding = std::string(total_string.size() - done_string.size(), ' ');
23 const int permil = int((1000.0 * double(done)) / double(total));
24
25 // TODO: drop C++17, use std::format
26
27 logger.log
28 (
29 padding + done_string + " / " + total_string + " " +
30 std::to_string(permil / 10) + "." + std::to_string(permil % 10) +
31 "% in " + std::to_string(duration_since_start.count()) + "s"
32 );
33
34 printed = done;
35 gap *= 1.1;
36 if (gap > 10.0)
37 gap = 10.0;
38 }
39 }
40 }
41 catch (...)
42 {
43 }
44 }
45
46 //////////////////////////////////////////////////////////////////////////
48 //////////////////////////////////////////////////////////////////////////
49 (
50 const int64_t total,
51 Logger &logger
52 ):
53 total(total),
54 done(0),
55 printed(0),
56 gap(1.0),
57 logger(logger),
58 start(std::chrono::steady_clock::now()),
59 last_print_time(start)
60 {
61 }
62
63 //////////////////////////////////////////////////////////////////////////
64 void Progress_Bar::print(const int64_t current)
65 //////////////////////////////////////////////////////////////////////////
66 {
67 done = current;
68 print_progress();
69 }
70
71 //////////////////////////////////////////////////////////////////////////
73 //////////////////////////////////////////////////////////////////////////
74 {
75 print_progress();
76 }
77}
virtual void log(const std::string &message) noexcept
Definition Logger.h:12
void print(int64_t current)
Progress_Bar(int64_t total, Logger &logger)