From a4d5457771dbc51e8b4c21ce093cfad9a5b74223 Mon Sep 17 00:00:00 2001 From: Tait Hoyem <tait@tait.tech> Date: Wed, 5 Jun 2024 11:24:56 -0600 Subject: [PATCH 1/6] Use no_std compatible types std::env -> core::option_env --- macros/src/lib.rs | 13 ++++++++++--- src/lib.rs | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index c78f9e6..077d10a 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,7 +1,16 @@ // Copyright (C) 2019-2023 Daniel Mueller <deso@posteo.net> // SPDX-License-Identifier: (Apache-2.0 OR MIT) +#![no_std] + extern crate proc_macro; +extern crate alloc; + +use alloc::{ + string::String, + vec, + vec::Vec, +}; use proc_macro::TokenStream; use proc_macro2::TokenStream as Tokens; @@ -199,10 +208,8 @@ fn expand_tracing_init(attribute_args: &AttributeArgs) -> Tokens { let __internal_event_filter = { use ::test_log::tracing_subscriber::fmt::format::FmtSpan; - match ::std::env::var_os("RUST_LOG_SPAN_EVENTS") { + match ::core::option_env!("RUST_LOG_SPAN_EVENTS") { Some(mut value) => { - value.make_ascii_lowercase(); - let value = value.to_str().expect("test-log: RUST_LOG_SPAN_EVENTS must be valid UTF-8"); value .split(",") .map(|filter| match filter.trim() { diff --git a/src/lib.rs b/src/lib.rs index 59084b9..51b413a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: (Apache-2.0 OR MIT) #![deny(missing_docs)] +#![no_std] //! A crate providing a replacement #[[macro@test]] attribute that //! initializes logging and/or tracing infrastructure before running From b7306ef19d0192d853d7f1aab84748cabad48f6a Mon Sep 17 00:00:00 2001 From: Tait Hoyem <tait@tait.tech> Date: Sun, 9 Jun 2024 20:50:30 -0600 Subject: [PATCH 2/6] Cargo format --- macros/src/lib.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 077d10a..7dff2c5 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -3,14 +3,12 @@ #![no_std] -extern crate proc_macro; extern crate alloc; +extern crate proc_macro; -use alloc::{ - string::String, - vec, - vec::Vec, -}; +use alloc::string::String; +use alloc::vec; +use alloc::vec::Vec; use proc_macro::TokenStream; use proc_macro2::TokenStream as Tokens; From 1a5630359fca8a8f1fbba3d07d1bce0f7014cb32 Mon Sep 17 00:00:00 2001 From: Tait Hoyem <tait@tait.tech> Date: Fri, 14 Jun 2024 08:55:30 -0600 Subject: [PATCH 3/6] Add std features, enabled by default fmt --- Cargo.toml | 5 +++-- macros/Cargo.toml | 2 ++ macros/src/lib.rs | 13 +++++++++++-- src/lib.rs | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e78b25..65cd0f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,8 @@ path = "tests/default_log_filter.rs" required-features = ["log", "unstable"] [features] -default = ["log", "color"] +default = ["log", "color", "std"] +std = ["test-log-macros/std"] trace = ["dep:tracing-subscriber", "test-log-macros/trace"] log = ["dep:env_logger", "test-log-macros/log", "tracing-subscriber?/tracing-log"] color = ["env_logger?/auto-color", "tracing-subscriber?/ansi"] @@ -45,7 +46,7 @@ unstable = ["test-log-macros/unstable"] members = ["macros"] [dependencies] -test-log-macros = {version = "0.2.15", path = "macros"} +test-log-macros = {version = "0.2.15", default-features = false, path = "macros"} tracing-subscriber = {version = "0.3.17", default-features = false, optional = true, features = ["env-filter", "fmt"]} env_logger = {version = "0.11", default-features = false, optional = true} diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 09aa829..095bbd4 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -14,6 +14,8 @@ Supporting procedural macro crate for test-log. proc-macro = true [features] +default = ["std"] +std = [] trace = [] log = [] unstable = [] diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 7dff2c5..2fde2ae 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,7 +1,7 @@ // Copyright (C) 2019-2023 Daniel Mueller <deso@posteo.net> // SPDX-License-Identifier: (Apache-2.0 OR MIT) -#![no_std] +#![cfg_attr(not(feature = "std"), no_std)] extern crate alloc; extern crate proc_macro; @@ -206,7 +206,16 @@ fn expand_tracing_init(attribute_args: &AttributeArgs) -> Tokens { let __internal_event_filter = { use ::test_log::tracing_subscriber::fmt::format::FmtSpan; - match ::core::option_env!("RUST_LOG_SPAN_EVENTS") { + #[cfg(feature = "std")] + let env_var = match ::std::env::var("RUST_LOG_SPAN_EVENTS") { + Ok(val) => Some(val), + Err(::std::env::VarError::NotPresent) => None, + Err(::std::env::VarError::NotUnicode(os_string)) => panic!("RUST_LOG_SPAN_EVENTS must be unicode compliant, found {os_string:?}"), + }; + #[cfg(not(feature = "std"))] + let env_var = ::core::option_env!("RUST_LOG_SPAN_EVENTS"); + + match env_var { Some(mut value) => { value .split(",") diff --git a/src/lib.rs b/src/lib.rs index 51b413a..44b51f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: (Apache-2.0 OR MIT) #![deny(missing_docs)] -#![no_std] +#![cfg_attr(not(feature = "std"), no_std)] //! A crate providing a replacement #[[macro@test]] attribute that //! initializes logging and/or tracing infrastructure before running From 296744073e203d0efe514cd5bfbbfd76bd8effb6 Mon Sep 17 00:00:00 2001 From: Tait Hoyem <tait@tait.tech> Date: Fri, 14 Jun 2024 09:32:51 -0600 Subject: [PATCH 4/6] Add no_std test, make 'log' depend on 'std' cleanup --- Cargo.toml | 7 ++++++- tests/no_std_required.rs | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/no_std_required.rs diff --git a/Cargo.toml b/Cargo.toml index 65cd0f0..1f7cd66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,11 @@ tracing infrastructure before running tests. """ include = ["src/lib.rs", "LICENSE-*", "README.md", "CHANGELOG.md"] +[[test]] +name = "no_std_test" +path = "tests/no_std_required.rs" +required-features = ["trace"] + [[test]] name = "default_log_filter" path = "tests/default_log_filter.rs" @@ -36,7 +41,7 @@ required-features = ["log", "unstable"] default = ["log", "color", "std"] std = ["test-log-macros/std"] trace = ["dep:tracing-subscriber", "test-log-macros/trace"] -log = ["dep:env_logger", "test-log-macros/log", "tracing-subscriber?/tracing-log"] +log = ["dep:env_logger", "test-log-macros/log", "tracing-subscriber?/tracing-log", "std"] color = ["env_logger?/auto-color", "tracing-subscriber?/ansi"] # Enable unstable features. These are generally exempt from any semantic # versioning guarantees. diff --git a/tests/no_std_required.rs b/tests/no_std_required.rs new file mode 100644 index 0000000..d7c0401 --- /dev/null +++ b/tests/no_std_required.rs @@ -0,0 +1,10 @@ +#![no_std] + +use test_log::test; +use tracing::debug; + +#[test] +fn test_without_std() { + debug!("It works without std!"); + assert_eq!(1 + 1, 2); +} From a738bd2fa94250d2edebeab918d8c39c48facb0b Mon Sep 17 00:00:00 2001 From: Tait Hoyem <tait@tait.tech> Date: Fri, 14 Jun 2024 09:47:28 -0600 Subject: [PATCH 5/6] Add additional test step that tests the 'no_std_required' file --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5b64511..b413d02 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -37,6 +37,7 @@ jobs: cargo build --profile=${{ matrix.profile }} cargo build --all-features --profile=${{ matrix.profile }} cargo test --profile=${{ matrix.profile }} + cargo test --no-default-features --features trace no_std_required --profile=${{ matrix.profile }} build-minimum: name: Build using minimum versions of dependencies runs-on: ubuntu-latest From a9bb05ea92936ff08f4278033f9a5f46d697a369 Mon Sep 17 00:00:00 2001 From: Tait Hoyem <tait@tait.tech> Date: Fri, 14 Jun 2024 09:47:37 -0600 Subject: [PATCH 6/6] Rename test to match file --- tests/no_std_required.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/no_std_required.rs b/tests/no_std_required.rs index d7c0401..5aba62d 100644 --- a/tests/no_std_required.rs +++ b/tests/no_std_required.rs @@ -4,7 +4,7 @@ use test_log::test; use tracing::debug; #[test] -fn test_without_std() { +fn no_std_required() { debug!("It works without std!"); assert_eq!(1 + 1, 2); }