@@ -14,37 +14,29 @@ use crate::sync::{Arc, Mutex, MutexGuard};
14
14
use crate :: sys:: stdio;
15
15
use crate :: sys_common;
16
16
use crate :: sys_common:: remutex:: { ReentrantMutex , ReentrantMutexGuard } ;
17
- use crate :: thread:: LocalKey ;
18
17
19
18
type LocalStream = Arc < Mutex < Vec < u8 > > > ;
20
19
21
20
thread_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 >> = {
24
23
Cell :: new( None )
25
24
}
26
25
}
27
26
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.
36
28
///
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.
40
32
///
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
43
35
/// 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
45
37
/// the same thread*. Within the same thread, things always have a perfectly
46
38
/// consistent order. So Ordering::Relaxed is fine.
47
- static LOCAL_STREAMS : AtomicBool = AtomicBool :: new ( false ) ;
39
+ static OUTPUT_CAPTURE_USED : AtomicBool = AtomicBool :: new ( false ) ;
48
40
49
41
/// A handle to a raw instance of the standard input stream of this process.
50
42
///
@@ -890,70 +882,24 @@ impl fmt::Debug for StderrLock<'_> {
890
882
}
891
883
}
892
884
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.
925
886
#[ 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 ",
929
890
issue = "none"
930
891
) ]
931
892
#[ 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.
935
896
return None ;
936
897
}
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) )
954
900
}
955
901
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`
957
903
/// otherwise. `label` identifies the stream in a panic message.
958
904
///
959
905
/// 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>) {
963
909
/// thread, it will just fall back to the global stream.
964
910
///
965
911
/// 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
972
914
T : Write ,
973
915
{
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| {
976
918
// Note that we completely remove a local sink to write to in case
977
919
// our printing recursively panics/prints, so the recursive
978
920
// panic/print goes to the global sink instead of our local sink.
@@ -982,7 +924,7 @@ fn print_to<T>(
982
924
} )
983
925
} ) == Ok ( Some ( ( ) ) )
984
926
{
985
- // Succesfully wrote to local stream .
927
+ // Succesfully wrote to capture buffer .
986
928
return ;
987
929
}
988
930
@@ -999,7 +941,7 @@ fn print_to<T>(
999
941
#[ doc( hidden) ]
1000
942
#[ cfg( not( test) ) ]
1001
943
pub fn _print ( args : fmt:: Arguments < ' _ > ) {
1002
- print_to ( args, & LOCAL_STDOUT , stdout, "stdout" ) ;
944
+ print_to ( args, stdout, "stdout" ) ;
1003
945
}
1004
946
1005
947
#[ unstable(
@@ -1010,7 +952,7 @@ pub fn _print(args: fmt::Arguments<'_>) {
1010
952
#[ doc( hidden) ]
1011
953
#[ cfg( not( test) ) ]
1012
954
pub fn _eprint ( args : fmt:: Arguments < ' _ > ) {
1013
- print_to ( args, & LOCAL_STDERR , stderr, "stderr" ) ;
955
+ print_to ( args, stderr, "stderr" ) ;
1014
956
}
1015
957
1016
958
#[ cfg( test) ]
0 commit comments