|
60 | 60 | #[macro_export]
|
61 | 61 | macro_rules! print {
|
62 | 62 | ($($arg:tt)*) => {{
|
63 |
| - if cfg!(any(feature = "test", test)) { |
64 |
| - use std::io::Write as _; |
65 |
| - |
66 |
| - let stdio = std::io::stdout(); |
67 |
| - let choice = $crate::AutoStream::choice(&stdio); |
68 |
| - let buffer = Vec::new(); |
69 |
| - let mut stream = $crate::AutoStream::new(buffer, choice); |
70 |
| - // Ignore errors rather than panic |
71 |
| - let _ = ::std::write!(&mut stream, $($arg)*); |
72 |
| - let buffer = stream.into_inner(); |
73 |
| - // Should be UTF-8 but not wanting to panic |
74 |
| - let buffer = String::from_utf8_lossy(&buffer); |
| 63 | + if cfg!(test) || $crate::_macros::FEATURE_TEST_ACTIVATED { |
| 64 | + let target_stream = std::io::stdout(); |
| 65 | + let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); |
75 | 66 | ::std::print!("{}", buffer)
|
76 | 67 | } else {
|
77 | 68 | use std::io::Write as _;
|
@@ -141,18 +132,9 @@ macro_rules! println {
|
141 | 132 | $crate::print!("\n")
|
142 | 133 | };
|
143 | 134 | ($($arg:tt)*) => {{
|
144 |
| - if cfg!(any(feature = "test", test)) { |
145 |
| - use std::io::Write as _; |
146 |
| - |
147 |
| - let stdio = std::io::stdout(); |
148 |
| - let choice = $crate::AutoStream::choice(&stdio); |
149 |
| - let buffer = Vec::new(); |
150 |
| - let mut stream = $crate::AutoStream::new(buffer, choice); |
151 |
| - // Ignore errors rather than panic |
152 |
| - let _ = ::std::write!(&mut stream, $($arg)*); |
153 |
| - let buffer = stream.into_inner(); |
154 |
| - // Should be UTF-8 but not wanting to panic |
155 |
| - let buffer = String::from_utf8_lossy(&buffer); |
| 135 | + if cfg!(test) || $crate::_macros::FEATURE_TEST_ACTIVATED { |
| 136 | + let target_stream = std::io::stdout(); |
| 137 | + let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); |
156 | 138 | ::std::println!("{}", buffer)
|
157 | 139 | } else {
|
158 | 140 | use std::io::Write as _;
|
@@ -201,26 +183,17 @@ macro_rules! println {
|
201 | 183 | #[macro_export]
|
202 | 184 | macro_rules! eprint {
|
203 | 185 | ($($arg:tt)*) => {{
|
204 |
| - if cfg!(any(feature = "test", test)) { |
205 |
| - use std::io::Write as _; |
206 |
| - |
207 |
| - let stdio = std::io::stderr(); |
208 |
| - let choice = $crate::AutoStream::choice(&stdio); |
209 |
| - let buffer = Vec::new(); |
210 |
| - let mut stream = $crate::AutoStream::new(buffer, choice); |
211 |
| - // Ignore errors rather than panic |
212 |
| - let _ = ::std::write!(&mut stream, $($arg)*); |
213 |
| - let buffer = stream.into_inner(); |
214 |
| - // Should be UTF-8 but not wanting to panic |
215 |
| - let buffer = String::from_utf8_lossy(&buffer); |
| 186 | + if cfg!(test) || $crate::_macros::FEATURE_TEST_ACTIVATED { |
| 187 | + let target_stream = std::io::stderr(); |
| 188 | + let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); |
216 | 189 | ::std::eprint!("{}", buffer)
|
217 | 190 | } else {
|
218 | 191 | use std::io::Write as _;
|
219 | 192 |
|
220 | 193 | let mut stream = $crate::stderr();
|
221 | 194 | match ::std::write!(&mut stream, $($arg)*) {
|
222 | 195 | Err(e) if e.kind() != ::std::io::ErrorKind::BrokenPipe => {
|
223 |
| - ::std::panic!("failed printing to stdout: {e}"); |
| 196 | + ::std::panic!("failed printing to stderr: {e}"); |
224 | 197 | }
|
225 | 198 | Err(_) | Ok(_) => {}
|
226 | 199 | }
|
@@ -264,26 +237,17 @@ macro_rules! eprintln {
|
264 | 237 | $crate::eprint!("\n")
|
265 | 238 | };
|
266 | 239 | ($($arg:tt)*) => {{
|
267 |
| - if cfg!(any(feature = "test", test)) { |
268 |
| - use std::io::Write as _; |
269 |
| - |
270 |
| - let stdio = std::io::stderr(); |
271 |
| - let choice = $crate::AutoStream::choice(&stdio); |
272 |
| - let buffer = Vec::new(); |
273 |
| - let mut stream = $crate::AutoStream::new(buffer, choice); |
274 |
| - // Ignore errors rather than panic |
275 |
| - let _ = ::std::write!(&mut stream, $($arg)*); |
276 |
| - let buffer = stream.into_inner(); |
277 |
| - // Should be UTF-8 but not wanting to panic |
278 |
| - let buffer = String::from_utf8_lossy(&buffer); |
| 240 | + if cfg!(test) || $crate::_macros::FEATURE_TEST_ACTIVATED { |
| 241 | + let target_stream = std::io::stderr(); |
| 242 | + let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); |
279 | 243 | ::std::eprintln!("{}", buffer)
|
280 | 244 | } else {
|
281 | 245 | use std::io::Write as _;
|
282 | 246 |
|
283 | 247 | let mut stream = $crate::stderr();
|
284 | 248 | match ::std::writeln!(&mut stream, $($arg)*) {
|
285 | 249 | Err(e) if e.kind() != ::std::io::ErrorKind::BrokenPipe => {
|
286 |
| - ::std::panic!("failed printing to stdout: {e}"); |
| 250 | + ::std::panic!("failed printing to stderr: {e}"); |
287 | 251 | }
|
288 | 252 | Err(_) | Ok(_) => {}
|
289 | 253 | }
|
@@ -373,17 +337,29 @@ macro_rules! panic {
|
373 | 337 | ::std::panic!()
|
374 | 338 | };
|
375 | 339 | ($($arg:tt)*) => {{
|
376 |
| - use std::io::Write as _; |
377 |
| - |
378 |
| - let panic_stream = std::io::stderr(); |
379 |
| - let choice = $crate::AutoStream::choice(&panic_stream); |
380 |
| - let buffer = Vec::new(); |
381 |
| - let mut stream = $crate::AutoStream::new(buffer, choice); |
382 |
| - // Ignore errors rather than panic |
383 |
| - let _ = ::std::write!(&mut stream, $($arg)*); |
384 |
| - let buffer = stream.into_inner(); |
385 |
| - // Should be UTF-8 but not wanting to panic |
386 |
| - let buffer = String::from_utf8_lossy(&buffer).into_owned(); |
| 340 | + let target_stream = std::io::stderr(); |
| 341 | + let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream); |
387 | 342 | ::std::panic!("{}", buffer)
|
388 | 343 | }};
|
389 | 344 | }
|
| 345 | + |
| 346 | +#[cfg(feature = "auto")] |
| 347 | +pub const FEATURE_TEST_ACTIVATED: bool = cfg!(feature = "test"); |
| 348 | + |
| 349 | +#[cfg(feature = "auto")] |
| 350 | +pub fn to_adapted_string( |
| 351 | + display: &dyn std::fmt::Display, |
| 352 | + stream: &impl crate::stream::RawStream, |
| 353 | +) -> String { |
| 354 | + use std::io::Write as _; |
| 355 | + |
| 356 | + let choice = crate::AutoStream::choice(stream); |
| 357 | + let buffer = Vec::new(); |
| 358 | + let mut stream = crate::AutoStream::new(buffer, choice); |
| 359 | + // Ignore errors rather than panic |
| 360 | + let _ = ::std::write!(&mut stream, "{display}"); |
| 361 | + let buffer = stream.into_inner(); |
| 362 | + // Should be UTF-8 but not wanting to panic |
| 363 | + let buffer = String::from_utf8_lossy(&buffer).into_owned(); |
| 364 | + buffer |
| 365 | +} |
0 commit comments