diff --git a/crates/env_filter/Cargo.toml b/crates/env_filter/Cargo.toml index 538ea1e..f28164f 100644 --- a/crates/env_filter/Cargo.toml +++ b/crates/env_filter/Cargo.toml @@ -26,12 +26,13 @@ pre-release-replacements = [ ] [features] -default = ["regex"] -regex = ["dep:regex"] +default = ["std", "regex"] +regex = ["std", "dep:regex"] +std = [] [dependencies] -log = { version = "0.4.8", features = ["std"] } -regex = { version = "1.0.3", optional = true, default-features=false, features=["std", "perf"] } +log = { version = "0.4.8", default-features = false } +regex = { version = "1.0.3", optional = true, default-features = false, features = ["std", "perf"] } [dev-dependencies] snapbox = "0.6" diff --git a/crates/env_filter/src/directive.rs b/crates/env_filter/src/directive.rs index 3721ef7..4d2baa2 100644 --- a/crates/env_filter/src/directive.rs +++ b/crates/env_filter/src/directive.rs @@ -1,3 +1,5 @@ +use alloc::string::String; + use log::Level; use log::LevelFilter; diff --git a/crates/env_filter/src/filter.rs b/crates/env_filter/src/filter.rs index fa28ebd..c81d676 100644 --- a/crates/env_filter/src/filter.rs +++ b/crates/env_filter/src/filter.rs @@ -1,12 +1,14 @@ -use std::env; -use std::fmt; -use std::mem; +use alloc::borrow::ToOwned; +use alloc::string::ToString; +use alloc::vec::Vec; + +use core::fmt; +use core::mem; use log::{LevelFilter, Metadata, Record}; use crate::enabled; use crate::parse_spec; -use crate::parser::ParseResult; use crate::Directive; use crate::FilterOp; use crate::ParseError; @@ -48,10 +50,11 @@ impl Builder { } /// Initializes the filter builder from an environment. + #[cfg(feature = "std")] pub fn from_env(env: &str) -> Builder { let mut builder = Builder::new(); - if let Ok(s) = env::var(env) { + if let Ok(s) = std::env::var(env) { builder.parse(&s); } @@ -98,10 +101,11 @@ impl Builder { /// See the [Enabling Logging] section for more details. /// /// [Enabling Logging]: ../index.html#enabling-logging + #[cfg(feature = "std")] pub fn parse(&mut self, filters: &str) -> &mut Self { #![allow(clippy::print_stderr)] // compatibility - let ParseResult { + let crate::parser::ParseResult { directives, filter, errors, diff --git a/crates/env_filter/src/lib.rs b/crates/env_filter/src/lib.rs index bdc7c8e..744621d 100644 --- a/crates/env_filter/src/lib.rs +++ b/crates/env_filter/src/lib.rs @@ -37,11 +37,14 @@ //! let logger = env_filter::FilteredLog::new(PrintLogger, builder.build()); //! ``` +#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(docsrs, feature(doc_auto_cfg))] #![warn(missing_docs)] #![warn(clippy::print_stderr)] #![warn(clippy::print_stdout)] +extern crate alloc; + mod directive; mod filter; mod filtered_log; diff --git a/crates/env_filter/src/op.rs b/crates/env_filter/src/op.rs index fc10f7d..6d89704 100644 --- a/crates/env_filter/src/op.rs +++ b/crates/env_filter/src/op.rs @@ -1,4 +1,6 @@ -use std::fmt; +use alloc::string::{String, ToString}; + +use core::fmt; #[derive(Debug)] pub(crate) struct FilterOp { @@ -24,13 +26,13 @@ impl FilterOp { #[cfg(not(feature = "regex"))] impl FilterOp { - pub fn new(spec: &str) -> Result { + pub(crate) fn new(spec: &str) -> Result { Ok(Self { inner: spec.to_string(), }) } - pub fn is_match(&self, s: &str) -> bool { + pub(crate) fn is_match(&self, s: &str) -> bool { s.contains(&self.inner) } } diff --git a/crates/env_filter/src/parser.rs b/crates/env_filter/src/parser.rs index 097058d..ae298d6 100644 --- a/crates/env_filter/src/parser.rs +++ b/crates/env_filter/src/parser.rs @@ -1,6 +1,10 @@ +use alloc::borrow::ToOwned; +use alloc::format; +use alloc::string::String; +use alloc::vec::Vec; + +use core::fmt::{Display, Formatter}; use log::LevelFilter; -use std::error::Error; -use std::fmt::{Display, Formatter}; use crate::Directive; use crate::FilterOp; @@ -46,12 +50,16 @@ pub struct ParseError { } impl Display for ParseError { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { write!(f, "error parsing logger filter: {}", self.details) } } -impl Error for ParseError {} +#[cfg(feature = "std")] +impl std::error::Error for ParseError {} + +#[cfg(not(feature = "std"))] +impl core::error::Error for ParseError {} /// Parse a logging specification string (e.g: `crate1,crate2::mod3,crate3::x=error/foo`) /// and return a vector with log directives.