Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
progressbar.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <iomanip>
22#include <iostream>
23
24namespace ore {
25namespace data {
26
27void ProgressReporter::registerProgressIndicator(const QuantLib::ext::shared_ptr<ProgressIndicator>& indicator) {
28 indicators_.insert(indicator);
29}
30
31void ProgressReporter::unregisterProgressIndicator(const QuantLib::ext::shared_ptr<ProgressIndicator>& indicator) {
32 indicators_.erase(indicator);
33}
34
36
37void ProgressReporter::updateProgress(const unsigned long progress, const unsigned long total, const std::string& detail) {
38 for (const auto& i : indicators_)
39 i->updateProgress(progress, total, detail);
40}
41
43 for (const auto& i : indicators_)
44 i->reset();
45}
46
47SimpleProgressBar::SimpleProgressBar(const std::string& message, const QuantLib::Size messageWidth,
48 const QuantLib::Size barWidth, const QuantLib::Size numberOfScreenUpdates)
49 : key_(message), messageWidth_(messageWidth), barWidth_(barWidth),
50 numberOfScreenUpdates_(numberOfScreenUpdates), updateCounter_(0), finalized_(false) {
51 updateProgress(0, 1, "");
53}
54
55void SimpleProgressBar::updateProgress(const unsigned long progress, const unsigned long total, const std::string& detail) {
56 if (!ConsoleLog::instance().enabled())
57 return;
58 if (finalized_)
59 return;
60 double ratio = static_cast<double>(progress) / static_cast<double>(total);
61 if (progress >= total) {
62 std::cout << "\r" << std::setw(messageWidth_) << std::left << key_;
63 for (unsigned int i = 0; i < barWidth_; ++i)
64 std::cout << " ";
65 std::cout << " \r";
66 std::cout << std::setw(messageWidth_) << std::left << key_;
67 std::cout.flush();
68 finalized_ = true;
69 return;
70 }
71 if (updateCounter_ > 0 && progress * numberOfScreenUpdates_ < updateCounter_ * total) {
72 return;
73 }
74 std::cout << "\r" << std::setw(messageWidth_) << std::left << key_;
75 if (barWidth_ > 0)
76 std::cout << "[";
77 unsigned int pos = static_cast<unsigned int>(static_cast<double>(barWidth_) * ratio);
78 for (unsigned int i = 0; i < barWidth_; ++i) {
79 if (i < pos)
80 std::cout << "=";
81 else if (i == pos && pos != 0)
82 std::cout << ">";
83 else
84 std::cout << " ";
85 }
86 if (barWidth_ > 0)
87 std::cout << "] ";
88 std::cout << static_cast<int>(ratio * 100.0) << " %\r";
89 std::cout.flush();
91}
92
95 finalized_ = false;
96}
97
98ProgressLog::ProgressLog(const std::string& message, const unsigned int numberOfMessages, const oreSeverity logLevel)
99 : key_(message), numberOfMessages_(numberOfMessages), logLevel_(logLevel), messageCounter_(0) {}
100
101void ProgressLog::updateProgress(const unsigned long progress, const unsigned long total, const std::string& detail) {
102 if (messageCounter_ > 0 && progress * numberOfMessages_ < (messageCounter_ * total)) {
103 return;
104 }
105 MLOG(logLevel_, key_ << " (" << detail << "): " << progress << " out of " << total << " steps ("
106 << static_cast<int>(static_cast<double>(progress) / static_cast<double>(total) * 100.0)
107 << "%) completed");
108 ProgressMessage(key_, progress, total, detail).log();
110}
111
113
115 const std::set<QuantLib::ext::shared_ptr<ProgressIndicator>>& indicators)
116 : indicators_(indicators) {}
117
118void MultiThreadedProgressIndicator::updateProgress(const unsigned long progress, const unsigned long total, const std::string& detail) {
119 boost::unique_lock<boost::shared_mutex> lock(mutex_);
120 threadData_[std::this_thread::get_id()] = std::make_tuple(progress, total, detail);
121 unsigned long progressTmp = 0;
122 unsigned long totalTmp = 0;
123 std::ostringstream detailTmp;
124 for (auto const& d : threadData_) {
125 progressTmp += std::get<0>(d.second);
126 totalTmp += std::get<1>(d.second);
127
128 if (detailTmp.tellp() != 0)
129 detailTmp << "|";
130 detailTmp << std::get<2>(d.second);
131 }
132 for (auto& i : indicators_)
133 i->updateProgress(progressTmp, totalTmp, detailTmp.str());
134}
135
137 boost::unique_lock<boost::shared_mutex> lock(mutex_);
138 for (auto& i : indicators_)
139 i->reset();
140 threadData_.clear();
141}
142
143NoProgressBar::NoProgressBar(const std::string& message, const unsigned int messageWidth) {
144 std::cout << std::setw(messageWidth) << message << std::flush;
145}
146
147} // namespace data
148} // namespace ore
void log() const
generate Boost log record to pass to corresponding sinks
Definition: log.cpp:491
void updateProgress(const unsigned long progress, const unsigned long total, const std::string &detail) override
MultiThreadedProgressIndicator(const std::set< QuantLib::ext::shared_ptr< ProgressIndicator > > &indicators)
std::set< QuantLib::ext::shared_ptr< ProgressIndicator > > indicators_
std::map< std::thread::id, std::tuple< unsigned long, unsigned long, std::string > > threadData_
NoProgressBar(const std::string &message, const unsigned int messageWidth=40)
unsigned int numberOfMessages_
void updateProgress(const unsigned long progress, const unsigned long total, const std::string &detail) override
ProgressIndicator interface.
ProgressLog(const std::string &message, const unsigned int numberOfMessages=100, const oreSeverity logLevel=oreSeverity::debug)
Definition: progressbar.cpp:98
unsigned int messageCounter_
void reset() override
void unregisterProgressIndicator(const QuantLib::ext::shared_ptr< ProgressIndicator > &indicator)
unregister a Progress Indicator
Definition: progressbar.cpp:31
void updateProgress(const unsigned long progress, const unsigned long total, const std::string &detail="")
update progress
Definition: progressbar.cpp:37
std::set< QuantLib::ext::shared_ptr< ProgressIndicator > > indicators_
Definition: progressbar.hpp:75
void registerProgressIndicator(const QuantLib::ext::shared_ptr< ProgressIndicator > &indicator)
register a Progress Indicator
Definition: progressbar.cpp:27
void unregisterAllProgressIndicators()
unregister all progress indicators
Definition: progressbar.cpp:35
void updateProgress(const unsigned long progress, const unsigned long total, const std::string &detail) override
ProgressIndicator interface.
Definition: progressbar.cpp:55
unsigned int numberOfScreenUpdates_
Definition: progressbar.hpp:96
SimpleProgressBar(const std::string &message, const QuantLib::Size messageWidth=40, const QuantLib::Size barWidth=40, const QuantLib::Size numberOfScreenUpdates=100)
Definition: progressbar.cpp:47
oreSeverity
Definition: log.hpp:70
@ data
Definition: log.hpp:77
#define MLOG(mask, text)
Definition: log.hpp:529
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Classes for progress reporting.