Revise the eprint(ln)! feature.
* Factor out the nigh-identical bodies of `_print` and `_eprint` to a helper function `print_to` (I was sorely tempted to call it `_doprnt`). * Update the issue number for the unstable `eprint` feature. * Add entries to the "unstable book" for `eprint` and `eprint_internal`. * Style corrections to the documentation.
This commit is contained in:
parent
76127275a0
commit
07766f675c
@ -130,6 +130,8 @@
|
|||||||
- [derive_eq](library-features/derive-eq.md)
|
- [derive_eq](library-features/derive-eq.md)
|
||||||
- [discriminant_value](library-features/discriminant-value.md)
|
- [discriminant_value](library-features/discriminant-value.md)
|
||||||
- [error_type_id](library-features/error-type-id.md)
|
- [error_type_id](library-features/error-type-id.md)
|
||||||
|
- [eprint](library-features/eprint.md)
|
||||||
|
- [eprint_internal](library-features/eprint-internal.md)
|
||||||
- [exact_size_is_empty](library-features/exact-size-is-empty.md)
|
- [exact_size_is_empty](library-features/exact-size-is-empty.md)
|
||||||
- [fd](library-features/fd.md)
|
- [fd](library-features/fd.md)
|
||||||
- [fd_read](library-features/fd-read.md)
|
- [fd_read](library-features/fd-read.md)
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
# `eprint_internal`
|
||||||
|
|
||||||
|
This feature is internal to the Rust compiler and is not intended for general use.
|
||||||
|
|
||||||
|
------------------------
|
13
src/doc/unstable-book/src/library-features/eprint.md
Normal file
13
src/doc/unstable-book/src/library-features/eprint.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# `eprint`
|
||||||
|
|
||||||
|
The tracking issue for this feature is: [#40528]
|
||||||
|
|
||||||
|
[#40528]: https://github.com/rust-lang/rust/issues/40528
|
||||||
|
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
This feature enables the `eprint!` and `eprintln!` global macros,
|
||||||
|
which are just like `print!` and `println!`, respectively, except that
|
||||||
|
they send output to the standard _error_ stream, rather than standard
|
||||||
|
output. (`panic!` messages have always been written to standard error.)
|
||||||
|
|
@ -290,7 +290,7 @@ pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat};
|
|||||||
pub use self::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr};
|
pub use self::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr};
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub use self::stdio::{StdoutLock, StderrLock, StdinLock};
|
pub use self::stdio::{StdoutLock, StderrLock, StdinLock};
|
||||||
#[unstable(feature = "eprint", issue="39228")]
|
#[unstable(feature = "eprint", issue="40528")]
|
||||||
pub use self::stdio::_eprint;
|
pub use self::stdio::_eprint;
|
||||||
#[unstable(feature = "libstd_io_internals", issue = "0")]
|
#[unstable(feature = "libstd_io_internals", issue = "0")]
|
||||||
#[doc(no_inline, hidden)]
|
#[doc(no_inline, hidden)]
|
||||||
|
@ -17,7 +17,7 @@ use io::{self, BufReader, LineWriter};
|
|||||||
use sync::{Arc, Mutex, MutexGuard};
|
use sync::{Arc, Mutex, MutexGuard};
|
||||||
use sys::stdio;
|
use sys::stdio;
|
||||||
use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
||||||
use thread::LocalKeyState;
|
use thread::{LocalKey, LocalKeyState};
|
||||||
|
|
||||||
/// Stdout used by print! and println! macros
|
/// Stdout used by print! and println! macros
|
||||||
thread_local! {
|
thread_local! {
|
||||||
@ -659,75 +659,54 @@ pub fn set_print(sink: Option<Box<Write + Send>>) -> Option<Box<Write + Send>> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unstable(feature = "print",
|
/// Write `args` to output stream `local_s` if possible, `global_s`
|
||||||
reason = "implementation detail which may disappear or be replaced at any time",
|
/// otherwise. `label` identifies the stream in a panic message.
|
||||||
issue = "0")]
|
///
|
||||||
#[doc(hidden)]
|
/// This function is used to print error messages, so it takes extra
|
||||||
pub fn _print(args: fmt::Arguments) {
|
/// care to avoid causing a panic when `local_stream` is unusable.
|
||||||
// As an implementation of the `println!` macro, we want to try our best to
|
/// For instance, if the TLS key for the local stream is uninitialized
|
||||||
// not panic wherever possible and get the output somewhere. There are
|
/// or already destroyed, or if the local stream is locked by another
|
||||||
// currently two possible vectors for panics we take care of here:
|
/// thread, it will just fall back to the global stream.
|
||||||
//
|
///
|
||||||
// 1. If the TLS key for the local stdout has been destroyed, accessing it
|
/// However, if the actual I/O causes an error, this function does panic.
|
||||||
// would cause a panic. Note that we just lump in the uninitialized case
|
fn print_to<T>(args: fmt::Arguments,
|
||||||
// here for convenience, we're not trying to avoid a panic.
|
local_s: &'static LocalKey<RefCell<Option<Box<Write+Send>>>>,
|
||||||
// 2. If the local stdout is currently in use (e.g. we're in the middle of
|
global_s: fn() -> T,
|
||||||
// already printing) then accessing again would cause a panic.
|
label: &str) where T: Write {
|
||||||
//
|
let result = match local_s.state() {
|
||||||
// If, however, the actual I/O causes an error, we do indeed panic.
|
|
||||||
let result = match LOCAL_STDOUT.state() {
|
|
||||||
LocalKeyState::Uninitialized |
|
LocalKeyState::Uninitialized |
|
||||||
LocalKeyState::Destroyed => stdout().write_fmt(args),
|
LocalKeyState::Destroyed => global_s().write_fmt(args),
|
||||||
LocalKeyState::Valid => {
|
LocalKeyState::Valid => {
|
||||||
LOCAL_STDOUT.with(|s| {
|
local_s.with(|s| {
|
||||||
if let Ok(mut borrowed) = s.try_borrow_mut() {
|
if let Ok(mut borrowed) = s.try_borrow_mut() {
|
||||||
if let Some(w) = borrowed.as_mut() {
|
if let Some(w) = borrowed.as_mut() {
|
||||||
return w.write_fmt(args);
|
return w.write_fmt(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stdout().write_fmt(args)
|
global_s().write_fmt(args)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
panic!("failed printing to stdout: {}", e);
|
panic!("failed printing to {}: {}", label, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[unstable(feature = "print",
|
||||||
|
reason = "implementation detail which may disappear or be replaced at any time",
|
||||||
|
issue = "0")]
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub fn _print(args: fmt::Arguments) {
|
||||||
|
print_to(args, &LOCAL_STDOUT, stdout, "stdout");
|
||||||
|
}
|
||||||
|
|
||||||
#[unstable(feature = "eprint_internal",
|
#[unstable(feature = "eprint_internal",
|
||||||
reason = "implementation detail which may disappear or be replaced at any time",
|
reason = "implementation detail which may disappear or be replaced at any time",
|
||||||
issue = "0")]
|
issue = "0")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn _eprint(args: fmt::Arguments) {
|
pub fn _eprint(args: fmt::Arguments) {
|
||||||
// As an implementation of the `eprintln!` macro, we want to try our best to
|
|
||||||
// not panic wherever possible and get the output somewhere. There are
|
|
||||||
// currently two possible vectors for panics we take care of here:
|
|
||||||
//
|
|
||||||
// 1. If the TLS key for the local stderr has been destroyed, accessing it
|
|
||||||
// would cause a panic. Note that we just lump in the uninitialized case
|
|
||||||
// here for convenience, we're not trying to avoid a panic.
|
|
||||||
// 2. If the local stderr is currently in use (e.g. we're in the middle of
|
|
||||||
// already printing) then accessing again would cause a panic.
|
|
||||||
//
|
|
||||||
// If, however, the actual I/O causes an error, we do indeed panic.
|
|
||||||
use panicking::LOCAL_STDERR;
|
use panicking::LOCAL_STDERR;
|
||||||
let result = match LOCAL_STDERR.state() {
|
print_to(args, &LOCAL_STDERR, stderr, "stderr");
|
||||||
LocalKeyState::Uninitialized |
|
|
||||||
LocalKeyState::Destroyed => stderr().write_fmt(args),
|
|
||||||
LocalKeyState::Valid => {
|
|
||||||
LOCAL_STDERR.with(|s| {
|
|
||||||
if let Ok(mut borrowed) = s.try_borrow_mut() {
|
|
||||||
if let Some(w) = borrowed.as_mut() {
|
|
||||||
return w.write_fmt(args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stderr().write_fmt(args)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if let Err(e) = result {
|
|
||||||
panic!("failed printing to stderr: {}", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -113,7 +113,7 @@ macro_rules! print {
|
|||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if writing to `io::stdout()` fails.
|
/// Panics if writing to `io::stdout` fails.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
@ -133,7 +133,7 @@ macro_rules! println {
|
|||||||
/// Macro for printing to the standard error.
|
/// Macro for printing to the standard error.
|
||||||
///
|
///
|
||||||
/// Equivalent to the `print!` macro, except that output goes to
|
/// Equivalent to the `print!` macro, except that output goes to
|
||||||
/// `io::stderr()` instead of `io::stdout()`. See `print!` for
|
/// `io::stderr` instead of `io::stdout`. See `print!` for
|
||||||
/// example usage.
|
/// example usage.
|
||||||
///
|
///
|
||||||
/// Use `eprint!` only for error and progress messages. Use `print!`
|
/// Use `eprint!` only for error and progress messages. Use `print!`
|
||||||
@ -141,9 +141,9 @@ macro_rules! println {
|
|||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if writing to `io::stderr()` fails.
|
/// Panics if writing to `io::stderr` fails.
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
#[unstable(feature = "eprint", issue="39228")]
|
#[unstable(feature = "eprint", issue="40528")]
|
||||||
#[allow_internal_unstable]
|
#[allow_internal_unstable]
|
||||||
macro_rules! eprint {
|
macro_rules! eprint {
|
||||||
($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*)));
|
($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*)));
|
||||||
@ -152,7 +152,7 @@ macro_rules! eprint {
|
|||||||
/// Macro for printing to the standard error, with a newline.
|
/// Macro for printing to the standard error, with a newline.
|
||||||
///
|
///
|
||||||
/// Equivalent to the `println!` macro, except that output goes to
|
/// Equivalent to the `println!` macro, except that output goes to
|
||||||
/// `io::stderr()` instead of `io::stdout()`. See `println!` for
|
/// `io::stderr` instead of `io::stdout`. See `println!` for
|
||||||
/// example usage.
|
/// example usage.
|
||||||
///
|
///
|
||||||
/// Use `eprintln!` only for error and progress messages. Use `println!`
|
/// Use `eprintln!` only for error and progress messages. Use `println!`
|
||||||
@ -160,9 +160,9 @@ macro_rules! eprint {
|
|||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if writing to `io::stderr()` fails.
|
/// Panics if writing to `io::stderr` fails.
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
#[unstable(feature = "eprint", issue="39228")]
|
#[unstable(feature = "eprint", issue="40528")]
|
||||||
macro_rules! eprintln {
|
macro_rules! eprintln {
|
||||||
() => (eprint!("\n"));
|
() => (eprint!("\n"));
|
||||||
($fmt:expr) => (eprint!(concat!($fmt, "\n")));
|
($fmt:expr) => (eprint!(concat!($fmt, "\n")));
|
||||||
|
Loading…
Reference in New Issue
Block a user