@@ -14,37 +14,29 @@ use crate::sync::{Arc, Mutex, MutexGuard};
1414use crate :: sys:: stdio;
1515use crate :: sys_common;
1616use crate :: sys_common:: remutex:: { ReentrantMutex , ReentrantMutexGuard } ;
17- use crate :: thread:: LocalKey ;
1817
1918type LocalStream = Arc < Mutex < Vec < u8 > > > ;
2019
2120thread_local ! {
22- /// Used by the test crate to capture the output of the print! and println! macros .
23- static LOCAL_STDOUT : Cell <Option <LocalStream >> = {
21+ /// Used by the test crate to capture the output of the print macros and panics .
22+ static OUTPUT_CAPTURE : Cell <Option <LocalStream >> = {
2423 Cell :: new( None )
2524 }
2625}
2726
28- thread_local ! {
29- /// Used by the test crate to capture the output of the eprint! and eprintln! macros, and panics.
30- static LOCAL_STDERR : Cell <Option <LocalStream >> = {
31- Cell :: new( None )
32- }
33- }
34-
35- /// Flag to indicate LOCAL_STDOUT and/or LOCAL_STDERR is used.
27+ /// Flag to indicate OUTPUT_CAPTURE is used.
3628///
37- /// If both are None and were never set on any thread, this flag is set to
38- /// false, and both LOCAL_STDOUT and LOCAL_STDOUT can be safely ignored on all
39- /// threads, saving some time and memory registering an unused thread local.
29+ /// If it is None and was never set on any thread, this flag is set to false,
30+ /// and OUTPUT_CAPTURE can be safely ignored on all threads, saving some time
31+ /// and memory registering an unused thread local.
4032///
41- /// Note about memory ordering: This contains information about whether two
42- /// thread local variables might be in use. Although this is a global flag, the
33+ /// Note about memory ordering: This contains information about whether a
34+ /// thread local variable might be in use. Although this is a global flag, the
4335/// memory ordering between threads does not matter: we only want this flag to
44- /// have a consistent order between set_print/set_panic and print_to *within
36+ /// have a consistent order between set_output_capture and print_to *within
4537/// the same thread*. Within the same thread, things always have a perfectly
4638/// consistent order. So Ordering::Relaxed is fine.
47- static LOCAL_STREAMS : AtomicBool = AtomicBool :: new ( false ) ;
39+ static OUTPUT_CAPTURE_USED : AtomicBool = AtomicBool :: new ( false ) ;
4840
4941/// A handle to a raw instance of the standard input stream of this process.
5042///
@@ -890,70 +882,24 @@ impl fmt::Debug for StderrLock<'_> {
890882 }
891883}
892884
893- /// Resets the thread-local stderr handle to the specified writer
894- ///
895- /// This will replace the current thread's stderr handle, returning the old
896- /// handle. All future calls to `panic!` and friends will emit their output to
897- /// this specified handle.
898- ///
899- /// Note that this does not need to be called for all new threads; the default
900- /// output handle is to the process's stderr stream.
901- #[ unstable(
902- feature = "set_stdio" ,
903- reason = "this function may disappear completely or be replaced \
904- with a more general mechanism",
905- issue = "none"
906- ) ]
907- #[ doc( hidden) ]
908- pub fn set_panic ( sink : Option < LocalStream > ) -> Option < LocalStream > {
909- if sink. is_none ( ) && !LOCAL_STREAMS . load ( Ordering :: Relaxed ) {
910- // LOCAL_STDERR is definitely None since LOCAL_STREAMS is false.
911- return None ;
912- }
913- LOCAL_STREAMS . store ( true , Ordering :: Relaxed ) ;
914- LOCAL_STDERR . with ( move |slot| slot. replace ( sink) )
915- }
916-
917- /// Resets the thread-local stdout handle to the specified writer
918- ///
919- /// This will replace the current thread's stdout handle, returning the old
920- /// handle. All future calls to `print!` and friends will emit their output to
921- /// this specified handle.
922- ///
923- /// Note that this does not need to be called for all new threads; the default
924- /// output handle is to the process's stdout stream.
885+ /// Sets the thread-local output capture buffer and returns the old one.
925886#[ unstable(
926- feature = "set_stdio " ,
927- reason = "this function may disappear completely or be replaced \
928- with a more general mechanism ",
887+ feature = "internal_output_capture " ,
888+ reason = "this function is meant for use in the test crate \
889+ and may disappear in the future ",
929890 issue = "none"
930891) ]
931892#[ doc( hidden) ]
932- pub fn set_print ( sink : Option < LocalStream > ) -> Option < LocalStream > {
933- if sink. is_none ( ) && !LOCAL_STREAMS . load ( Ordering :: Relaxed ) {
934- // LOCAL_STDOUT is definitely None since LOCAL_STREAMS is false.
893+ pub fn set_output_capture ( sink : Option < LocalStream > ) -> Option < LocalStream > {
894+ if sink. is_none ( ) && !OUTPUT_CAPTURE_USED . load ( Ordering :: Relaxed ) {
895+ // OUTPUT_CAPTURE is definitely None since OUTPUT_CAPTURE_USED is false.
935896 return None ;
936897 }
937- LOCAL_STREAMS . store ( true , Ordering :: Relaxed ) ;
938- LOCAL_STDOUT . with ( move |slot| slot. replace ( sink) )
939- }
940-
941- pub ( crate ) fn clone_io ( ) -> ( Option < LocalStream > , Option < LocalStream > ) {
942- // Don't waste time when LOCAL_{STDOUT,STDERR} are definitely None.
943- if !LOCAL_STREAMS . load ( Ordering :: Relaxed ) {
944- return ( None , None ) ;
945- }
946-
947- let clone = |cell : & Cell < Option < LocalStream > > | {
948- let s = cell. take ( ) ;
949- cell. set ( s. clone ( ) ) ;
950- s
951- } ;
952-
953- ( LOCAL_STDOUT . with ( clone) , LOCAL_STDERR . with ( clone) )
898+ OUTPUT_CAPTURE_USED . store ( true , Ordering :: Relaxed ) ;
899+ OUTPUT_CAPTURE . with ( move |slot| slot. replace ( sink) )
954900}
955901
956- /// Write `args` to output stream `local_s` if possible, `global_s`
902+ /// Write `args` to the capture buffer if enabled and possible, or `global_s`
957903/// otherwise. `label` identifies the stream in a panic message.
958904///
959905/// This function is used to print error messages, so it takes extra
@@ -963,16 +909,12 @@ pub(crate) fn clone_io() -> (Option<LocalStream>, Option<LocalStream>) {
963909/// thread, it will just fall back to the global stream.
964910///
965911/// However, if the actual I/O causes an error, this function does panic.
966- fn print_to < T > (
967- args : fmt:: Arguments < ' _ > ,
968- local_s : & ' static LocalKey < Cell < Option < LocalStream > > > ,
969- global_s : fn ( ) -> T ,
970- label : & str ,
971- ) where
912+ fn print_to < T > ( args : fmt:: Arguments < ' _ > , global_s : fn ( ) -> T , label : & str )
913+ where
972914 T : Write ,
973915{
974- if LOCAL_STREAMS . load ( Ordering :: Relaxed )
975- && local_s . try_with ( |s| {
916+ if OUTPUT_CAPTURE_USED . load ( Ordering :: Relaxed )
917+ && OUTPUT_CAPTURE . try_with ( |s| {
976918 // Note that we completely remove a local sink to write to in case
977919 // our printing recursively panics/prints, so the recursive
978920 // panic/print goes to the global sink instead of our local sink.
@@ -982,7 +924,7 @@ fn print_to<T>(
982924 } )
983925 } ) == Ok ( Some ( ( ) ) )
984926 {
985- // Succesfully wrote to local stream .
927+ // Succesfully wrote to capture buffer .
986928 return ;
987929 }
988930
@@ -999,7 +941,7 @@ fn print_to<T>(
999941#[ doc( hidden) ]
1000942#[ cfg( not( test) ) ]
1001943pub fn _print ( args : fmt:: Arguments < ' _ > ) {
1002- print_to ( args, & LOCAL_STDOUT , stdout, "stdout" ) ;
944+ print_to ( args, stdout, "stdout" ) ;
1003945}
1004946
1005947#[ unstable(
@@ -1010,7 +952,7 @@ pub fn _print(args: fmt::Arguments<'_>) {
1010952#[ doc( hidden) ]
1011953#[ cfg( not( test) ) ]
1012954pub fn _eprint ( args : fmt:: Arguments < ' _ > ) {
1013- print_to ( args, & LOCAL_STDERR , stderr, "stderr" ) ;
955+ print_to ( args, stderr, "stderr" ) ;
1014956}
1015957
1016958#[ cfg( test) ]
0 commit comments