aboutsummaryrefslogtreecommitdiff
path: root/libbutl/diagnostics.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-08-05 04:06:53 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-08-06 12:27:55 +0300
commit561a189f49441a4d211c0217dce8127f2ce7c32e (patch)
treec7928517d65fe7ee9deeaa677d91fd95650c6a9b /libbutl/diagnostics.cxx
parent07f657754b0af656ee48c38540805fcec7cee27d (diff)
Fix printing progress to non-terminal STDERR
Diffstat (limited to 'libbutl/diagnostics.cxx')
-rw-r--r--libbutl/diagnostics.cxx28
1 files changed, 22 insertions, 6 deletions
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 <cstddef> // size_t
#include <iostream> // cerr
-#include <libbutl/fdstream.hxx> // stderr_fd()
+#include <libbutl/optional.hxx>
+#include <libbutl/fdstream.hxx> // 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<bool> 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
{