From 615444d26306ddaf62388c73ae1dc17e48b3b2e3 Mon Sep 17 00:00:00 2001 From: Alex Crichton <alex@alexcrichton.com> Date: Mon, 4 Nov 2013 15:45:46 -0800 Subject: [PATCH] Stop extra buffering when stdout isn't a tty Right now if you're running a program with its output piped to some location and the program decides to go awry, when you kill the program via some signal none of the program's last 4K of output will get printed to the screen. In theory the solution to this would be to register a signal handler as part of the runtime which then flushes the output stream. I believe that the current behavior is far enough from what's expected that we shouldn't be providing this sort of "super buffering" by default when stdout isn't attached to a tty. --- src/libstd/rt/io/stdio.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/libstd/rt/io/stdio.rs b/src/libstd/rt/io/stdio.rs index fb1a3fac8d5..d33821a34b1 100644 --- a/src/libstd/rt/io/stdio.rs +++ b/src/libstd/rt/io/stdio.rs @@ -30,7 +30,7 @@ use fmt; use libc; use option::{Option, Some, None}; use result::{Ok, Err}; -use rt::io::buffered::{LineBufferedWriter, BufferedWriter}; +use rt::io::buffered::LineBufferedWriter; use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io, CloseAsynchronously}; use super::{Reader, Writer, io_error, IoError, OtherIoError}; @@ -135,14 +135,7 @@ fn with_task_stdout(f: &fn(&mut Writer)) { Some(ref mut handle) => f(*handle), None => { let handle = stdout(); - let mut handle = if handle.isatty() { - ~LineBufferedWriter::new(handle) as ~Writer - } else { - // The default capacity is very large, 64k, but this is just - // a stdout stream, and possibly per task, so let's not make - // this too expensive. - ~BufferedWriter::with_capacity(4096, handle) as ~Writer - }; + let mut handle = ~LineBufferedWriter::new(handle) as ~Writer; f(handle); (*task).stdout_handle = Some(handle); } @@ -152,10 +145,9 @@ fn with_task_stdout(f: &fn(&mut Writer)) { /// Flushes the local task's stdout handle. /// -/// By default, this stream is a buffering stream, flushing may be necessary to -/// ensure output is on the terminal screen. The buffering used is -/// line-buffering when stdout is attached to a terminal, and a fixed sized -/// buffer if it is not attached to a terminal. +/// By default, this stream is a line-buffering stream, so flushing may be +/// necessary to ensure that all output is printed to the screen (if there are +/// no newlines printed). /// /// Note that logging macros do not use this stream. Using the logging macros /// will emit output to stderr, and while they are line buffered the log