From eb12c16fd657cfa1cd5b83223730a1bdebfdcef4 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Thu, 10 Nov 2016 16:19:21 -0800 Subject: [PATCH] Output newlines for each progress indicator when not on a tty. --- CHANGELOG.md | 2 ++ include/util/isatty.hpp | 32 ++++++++++++++++++++++++++++++++ include/util/percent.hpp | 8 ++++++++ src/util/simple_logger.cpp | 10 ++-------- 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 include/util/isatty.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index ebfc69b1d..45797974b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,8 @@ - Added support for turn penalties - Internals - Internal/Shared memory datafacades now share common memory layout and data loading code + - Misc + - Progress indicators now print newlines when stdout is not a TTY # 5.4.3 - Changes from 5.4.2 diff --git a/include/util/isatty.hpp b/include/util/isatty.hpp new file mode 100644 index 000000000..39f80bc75 --- /dev/null +++ b/include/util/isatty.hpp @@ -0,0 +1,32 @@ +#ifndef ISATTY_HPP +#define ISATTY_HPP + +// For isatty() +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) +#include +#else +#ifdef WIN32 +#include +#define isatty _isatty +#define fileno _fileno +#else +#error Unknown platform - don't know which header to include for isatty() +#endif // win32 +#endif // unix + +#include + +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 \ No newline at end of file diff --git a/include/util/percent.hpp b/include/util/percent.hpp index b65c876ff..eeee33ad1 100644 --- a/include/util/percent.hpp +++ b/include/util/percent.hpp @@ -4,6 +4,8 @@ #include #include +#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(); } } diff --git a/src/util/simple_logger.cpp b/src/util/simple_logger.cpp index 13ee10391..f582841ee 100644 --- a/src/util/simple_logger.cpp +++ b/src/util/simple_logger.cpp @@ -1,11 +1,5 @@ #include "util/simple_logger.hpp" -#ifdef _MSC_VER -#include -#define isatty _isatty -#define fileno _fileno -#else -#include -#endif +#include "util/isatty.hpp" #include #include #include @@ -77,7 +71,7 @@ SimpleLogger::~SimpleLogger() std::lock_guard lock(get_mutex()); if (!LogPolicy::GetInstance().IsMute()) { - const bool is_terminal = static_cast(isatty(fileno(stdout))); + const bool is_terminal = IsStdoutATTY(); switch (level) { case logWARNING: