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

[RFC] Add logging support #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
68 changes: 68 additions & 0 deletions text/0000-logger_support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
- Feature Name: `Logger support`
- Start Date: 2019-12-28
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary
[summary]: #summary

Add support for interrupt priority based logging in RTFM (ala [`cortex-m-funnel`]).

# Motivation
[motivation]: #motivation

One of the most time consuming issues of embedded programming is debugging. And especially when it comes to event based systems where debugging can get in the way of timing requirements. Debugging is often done through logging with `printf`-like methods, and the logger implementation will often impede real-time requirements, causing the system to behave differently with and without logging enabled.
This often comes from logger implementations having global critical sections for accessing the log buffers, but with RTFM we can have seamless support for logging without critical sections by leveraging the design and ideas behind [`cortex-m-funnel`].

The idea behind it is that there exists one logging queue per interrupt priority, and a logging function always writes to the currently running priority which in turn means that no critical sections are needed (see [`cortex-m-funnel`] for implementation details).

Adding this feature will help users immensely, as figuring out logging is not a trivial task. This will also help adoption of RTFM as **we made logging easy**.

# Detailed design
[design]: #detailed-design

The important thing is to define how large the buffers should be. Either we can go with the simplest case (and have all buffers the same size):

```rust
#[app(logger = 256)]
// ...
```

Or we can have a base size, together with possible specialization (bikeshed of API needed):

```rust
// Base size of 256 bytes, but the queue for priority 7 is of 1024 bytes
#[app(logger = [base = 256, 7 = 1024])]
Copy link
Contributor

Choose a reason for hiding this comment

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

The syntax of the #[app] arguments can be anything; the downside is that it never gets formatted by rustfmt. I personally would prefer not to enter so much data as an attribute arguments but can't think of a better syntax right now.

// ...
```

The final implementation of the logging is very simple. Simply, based on the analysis, fill out the [`cortex-m-funnel`] macro.
Copy link
Contributor

Choose a reason for hiding this comment

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

cortex-m-funnel only supports ufmt atm; do we want to wait for core::fmt support before implementing this?


One thing we should be careful with is that the logger should be **external** to RTFM so it is easy to include in libraries. Just as the [`log`] crate is today.

## Should the log be part of task definition?

It is my view that it should not. We should follow the common design of [`log`] and [`cortex-m-funnel`], where the log macros can be added anywhere without propagating it down through the call tree. This is the key feature that makes it easy to use, just have RTFM provide the information to properly initialize the queues for the logger.

# How We Teach This
[how-we-teach-this]: #how-we-teach-this

This should be added to the book as its own `Safe logging` chapter.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would name the chapter 'non-blocking logging', 'real-time logging' or something like that. cortex-m-funnel is not the only memory safe global logger; the log and stlog crates also provide memory safe global loggers and would also be candidates for a 'Safe logging' chapter.


# Drawbacks
[drawbacks]: #drawbacks

* Increase of complexity of RTFM, but I would argue that this is of much greater help than not to have it.
Copy link
Contributor

Choose a reason for hiding this comment

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

it also ties RTFM semver to cortex-m-funnel version so if we release a RTFM that uses cortex-m-funnel v0.1 it probably won't work with the log! macro of cortex-m-funnel v0.2. I'm unsure about what kind of error you would get in that case; perhaps a linker error or perhaps all data that goes through the v0.2 of log! would be silently loss


# Alternatives
[alternatives]: #alternatives

* Use `cortex-m-funnel` manually, however this would be easy to get wrong

# Unresolved questions
[unresolved]: #unresolved-questions

* API design

[`cortex-m-funnel`]: https://github.com/japaric/cortex-m-funnel
[`log`]: https://github.com/rust-lang/log