summaryrefslogtreecommitdiff
path: root/lib/Support/raw_ostream.cpp
diff options
context:
space:
mode:
authorDan Gohman <gohman@apple.com>2009-08-15 02:05:19 +0000
committerDan Gohman <gohman@apple.com>2009-08-15 02:05:19 +0000
commitd7be663d4f96df8b0008c2116eb5af1de47ce613 (patch)
treed4266f81df2ff917f71d4a6d3a6144603fb94dd6 /lib/Support/raw_ostream.cpp
parentfbcb5b678f0fc0432072212a26a0507954c805d8 (diff)
downloadllvm-d7be663d4f96df8b0008c2116eb5af1de47ce613.tar.gz
llvm-d7be663d4f96df8b0008c2116eb5af1de47ce613.tar.bz2
llvm-d7be663d4f96df8b0008c2116eb5af1de47ce613.tar.xz
Always check to see if raw_fd_ostream's file descriptor is attached to
a terminal, not just when it's STDOUT_FILENO. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79066 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/raw_ostream.cpp')
-rw-r--r--lib/Support/raw_ostream.cpp29
1 files changed, 16 insertions, 13 deletions
diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp
index dcd33673ac..485119b35d 100644
--- a/lib/Support/raw_ostream.cpp
+++ b/lib/Support/raw_ostream.cpp
@@ -72,7 +72,11 @@ size_t raw_ostream::preferred_buffer_size() {
void raw_ostream::SetBuffered() {
// Ask the subclass to determine an appropriate buffer size.
- SetBufferSize(preferred_buffer_size());
+ if (size_t Size = preferred_buffer_size())
+ SetBufferSize(Size);
+ else
+ // It may return 0, meaning this stream should be unbuffered.
+ SetUnbuffered();
}
void raw_ostream::SetBufferSize(size_t Size) {
@@ -315,10 +319,6 @@ raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
if (Binary)
sys::Program::ChangeStdoutToBinary();
ShouldClose = false;
- // Mimic stdout by defaulting to unbuffered if the output device
- // is a terminal.
- if (sys::Process::StandardOutIsDisplayed())
- SetUnbuffered();
return;
}
@@ -375,8 +375,15 @@ size_t raw_fd_ostream::preferred_buffer_size() {
#if !defined(_MSC_VER) // Windows reportedly doesn't have st_blksize.
assert(FD >= 0 && "File not yet open!");
struct stat statbuf;
- if (fstat(FD, &statbuf) == 0)
+ if (fstat(FD, &statbuf) == 0) {
+ // If this is a terminal, don't use buffering. Line buffering
+ // would be a more traditional thing to do, but it's not worth
+ // the complexity.
+ if (S_ISCHR(statbuf.st_mode) && isatty(FD))
+ return 0;
+ // Return the preferred block size.
return statbuf.st_blksize;
+ }
error_detected();
#endif
return raw_ostream::preferred_buffer_size();
@@ -416,13 +423,9 @@ raw_ostream &raw_fd_ostream::resetColor() {
//===----------------------------------------------------------------------===//
// Set buffer settings to model stdout and stderr behavior.
-// raw_ostream doesn't support line buffering, so set standard output to be
-// unbuffered when the output device is a terminal. Set standard error to
-// be unbuffered.
-raw_stdout_ostream::raw_stdout_ostream()
- : raw_fd_ostream(STDOUT_FILENO, false,
- sys::Process::StandardOutIsDisplayed()) {}
-raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
+// Set standard error to be unbuffered by default.
+raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
+raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
true) {}
// An out of line virtual method to provide a home for the class vtable.