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
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ jobs:
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@cargo-hack
- name: Default features
run: cargo hack check --each-feature --locked --rust-version --ignore-private --workspace --all-targets --keep-going
run:
- cargo hack check --each-feature --locked --rust-version --ignore-private --package env_logger --all-targets --keep-going
# When the std feature is enabled, a lower MSRV of 1.71 is sufficient and was the MSRV for the crate before std was made optional.
# To make sure we keep it that way, check configurations with std enabled and disabled separately and with their respective MSRVs.
- cargo hack check --feature-powerset --depth 2 --locked --version-range 1.71..=1.71 --ignore-private --package env_filter --all-targets --keep-going --at-least-one-of std,default
- cargo hack check --each-feature --locked --version-range 1.81..=1.81 --ignore-private --package env_filter --all-targets --keep-going --exclude-features std,default
minimal-versions:
name: Minimal versions
strategy:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ unstable-kv = ["kv"]

[dependencies]
log = { version = "0.4.29", features = ["std"] }
env_filter = { version = "1.0.0", path = "crates/env_filter", default-features = false }
env_filter = { version = "1.0.0", path = "crates/env_filter", default-features = false, features = ["std"] }
jiff = { version = "0.2.22", default-features = false, features = ["std"], optional = true }
anstream = { version = "1.0.0", default-features = false, features = ["wincon"], optional = true }
anstyle = { version = "1.0.13", optional = true }
Expand Down
8 changes: 4 additions & 4 deletions crates/env_filter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ keywords = ["logging", "log", "logger"]
repository.workspace = true
license.workspace = true
edition.workspace = true
rust-version.workspace = true
include.workspace = true

[package.metadata.docs.rs]
Expand All @@ -26,12 +25,13 @@ pre-release-replacements = [
]

[features]
default = ["regex"]
default = ["std", "regex"]
regex = ["dep:regex"]
std = ["regex/std"]

[dependencies]
log = { version = "0.4.29", features = ["std"] }
regex = { version = "1.12.3", optional = true, default-features=false, features=["std", "perf"] }
log = { version = "0.4.29", default-features = false }
regex = { version = "1.12.3", optional = true, default-features=false, features=["perf"] }

[dev-dependencies]
snapbox = "1.0"
Expand Down
2 changes: 2 additions & 0 deletions crates/env_filter/src/directive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::string::String;

use log::Level;
use log::LevelFilter;

Expand Down
20 changes: 13 additions & 7 deletions crates/env_filter/src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::env;
use std::fmt;
use std::mem;
use alloc::{borrow::ToOwned, string::ToString, vec::Vec};
use core::{fmt, mem};

use log::{LevelFilter, Metadata, Record};

Expand Down Expand Up @@ -48,10 +47,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);
}

Expand Down Expand Up @@ -108,7 +108,10 @@ impl Builder {
} = parse_spec(filters);

for error in errors {
#[cfg(feature = "std")]
eprintln!("warning: {error}, ignoring it");
#[cfg(not(feature = "std"))]
log::warn!("{error}, ignoring it");
}

