Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
33 changes: 21 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Log>` 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<Log>` rather than a
//! `&'static Log`:
//!
//! ```
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1333,8 +1342,8 @@ where
}
}

#[cfg(feature = "std")]
impl<T> Log for std::boxed::Box<T>
#[cfg(feature = "alloc")]
impl<T> Log for Box<T>
where
T: ?Sized + Log,
{
Expand Down Expand Up @@ -1435,14 +1444,14 @@ pub fn max_level() -> LevelFilter {
/// `Box<Log>` 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<dyn Log>) -> Result<(), SetLoggerError> {
set_logger_inner(|| Box::leak(logger))
}
Expand Down Expand Up @@ -2015,7 +2024,7 @@ mod tests {

assert_is_log::<&dyn Log>();

#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
assert_is_log::<Box<dyn Log>>();

#[cfg(feature = "std")]
Expand All @@ -2024,7 +2033,7 @@ mod tests {
// Assert these statements for all T: Log + ?Sized
#[allow(unused)]
fn forall<T: Log + ?Sized>() {
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
assert_is_log::<Box<T>>();

assert_is_log::<&T>();
Expand Down