Output newlines for each progress indicator when not on a tty.

This commit is contained in:
Daniel Patterson
2016-11-10 16:19:21 -08:00
parent 17c32f5ce7
commit eb12c16fd6
4 changed files with 44 additions and 8 deletions
+32
View File
@@ -0,0 +1,32 @@
#ifndef ISATTY_HPP
#define ISATTY_HPP
// For isatty()
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#else
#ifdef WIN32
#include <io.h>
#define isatty _isatty
#define fileno _fileno
#else
#error Unknown platform - don't know which header to include for isatty()
#endif // win32
#endif // unix
#include <cstdio>
namespace osrm
{
namespace util
{
// Returns true if stdout is a tty, false otherwise
// Useful for when you want to do something different when
// output is redirected to a logfile
inline bool IsStdoutATTY() { return isatty(fileno(stdout)); }
} // namespace util
} // namespace osrm
#endif
+8
View File
@@ -4,6 +4,8 @@
#include <atomic>
#include <iostream>
#include "util/isatty.hpp"
namespace osrm
{
namespace util
@@ -71,6 +73,12 @@ class Percent
{
std::cout << ".";
}
// When not on a TTY, print newlines after each progress indicator so
// so that progress is visible to line-buffered logging systems
if (!IsStdoutATTY())
std::cout << std::endl;
std::cout.flush();
}
}