Unverified Commit 375f1172 authored by Adrià Arrufat's avatar Adrià Arrufat Committed by GitHub
Browse files

Add custom ostream to console progress indicator (#2234)

parent 2ef8e3ac
...@@ -66,15 +66,16 @@ namespace dlib ...@@ -66,15 +66,16 @@ namespace dlib
inline bool print_status ( inline bool print_status (
double cur, double cur,
bool always_print = false bool always_print = false,
std::ostream& out = std::cout
); );
/*! /*!
ensures ensures
- print_status() assumes it is called with values which are linearly - print_status() assumes it is called with values which are linearly
approaching target(). It will attempt to predict how much time is approaching target(). It will attempt to predict how much time is
remaining until cur becomes equal to target(). remaining until cur becomes equal to target().
- prints a status message to the screen which indicates how much - prints a status message to out which indicates how much more time is
more time is left until cur is equal to target() left until cur is equal to target()
- if (always_print) then - if (always_print) then
- This function prints to the screen each time it is called. - This function prints to the screen each time it is called.
- else - else
...@@ -83,7 +84,7 @@ namespace dlib ...@@ -83,7 +84,7 @@ namespace dlib
until about one second has elapsed. This means that the first call until about one second has elapsed. This means that the first call
to print_status() never prints to the screen. to print_status() never prints to the screen.
- This function returns true if it prints to the screen and false - This function returns true if it prints to the screen and false
otherwise. otherwise.
!*/ !*/
private: private:
...@@ -120,7 +121,8 @@ namespace dlib ...@@ -120,7 +121,8 @@ namespace dlib
bool console_progress_indicator:: bool console_progress_indicator::
print_status ( print_status (
double cur, double cur,
bool always_print bool always_print,
std::ostream& out
) )
{ {
const time_t cur_time = std::time(0); const time_t cur_time = std::time(0);
...@@ -149,30 +151,30 @@ namespace dlib ...@@ -149,30 +151,30 @@ namespace dlib
double seconds = delta_t/delta_val * std::abs(target_val - cur); double seconds = delta_t/delta_val * std::abs(target_val - cur);
std::ios::fmtflags oldflags = std::cout.flags(); std::ios::fmtflags oldflags = out.flags();
std::cout.setf(std::ios::fixed,std::ios::floatfield); out.setf(std::ios::fixed,std::ios::floatfield);
std::streamsize ss; std::streamsize ss;
if (seconds < 60) if (seconds < 60)
{ {
ss = std::cout.precision(0); ss = out.precision(0);
std::cout << "Time remaining: " << seconds << " seconds. \r" << std::flush; out << "Time remaining: " << seconds << " seconds. \r" << std::flush;
} }
else if (seconds < 60*60) else if (seconds < 60*60)
{ {
ss = std::cout.precision(2); ss = out.precision(2);
std::cout << "Time remaining: " << seconds/60 << " minutes. \r" << std::flush; out << "Time remaining: " << seconds/60 << " minutes. \r" << std::flush;
} }
else else
{ {
ss = std::cout.precision(2); ss = out.precision(2);
std::cout << "Time remaining: " << seconds/60/60 << " hours. \r" << std::flush; out << "Time remaining: " << seconds/60/60 << " hours. \r" << std::flush;
} }
// restore previous output flags and precision settings // restore previous output flags and precision settings
std::cout.flags(oldflags); out.flags(oldflags);
std::cout.precision(ss); out.precision(ss);
return true; return true;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment