Reduce unstable feature usage

This commit is contained in:
topjohnwu 2025-05-30 11:11:36 -07:00
parent 8c79d66b7b
commit d8cf42af16
6 changed files with 20 additions and 24 deletions

View File

@ -1,6 +1,4 @@
#![allow(clippy::missing_safety_doc)] #![allow(clippy::missing_safety_doc)]
#![feature(format_args_nl)]
#![feature(io_error_more)]
pub use const_format; pub use const_format;
pub use libc; pub use libc;

View File

@ -1,5 +1,4 @@
use std::fmt; use std::fmt;
use std::fmt::Arguments;
use std::io::{Write, stderr, stdout}; use std::io::{Write, stderr, stdout};
use std::process::exit; use std::process::exit;
@ -102,10 +101,6 @@ pub fn log_with_formatter<F: FnOnce(Formatter) -> fmt::Result>(level: LogLevel,
}); });
} }
pub fn log_with_args(level: LogLevel, args: Arguments) {
log_with_formatter(level, |w| w.write_fmt(args));
}
pub fn cmdline_logging() { pub fn cmdline_logging() {
fn cmdline_write(level: LogLevel, msg: &Utf8CStr) { fn cmdline_write(level: LogLevel, msg: &Utf8CStr) {
if matches!(level, LogLevel::Info) { if matches!(level, LogLevel::Info) {
@ -124,24 +119,31 @@ pub fn cmdline_logging() {
} }
} }
#[macro_export]
macro_rules! log_with_args {
($level:expr, $($args:tt)+) => {
$crate::log_with_formatter($level, |w| writeln!(w, $($args)+))
}
}
#[macro_export] #[macro_export]
macro_rules! error { macro_rules! error {
($($args:tt)+) => { ($($args:tt)+) => {
$crate::log_with_args($crate::LogLevel::Error, format_args_nl!($($args)+)) $crate::log_with_formatter($crate::LogLevel::Error, |w| writeln!(w, $($args)+))
} }
} }
#[macro_export] #[macro_export]
macro_rules! warn { macro_rules! warn {
($($args:tt)+) => { ($($args:tt)+) => {
$crate::log_with_args($crate::LogLevel::Warn, format_args_nl!($($args)+)) $crate::log_with_formatter($crate::LogLevel::Warn, |w| writeln!(w, $($args)+))
} }
} }
#[macro_export] #[macro_export]
macro_rules! info { macro_rules! info {
($($args:tt)+) => { ($($args:tt)+) => {
$crate::log_with_args($crate::LogLevel::Info, format_args_nl!($($args)+)) $crate::log_with_formatter($crate::LogLevel::Info, |w| writeln!(w, $($args)+))
} }
} }
@ -149,7 +151,7 @@ macro_rules! info {
#[macro_export] #[macro_export]
macro_rules! debug { macro_rules! debug {
($($args:tt)+) => { ($($args:tt)+) => {
$crate::log_with_args($crate::LogLevel::Debug, format_args_nl!($($args)+)) $crate::log_with_formatter($crate::LogLevel::Debug, |w| writeln!(w, $($args)+))
} }
} }

View File

@ -24,7 +24,7 @@ pub type LoggedResult<T> = Result<T, LoggedError>;
#[macro_export] #[macro_export]
macro_rules! log_err { macro_rules! log_err {
($($args:tt)+) => {{ ($($args:tt)+) => {{
$crate::log_with_args($crate::LogLevel::Error, format_args_nl!($($args)+)); $crate::error!($($args)+);
$crate::LoggedError::default() $crate::LoggedError::default()
}}; }};
} }
@ -148,12 +148,9 @@ impl<T, E: Display> Loggable<T> for Result<T, E> {
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(e) => { Err(e) => {
if let Some(caller) = caller { if let Some(caller) = caller {
log_with_args( log_with_args!(level, "[{}:{}] {:#}", caller.file(), caller.line(), e);
level,
format_args_nl!("[{}:{}] {:#}", caller.file(), caller.line(), e),
);
} else { } else {
log_with_args(level, format_args_nl!("{:#}", e)); log_with_args!(level, "{:#}", e);
} }
Err(LoggedError::default()) Err(LoggedError::default())
} }
@ -186,7 +183,7 @@ impl<T, E: Display> Loggable<T> for Result<T, E> {
impl<T: Display> From<T> for LoggedError { impl<T: Display> From<T> for LoggedError {
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
fn from(e: T) -> Self { fn from(e: T) -> Self {
log_with_args(LogLevel::Error, format_args_nl!("{:#}", e)); log_with_args!(LogLevel::Error, "{:#}", e);
LoggedError::default() LoggedError::default()
} }
@ -194,9 +191,12 @@ impl<T: Display> From<T> for LoggedError {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
fn from(e: T) -> Self { fn from(e: T) -> Self {
let caller = Location::caller(); let caller = Location::caller();
log_with_args( log_with_args!(
LogLevel::Error, LogLevel::Error,
format_args_nl!("[{}:{}] {:#}", caller.file(), caller.line(), e), "[{}:{}] {:#}",
caller.file(),
caller.line(),
e
); );
LoggedError::default() LoggedError::default()
} }

View File

@ -1,4 +1,3 @@
#![feature(format_args_nl)]
#![feature(try_blocks)] #![feature(try_blocks)]
#![feature(let_chains)] #![feature(let_chains)]
#![feature(fn_traits)] #![feature(fn_traits)]

View File

@ -1,5 +1,3 @@
#![feature(format_args_nl)]
#![feature(once_cell_try)]
#![feature(try_blocks)] #![feature(try_blocks)]
#![allow(clippy::missing_safety_doc)] #![allow(clippy::missing_safety_doc)]

View File

@ -1,4 +1,3 @@
#![feature(format_args_nl)]
#![feature(try_blocks)] #![feature(try_blocks)]
pub use base; pub use base;