self.filter = filter;
Expand Down Expand Up @@ -258,8 +261,9 @@ impl fmt::Debug for Filter {

#[cfg(test)]
mod tests {
use alloc::{borrow::ToOwned, vec, vec::Vec};

use log::{Level, LevelFilter};
use snapbox::{assert_data_eq, str};

use super::{enabled, Builder, Directive, Filter};

Expand Down Expand Up @@ -486,10 +490,12 @@ mod tests {

#[test]
fn try_parse_invalid_filter() {
#[allow(unused_variables)]
let error = Builder::new().try_parse("info,crate1=invalid").unwrap_err();
assert_data_eq!(
#[cfg(feature = "std")]
snapbox::assert_data_eq!(
error,
str!["error parsing logger filter: invalid logging spec 'invalid'"]
snapbox::str!["error parsing logger filter: invalid logging spec 'invalid'"]
);
}

Expand Down
3 changes: 3 additions & 0 deletions crates/env_filter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@
//! ```

#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]
Comment on lines 40 to 44
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a no-std crate, I would expect the following lints added

#![warn(clippy::std_instead_of_core)]
#![warn(clippy::std_instead_of_alloc)]


extern crate alloc;

mod directive;
mod filter;
mod filtered_log;
Expand Down
7 changes: 4 additions & 3 deletions crates/env_filter/src/op.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use alloc::string::{String, ToString};
use core::fmt;

#[derive(Debug, Clone)]
pub(crate) struct FilterOp {
Expand All @@ -24,13 +25,13 @@ impl FilterOp {

#[cfg(not(feature = "regex"))]
impl FilterOp {
pub fn new(spec: &str) -> Result<Self, String> {
pub(crate) fn new(spec: &str) -> Result<Self, String> {
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)
}
}
Expand Down
68 changes: 48 additions & 20 deletions crates/env_filter/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::{borrow::ToOwned, format, string::String, 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;
Expand Down Expand Up @@ -46,12 +47,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.
Expand Down Expand Up @@ -115,9 +120,13 @@ pub(crate) fn parse_spec(spec: &str) -> ParseResult {

#[cfg(test)]
mod tests {
use alloc::{borrow::ToOwned, string::ToString};

use crate::ParseError;
use log::LevelFilter;
use snapbox::{assert_data_eq, str, Data, IntoData};
#[cfg(feature = "std")]
use snapbox::{assert_data_eq, str};
use snapbox::{Data, IntoData};

use super::{parse_spec, ParseResult};

Expand Down Expand Up @@ -164,6 +173,7 @@ mod tests {
assert!(filter.is_none());

assert_eq!(errors.len(), 1);
#[cfg(feature = "std")]
assert_data_eq!(
&errors[0],
str!["invalid logging spec 'crate1::mod1=warn=info'"]
Expand All @@ -185,6 +195,7 @@ mod tests {
assert!(filter.is_none());

assert_eq!(errors.len(), 1);
#[cfg(feature = "std")]
assert_data_eq!(&errors[0], str!["invalid logging spec 'noNumber'"]);
}

Expand All @@ -203,6 +214,7 @@ mod tests {
assert!(filter.is_none());

assert_eq!(errors.len(), 1);
#[cfg(feature = "std")]
assert_data_eq!(&errors[0], str!["invalid logging spec 'wrong'"]);
}

Expand All @@ -221,6 +233,7 @@ mod tests {
assert!(filter.is_none());

assert_eq!(errors.len(), 1);
#[cfg(feature = "std")]
assert_data_eq!(&errors[0], str!["invalid logging spec 'wrong'"]);
}

Expand Down Expand Up @@ -394,6 +407,7 @@ mod tests {
assert!(filter.is_some() && filter.unwrap().to_string() == "a.c");

assert_eq!(errors.len(), 1);
#[cfg(feature = "std")]
assert_data_eq!(
&errors[0],
str!["invalid logging spec 'crate1::mod1=error=warn'"]
Expand Down Expand Up @@ -425,6 +439,7 @@ mod tests {
assert!(filter.is_none());

assert_eq!(errors.len(), 1);
#[cfg(feature = "std")]
assert_data_eq!(
&errors[0],
str!["invalid logging spec 'debug/abc/a.c' (too many '/'s)"]
Expand All @@ -446,14 +461,17 @@ mod tests {
assert!(filter.is_none());

assert_eq!(errors.len(), 2);
assert_data_eq!(
&errors[0],
str!["invalid logging spec 'crate1::mod1=warn=info'"]
);
assert_data_eq!(
&errors[1],
str!["invalid logging spec 'crate3=error=error'"]
);
#[cfg(feature = "std")]
{
assert_data_eq!(
&errors[0],
str!["invalid logging spec 'crate1::mod1=warn=info'"]
);
assert_data_eq!(
&errors[1],
str!["invalid logging spec 'crate3=error=error'"]
);
}
}

#[test]
Expand All @@ -471,8 +489,11 @@ mod tests {
assert!(filter.is_none());

assert_eq!(errors.len(), 2);
assert_data_eq!(&errors[0], str!["invalid logging spec 'noNumber'"]);
assert_data_eq!(&errors[1], str!["invalid logging spec 'invalid'"]);
#[cfg(feature = "std")]
{
assert_data_eq!(&errors[0], str!["invalid logging spec 'noNumber'"]);
assert_data_eq!(&errors[1], str!["invalid logging spec 'invalid'"]);
}
}

#[test]
Expand All @@ -490,18 +511,23 @@ mod tests {
assert!(filter.is_none());

assert_eq!(errors.len(), 2);
assert_data_eq!(
&errors[0],
str!["invalid logging spec 'crate1::mod1=debug=info'"]
);
assert_data_eq!(&errors[1], str!["invalid logging spec 'invalid'"]);
#[cfg(feature = "std")]
{
assert_data_eq!(
&errors[0],
str!["invalid logging spec 'crate1::mod1=debug=info'"]
);
assert_data_eq!(&errors[1], str!["invalid logging spec 'invalid'"]);
}
}

#[test]
fn parse_error_message_single_error() {
#[allow(unused_variables)]
let error = parse_spec("crate1::mod1=debug=info,crate2=debug")
.ok()
.unwrap_err();
#[cfg(feature = "std")]
assert_data_eq!(
error,
str!["error parsing logger filter: invalid logging spec 'crate1::mod1=debug=info'"]
Expand All @@ -510,9 +536,11 @@ mod tests {

#[test]
fn parse_error_message_multiple_errors() {
#[allow(unused_variables)]
let error = parse_spec("crate1::mod1=debug=info,crate2=debug,crate3=invalid")
.ok()
.unwrap_err();
#[cfg(feature = "std")]
assert_data_eq!(
error,
str!["error parsing logger filter: invalid logging spec 'crate1::mod1=debug=info'"]
Expand Down
Loading