Skip to content
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

Defmt log / Unknown cmd #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

alexkazik
Copy link

This moves the defmt logging in the lib to a new feature.
This allows to have defmt::Format for the structures and not have the crate-internal logging.
I can rewrite it to be the other way around (new feature for only derive, but I think this is what most people expect).

Also I wondered which unknown command my computer always sends to it, so I've added the command byte to the unknown enum field.
(In my case it was PREVENT ALLOW MEDIUM REMOVAL, which I think is ok to ignore for a "usb-stick".)
There is a doc to show that scsi commands are usually always shown in hex, but debug/defmt is not.

The last commit is a fixup to the previous, which uses a newtype to enforce hex display for that command.

Fell free to pick what looks good.

@apohrebniak
Copy link
Owner

Thanks for the PR! I'll take a look this week

@alexkazik
Copy link
Author

I've found a few improvements after looking at it now, and pushed them.

Copy link
Owner

@apohrebniak apohrebniak left a comment

Choose a reason for hiding this comment

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

Pleasegit fixup and force-push you changes, so I can pull all your commits at once 😃

@@ -4,9 +4,9 @@
macro_rules! trace {
($s:literal $(, $x:expr)* $(,)?) => {
{
#[cfg(feature = "defmt")]
#[cfg(feature = "defmt-log")]
Copy link
Owner

Choose a reason for hiding this comment

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

The idea was to rely on defmt's filtering mechanism: https://defmt.ferrous-systems.com/filtering#disabling-logs
So one would enable a defmt feature on a crate and then compile a binary with DEFMT_LOG=trace,usbd-storage=off cargo run --bin app
Do you have a different use-case?

Copy link
Author

Choose a reason for hiding this comment

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

I'm very new to defmt, if that is the usual way, I'll use it no problem

/// A u8, when printed shown as hex
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct U8Hex(pub u8);
Copy link
Owner

Choose a reason for hiding this comment

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

I like the idea of printing in hex, however I would expand it to all ScsiCommands and UfiCommands and therefore for u16, u64 and u32.
The newtype is an obvious choice but I wonder if we could rely on https://defmt.ferrous-systems.com/hints#propagation instead. If I understand it correctly, then by adding something like :#X to src/fmt.rs macros, we get Hex formatting everywhere for free?

I'm not sure that derive(defmt::Format) would add =u8-like typing info, so maybe it would require an explicit implementation of defmt::Format for ScsiCommand and UfiCommand? (may put them in fmt.rs)

As a last resort, I would still try to implement newtypes for other primitives and see how much of a code change it requires. (I don't mind changing a public API. that's what semver is for :) )

Copy link
Author

Choose a reason for hiding this comment

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

I don't think propagation does help, as entries with display hint (= show hex) can't be overwritten. Also only a struct (or so) can implement Format, but since the command is a u8 it can't be formatted without newtype.

Yes, I should add =u8, and probably use {=u8:#04x} instead of 0x{:02x}.

Copy link
Owner

Choose a reason for hiding this comment

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

I tried

#[derive(defmt::Format)]
enum FakeCommand {
    Unknown {
        u16: u16,
        u32: u32,
        u64: u64,
        u128: u128,
    }
}

#[cortex_m_rt::entry]
fn main() -> ! {
    let foo = FakeCommand::Unknown{
        u16: 0xCAFE,
        u32: 0xCAFE,
        u64: 0xCAFE,
        u128: 0xCAFE,
    };
    defmt::info!("{:#X}", foo);
}

and it printed
Unknown { u16: 0xCAFE, u32: 0xCAFE, u64: 0xCAFE, u128: 0xCAFE }.

So if :#X hints are added to fmt.rs macros, then crate-internal logging will output everything in hex.

Note, that this does not currently work with u64 and u128. Hopefully knurling-rs/defmt#869 will be merged

@apohrebniak
Copy link
Owner

Any updates on this PR?

@alexkazik
Copy link
Author

Sorry, I kinda forgot about it.
But, to continue I'd delete the defmt and hex commits (because there are already established solutions about it, I didn't know) and then it's just reporting the unknown command and a typo - ok?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants