2014-11-28 06:13:18 -05:00
|
|
|
#ifndef PERCENT_HPP
|
|
|
|
#define PERCENT_HPP
|
2010-08-31 10:00:40 -04:00
|
|
|
|
2014-05-20 17:58:32 -04:00
|
|
|
#include <atomic>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <iostream>
|
2011-11-26 10:42:15 -05:00
|
|
|
|
2016-11-10 19:19:21 -05:00
|
|
|
#include "util/isatty.hpp"
|
2016-12-06 15:30:46 -05:00
|
|
|
#include "util/log.hpp"
|
2016-11-10 19:19:21 -05:00
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
class Percent
|
|
|
|
{
|
2016-12-06 15:30:46 -05:00
|
|
|
Log &log;
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
public:
|
2016-12-06 15:30:46 -05:00
|
|
|
explicit Percent(Log &log_, unsigned max_value, unsigned step = 5) : log{log_}
|
|
|
|
{
|
|
|
|
Reinit(max_value, step);
|
|
|
|
}
|
2010-08-31 10:00:40 -04:00
|
|
|
|
2015-02-19 03:19:51 -05:00
|
|
|
// Reinitializes
|
2016-04-28 18:46:59 -04:00
|
|
|
void Reinit(unsigned max_value, unsigned step = 5)
|
2014-05-07 12:39:16 -04:00
|
|
|
{
|
|
|
|
m_max_value = max_value;
|
|
|
|
m_current_value = 0;
|
|
|
|
m_percent_interval = m_max_value / 100;
|
|
|
|
m_next_threshold = m_percent_interval;
|
|
|
|
m_last_percent = 0;
|
|
|
|
m_step = step;
|
2010-08-31 10:00:40 -04:00
|
|
|
}
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
// If there has been significant progress, display it.
|
2016-04-28 18:46:59 -04:00
|
|
|
void PrintStatus(unsigned current_value)
|
2014-05-07 12:39:16 -04:00
|
|
|
{
|
|
|
|
if (current_value >= m_next_threshold)
|
|
|
|
{
|
|
|
|
m_next_threshold += m_percent_interval;
|
2016-04-28 18:46:59 -04:00
|
|
|
PrintPercent(current_value / static_cast<double>(m_max_value) * 100.);
|
2010-08-31 10:00:40 -04:00
|
|
|
}
|
2014-05-07 12:39:16 -04:00
|
|
|
if (current_value + 1 == m_max_value)
|
2016-12-06 15:30:46 -05:00
|
|
|
log << " 100%";
|
2010-08-31 10:00:40 -04:00
|
|
|
}
|
|
|
|
|
2016-04-28 18:46:59 -04:00
|
|
|
void PrintIncrement()
|
2014-05-07 12:39:16 -04:00
|
|
|
{
|
|
|
|
++m_current_value;
|
2016-04-28 18:46:59 -04:00
|
|
|
PrintStatus(m_current_value);
|
2010-08-31 10:00:40 -04:00
|
|
|
}
|
2012-03-05 03:37:13 -05:00
|
|
|
|
2016-04-28 18:46:59 -04:00
|
|
|
void PrintAddition(const unsigned addition)
|
2014-05-07 12:39:16 -04:00
|
|
|
{
|
|
|
|
m_current_value += addition;
|
2016-04-28 18:46:59 -04:00
|
|
|
PrintStatus(m_current_value);
|
2012-03-05 03:37:13 -05:00
|
|
|
}
|
2010-08-31 10:00:40 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
private:
|
2014-05-20 17:58:32 -04:00
|
|
|
std::atomic_uint m_current_value;
|
2014-05-07 12:39:16 -04:00
|
|
|
unsigned m_max_value;
|
|
|
|
unsigned m_percent_interval;
|
|
|
|
unsigned m_next_threshold;
|
|
|
|
unsigned m_last_percent;
|
|
|
|
unsigned m_step;
|
|
|
|
|
|
|
|
// Displays progress.
|
2016-04-28 18:46:59 -04:00
|
|
|
void PrintPercent(double percent)
|
2014-05-07 12:39:16 -04:00
|
|
|
{
|
|
|
|
while (percent >= m_last_percent + m_step)
|
|
|
|
{
|
|
|
|
m_last_percent += m_step;
|
|
|
|
if (m_last_percent % 10 == 0)
|
|
|
|
{
|
2016-12-06 15:30:46 -05:00
|
|
|
log << " " << m_last_percent << "% ";
|
2010-08-31 10:00:40 -04:00
|
|
|
}
|
2014-05-07 12:39:16 -04:00
|
|
|
else
|
|
|
|
{
|
2016-12-06 15:30:46 -05:00
|
|
|
log << ".";
|
2010-08-31 10:00:40 -04:00
|
|
|
}
|
2016-11-10 19:19:21 -05:00
|
|
|
|
|
|
|
// When not on a TTY, print newlines after each progress indicator so
|
|
|
|
// so that progress is visible to line-buffered logging systems
|
|
|
|
if (!IsStdoutATTY())
|
2016-12-06 15:30:46 -05:00
|
|
|
log << "" << std::endl;
|
2010-08-31 10:00:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-01-05 10:51:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-28 06:13:18 -05:00
|
|
|
#endif // PERCENT_HPP
|