From 561a189f49441a4d211c0217dce8127f2ce7c32e Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Sat, 5 Aug 2017 04:06:53 +0300 Subject: Fix printing progress to non-terminal STDERR --- libbutl/diagnostics.cxx | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'libbutl/diagnostics.cxx') diff --git a/libbutl/diagnostics.cxx b/libbutl/diagnostics.cxx index 4cfc291..73eb155 100644 --- a/libbutl/diagnostics.cxx +++ b/libbutl/diagnostics.cxx @@ -19,7 +19,8 @@ #include // size_t #include // cerr -#include // stderr_fd() +#include +#include // stderr_fd(), fdterm() using namespace std; @@ -33,6 +34,8 @@ namespace butl static string diag_progress_blank; // Being printed blanks out the line. static size_t diag_progress_size; // Size of the last printed progress. + static optional diag_term; + // Print the progress string to STDERR. Ignore underlying OS errors (this is // a progress bar after all, and throwing from dtors wouldn't be nice). Must // be called with the diag_mutex being aquired. @@ -46,18 +49,31 @@ namespace butl static inline void progress_print (string& s) { - // If the new progress string is shorter than the printed one, then we will - // complement it with the required number of spaces (to overwrite the - // trailing junk) prior to printing, and restore it afterwards. + if (!diag_term) + try + { + diag_term = fdterm (stderr_fd()); + } + catch (const ios::failure&) + { + diag_term = false; + } + + // If we print to a terminal, and the new progress string is shorter than + // the printed one, then we will complement it with the required number of + // spaces (to overwrite the trailing junk) prior to printing, and restore + // it afterwards. // size_t n (s.size ()); - if (n < diag_progress_size) + if (*diag_term && n < diag_progress_size) s.append (diag_progress_size - n, ' '); if (!s.empty ()) { - s += '\r'; // Position the cursor at the beginning of the line. + s += *diag_term + ? '\r' // Position the cursor at the beginning of the line. + : '\n'; try { -- cgit v1.1