-
-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
target-side env_logger
-like env filter
#519
Conversation
macros/src/env_filter.rs
Outdated
|
||
// TODO unclear if we want the same behavior as `env_logger` | ||
#[test] | ||
fn when_duplicates_entries_in_defmt_log_use_last_entry() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, now I see that this an override mechanism (from right to left) which is a reasonable thing to have. For example, RUST_LOG=info,krate=trace
means: "emit info level logs from all crates, but (override) for the krate
emit trace level logs"
one big difference between this approach and the current one is that: which profile ( $ # see issue description example
$ # defmt 0.2.0
$ cargo r --bin hello
0 ERROR a
1 WARN b
2 INFO c
3 DEBUG d
4 TRACE e
$ # defmt 0.2.0
$ cargo r --bin hello --release
0 ERROR a
1 WARN b
2 INFO c
$ # this PR
$ cargo r --bin hello
0 ERROR a
$ # this PR
$ cargo r --bin hello --release
0 ERROR a |
I think another important thing to note is that this removes the ability to set a default log level. This is potentially useful when writing tests, when you don't want to set the same environment variable all the time. (something like dotenv can work around this, but that seems suboptimal, since it's a third-party tool) |
@jonas-schievink: You mean that one can't set a default for your own crate? Cause afai understand there are still some default levels. Do you see any other use cases where this would be a "problem" (or annoyance), except testing? |
Right, there's a fixed default to |
that line of code means log at this level or higher "severity". For instance if you set Lines 137 to 144 in fde2aab
So trace < debug < info < warn < error in terms of severity. This is the same as the log crate.
|
good point. I have noted this down in the issue description as a reminder to add it to the documentation. thinking about the case of testing, in std::env::set_var("RUST_LOG", "my_crate=trace");
env_logger::init(); this is not possible with in the particular case, of testing embedded code the only way to set |
Okay, I think I got it now:
Correct? I guess I was mainly confused about the two different defaults. |
yes, that is correct. The "defaults" match the behavior of the |
pushed very WIP module path support. This works: fn main() -> ! {
defmt::info!("hello");
foo::foo();
bar::bar();
app::exit()
}
mod foo {
pub fn foo() {
defmt::info!("foo");
}
}
mod bar {
pub fn bar() {
defmt::info!("bar");
}
} $ DEFMT_LOG=hello::foo cargo r --bin hello
0 INFO foo
└─ hello::foo::foo @ src/bin/hello.rs:16
$ DEFMT_LOG=hello::bar cargo r --bin hello
0 INFO bar
└─ hello::bar::bar @ src/bin/hello.rs:22
$ DEFMT_LOG=hello::foo,hello::bar cargo r --bin hello
0 INFO foo
└─ hello::foo::foo @ src/bin/hello.rs:16
1 INFO bar
└─ hello::bar::bar @ src/bin/hello.rs:22
$ DEFMT_LOG=hello cargo r --bin hello
0 INFO hello
└─ hello::__cortex_m_rt_main @ src/bin/hello.rs:8
1 INFO foo
└─ hello::foo::foo @ src/bin/hello.rs:16
2 INFO bar
└─ hello::bar::bar @ src/bin/hello.rs:22 |
status update: jonas-schievink pointed out that with this change, the To address the issue, I have proposed adding a |
Update: I have fleshed out the RFC text in the PR description and moved it to issue #571. This PR is blocked on that RFC being approved and |
@@ -346,9 +346,19 @@ fn test_single_snapshot( | |||
args.extend_from_slice(&["--features", features]); | |||
} | |||
|
|||
// matches the behavior of the old Cargo-feature-based log filter | |||
// TODO(japaric) the new log filter doesn't depend on the compilation profile so |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I plan to do this in a follow-up PR. I want to avoid the churn in this PR.
env_logger
-like env filterenv_logger
-like env filter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good.
One question regarding file-structure: Shouldn't macros/src/function_like/log.rs
be macros/src/function_like/log/mod.rs
? That's the usual structure I know.
(We can also fix this in a follow-up PR, since it seems that other modules in macros
also not comply with it)
bors r=Urhengulas |
Merge conflict. |
implements RFC #571
fixes test-book
rebased |
Build succeeded: |
600: only run snapshot & backcompat tests in dev mode r=jonas-schievink a=japaric the log filter no longer depends on the compilation profile so there's no longer a reason to run these tests in release mode follow up of #519 (comment) Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
547: Migration guide `v0.2.x` to `v0.3.0` r=japaric a=Urhengulas Migration guide from `defmt v0.2.x` to version `v0.3.0`. https://deploy-preview-547--admiring-dubinsky-56dff5.netlify.app/migration-02-03.html Fixes #530. ### TODO - [x] #505: Logger trait v2 - [x] #521: [3/n] Remove u24 - [x] #522: Replace `µs` hint with `us` - [x] (no adaption needed) ~~#508: [5/n] Format trait v2~~ - [x] #519: `DEFMT_LOG` - [x] #550: `defmt::flush()` - [x] knurling-rs/probe-run#198, knurling-rs/probe-run#250, Co-authored-by: Johann Hemmann <johann.hemmann@code.berlin>
This PR implements an alternative approach to disabling/enabling logging in crates. Currently we use Cargo features.
This PR removes the need for those Cargo features and replaces them with the env variable:
DEFMT_LOG
.The
DEFMT_LOG
env var specifies which crates should emit defmt logs and at which level. Its syntax is the same as the syntax used by theenv_logger
crate.TheNo longer true,DEFMT_LOG
env var parser currently rejects paths that include modules: soDEFMT_LOG=krate=info
is OK, butDEFMT_LOG=krate::module=info
is rejected. This is intentional and should let us accept module paths and filter based on modules in the future (as in add module support in a backwards compatible fashion).DEFMT_LOG
now supports module paths.One of the main advantages of
DEFMT_LOG
is that allows filtering logs with module level granularity. The current Cargo features based approach is more coarse grained and only supports enabling / disabling entire crates.NOTE this is a target side emit filter and orthogonal to the host side display filter proposed in knurling-rs/probe-run#74. Disabling logs with the target filter makes the target program smaller (, maybe also slightly faster) and it reduces the amount of data sent from the target to the host. Disabling logs with the host filter does not change the target program at all not it changes the amount of data transmitted from the target to the host.
Changing the value of the
DEFMT_LOG
env var causes all crates that transitively depend on thedefmt_macros
crate to be recompiled.Example usage:
$ bat src/bin/hello.rs
how to test this
as this is not even merged into the
main
branch and breaking changes (with respect to defmt v0.2.0) have landed intodefmt
, the whole process is quite involvedprobe-run
that uses the decoder in this branch[patch.crates-io]
inCargo.toml
(the one in the root of the workspace) to use the git version ofdefmt
instead of v0.2.x. Theapp-template
already includes this section in itsCargo.toml
IMPORTANT use
branch = "env-filter"
instead ofrev = "somehash"
PROBE_RUN_IGNORE_VERSION
env variable before you run your firmware e.g.$ PROBE_RUN_IGNORE_VERSION=1 cargo run --bin hello
DEFMT_LOG
env variable!As there's other breaking-change work currently ongoing (#508) I plan to leave this work on hold for now -- but will be answering to feedback! -- and resume it when that's concluded.
Unresolved questions
DEFMT_LOG
toDEFMT_CAPTURE
orDEFMT_TARGET_CAPTURE
? if we keep theDEFMT_LOG
name, what should we name the env var to be used in display filter via env variable probe-run#74?TODO list
DEFMT_LOG=info
off
pseudo log-level (disables logging)DEFMT_LOG=krate::module
doesn't matchkrate::module1
,krate::module2
, etc.dev
/profile
) has no effect on the log level filtercloses #183
closes #255
closes #459