From b88266cfed8b287f8c35b2015808b09b056f61af Mon Sep 17 00:00:00 2001 From: malezjaa Date: Thu, 13 Aug 2026 01:34:46 +0200 Subject: [PATCH] Add alloc support for boxed loggers --- Cargo.toml | 3 ++- src/lib.rs | 33 +++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3dd2487df..4112ebb45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,8 @@ release_max_level_info = [] release_max_level_debug = [] release_max_level_trace = [] -std = [] +alloc = [] +std = ["alloc"] kv = [] kv_sval = ["kv", "value-bag/sval", "sval", "sval_ref"] diff --git a/src/lib.rs b/src/lib.rs index 41c5d5f3d..9d164e65a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -230,12 +230,12 @@ //! Implementations that adjust their configurations at runtime should take care //! to adjust the maximum log level as well. //! -//! # Use with `std` +//! # Use with `alloc` //! //! `set_logger` requires you to provide a `&'static Log`, which can be hard to //! obtain if your logger depends on some runtime configuration. The -//! `set_boxed_logger` function is available with the `std` Cargo feature. It is -//! identical to `set_logger` except that it takes a `Box` rather than a +//! `set_boxed_logger` function is available with the `alloc` Cargo feature. It +//! is identical to `set_logger` except that it takes a `Box` rather than a //! `&'static Log`: //! //! ``` @@ -247,7 +247,11 @@ //! # fn flush(&self) {} //! # } //! # fn main() {} -//! # #[cfg(feature = "std")] +//! # #[cfg(feature = "alloc")] +//! # extern crate alloc; +//! # #[cfg(feature = "alloc")] +//! # use alloc::boxed::Box; +//! # #[cfg(feature = "alloc")] //! pub fn init() -> Result<(), SetLoggerError> { //! log::set_boxed_logger(Box::new(SimpleLogger)) //! .map(|()| log::set_max_level(LevelFilter::Info)) @@ -293,8 +297,9 @@ //! The following crate feature flags are available in addition to the filters. They are //! configured in your `Cargo.toml`. //! -//! * `std` allows use of `std` crate instead of the default `core`. Enables using `std::error` and -//! `set_boxed_logger` functionality. +//! * `alloc` enables using `alloc::boxed::Box` and `set_boxed_logger`. +//! * `std` enables `alloc` and allows use of the `std` crate instead of the default `core`. +//! It also enables using `std::error`. //! * `serde` enables support for serialization and deserialization of `Level` and `LevelFilter`. //! //! ```toml @@ -393,9 +398,13 @@ compile_error!("multiple max_level_* features set"); ))] compile_error!("multiple release_max_level_* features set"); +#[cfg(feature = "alloc")] +extern crate alloc; #[cfg(all(not(feature = "std"), not(test)))] extern crate core as std; +#[cfg(feature = "alloc")] +use alloc::boxed::Box; use std::cfg; #[cfg(feature = "std")] use std::error; @@ -1333,8 +1342,8 @@ where } } -#[cfg(feature = "std")] -impl Log for std::boxed::Box +#[cfg(feature = "alloc")] +impl Log for Box where T: ?Sized + Log, { @@ -1435,14 +1444,14 @@ pub fn max_level() -> LevelFilter { /// `Box` rather than a `&'static Log`. See the documentation for /// [`set_logger`] for more details. /// -/// Requires the `std` feature. +/// Requires the `alloc` feature. /// /// # Errors /// /// An error is returned if a logger has already been set. /// /// [`set_logger`]: fn.set_logger.html -#[cfg(all(feature = "std", target_has_atomic = "ptr"))] +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] pub fn set_boxed_logger(logger: Box) -> Result<(), SetLoggerError> { set_logger_inner(|| Box::leak(logger)) } @@ -2015,7 +2024,7 @@ mod tests { assert_is_log::<&dyn Log>(); - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] assert_is_log::>(); #[cfg(feature = "std")] @@ -2024,7 +2033,7 @@ mod tests { // Assert these statements for all T: Log + ?Sized #[allow(unused)] fn forall() { - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] assert_is_log::>(); assert_is_log::<&T>();