Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions crates/env_filter/Cargo.toml
Copy link
Contributor

Choose a reason for hiding this comment

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

refactor(filter): Add support for no_std environments

Mind fixing your commit type?

A refactor, by definition, has no user facing changes while this does. I would use feat

Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ 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"] }
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
16 changes: 10 additions & 6 deletions crates/env_filter/src/filter.rs
Original file line number Diff line number Diff line change
@@ -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;
Comment on lines +1 to 10
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

use crate::parse_spec;
use crate::parser::ParseResult;
use crate::Directive;
use crate::FilterOp;
use crate::ParseError;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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,
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 @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions crates/env_filter/src/op.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::fmt;
use alloc::string::{String, ToString};

use core::fmt;
Comment on lines +1 to +3
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you group these?


#[derive(Debug)]
pub(crate) struct FilterOp {
Expand All @@ -24,13 +26,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 {
Comment on lines +29 to +35
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you split this out into a separate commit?

s.contains(&self.inner)
}
}
Expand Down
16 changes: 12 additions & 4 deletions crates/env_filter/src/parser.rs
Original file line number Diff line number Diff line change
@@ -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;
Comment on lines +1 to 10
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're changing grouping, could you do

use {core,alloc,std}

use third-party

use crate

Expand Down Expand Up @@ -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 {}
Comment on lines +61 to +62
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes the lack of std require a higher MSRV


/// Parse a logging specification string (e.g: `crate1,crate2::mod3,crate3::x=error/foo`)
/// and return a vector with log directives.
Expand Down