-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
Compatibility for log
logging macros?
#149
Comments
log
logging macros?log
logging macros?
I believe we now handle the common cases with #270. Whether we want to support the entire |
It’d be great to have the user doc indicate any existing compatibility with the log crate, and perhaps an example. Compatibility was my first question. Thanks. |
I'm new to embedded programming (trying out an stm32f10 nucleo board). However I stumbled straight away on that I tried working around the problem by implement the glue myself: use log::{Level, Log, Metadata, Record};
static DEFMT_LOGGER: DefmtLogger = DefmtLogger;
struct DefmtLogger;
impl Log for DefmtLogger {
fn enabled(&self, _metadata: &Metadata) -> bool {
true
}
fn log(&self, record: &Record) {
match record.level() {
Level::Error => defmt::error!("{}", record.args().as_str()),
Level::Warn => defmt::warn!("{}", record.args().as_str()),
Level::Info => defmt::info!("{}", record.args().as_str()),
Level::Debug => defmt::debug!("{}", record.args().as_str()),
Level::Trace => defmt::trace!("{}", record.args().as_str()),
}
}
fn flush(&self) {
defmt::flush();
}
}
fn init_log() {
log::set_logger(&DEFMT_LOGGER).unwrap();
} But I guess there's something missing in my understanding, cause this doesn't seem to work. |
Hi @algesten, It is great to see you tapping your toe into embedded development! You are right that Please have a look into our user documentation at https://defmt.ferrous-systems.com/ for a overview of what Greetings 👋🏾 |
Hi @Urhengulas! Thanks for the kind words! I understand the motivation, and after working with defmt for a day, nothing was particularly confusing. Thanks! Would you be open for me making a PR to README.md that explains these things for beginners like me? Far down the page :) |
That is good to hear!
Yes, definitely! Either to the Readme or to the book (https://defmt.ferrous-systems.com). |
While defmt is shaping up great for embedded logging, the nice thing about the normal
log
macros is that they can be used in code that's used both in embedded and non-embedded contexts, such as:Nontrivial libraries like
smoltcp
that are used in both contexts, where high-bandwidth logging is desirable in both cases.Parts of a firmware that are used non-embedded for testing and simulation purposes. It can be quite time saving to extract the hardware independent parts into a library crate, where they can be tested on normal CI infrastructure, and used to simulate the firmware's behavior.
In both cases, using
defmt
would make this cumbersome, needing to make the logging conditional somehow.I'm opening this issue for a discussion if it is possible to provide, as a part of
defmt
, a shim that would keep logging usable for both contexts, with acceptable restrictions. The first thing coming to mind is a macro that makes a transformation like this:i.e. the format string is rewritten for
log
and the two versions emitted side by side. Of course this can't expect to deliver the same output for all cases (bitfields come to mind), but IMO a best effort would make life much easier for the overwhelming majority of cases.The text was updated successfully, but these errors were encountered: