Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

added option to add support for the log crate #100

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[target.{{ rust_target }}]
runner = "espflash flash --monitor"


{% if logging -%}
[env]
ESP_LOGLEVEL="DEBUG"
{% endif -%}

[build]
rustflags = [
"-C", "link-arg=-Tlinkall.x",
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
matrix:
board: ["esp32", "esp32c2", "esp32c3", "esp32c6", "esp32h2", "esp32s2", "esp32s3"]
alloc: ["true", "false"]
logging: ["true", "false"]
action:
- command: build
args: --release
Expand Down Expand Up @@ -68,6 +69,6 @@ jobs:
if: steps.binaries.outcome != 'success'
run: cargo install cargo-generate
- name: Generate
run: cargo generate --path /home/runner/work/esp-template/esp-template/github-esp-template --allow-commands --name test --vcs none --silent -d mcu=${{ matrix.board }} -d advanced=true -d devcontainer=false -d wokwi=false -d alloc=${{ matrix.alloc }} -d ci=false
run: cargo generate --path /home/runner/work/esp-template/esp-template/github-esp-template --allow-commands --name test --vcs none --silent -d mcu=${{ matrix.board }} -d advanced=true -d devcontainer=false -d wokwi=false -d alloc=${{ matrix.alloc }} -d ci=false -d logging=${{ matrix.logging }}
- name: cargo ${{ matrix.action.command }}
run: cd test; cargo ${{ matrix.action.command }} ${{ matrix.action.args }}
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ license = "MIT OR Apache-2.0"
[dependencies]
hal = { package = "{{ mcu }}-hal", version = "{{ hal_version }}" }
esp-backtrace = { version = "0.7.0", features = ["{{ mcu }}", "panic-handler", "exception-handler", "print-uart"] }
{% if logging -%}
esp-println = { version = "0.5.0", features = ["{{ mcu }}","log"] }
log = { version = "0.4.18" }
{% else -%}
esp-println = { version = "0.5.0", features = ["{{ mcu }}"] }
{% endif -%}
{% if alloc -%}
esp-alloc = { version = "0.3.0"}
esp-alloc = { version = "0.3.0" }
{% endif -%}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ After running the command, there will be a few prompts:
- `Configure advanced template options?`: If `false`, skips the rest of the prompts and uses their default value. If `true`, you will be prompted with:
- `Enable allocations via the esp-alloc crate?`: Adds [`esp-alloc`] dependency, and initializes the heap.
- `Configure project to support Wokwi simulation with Wokwi VS Code extension?`: Adds support for Wokwi simulation using [VS Code Wokwi extension].
- `Setup logging using the log crate?`: Adds [`log`] dependency and initializes logging.
- `Configure project to use Dev Containers (VS Code and GitHub Codespaces)?`: Adds support for:
- [VS Code Dev Containers]
- [GitHub Codespaces]
Expand All @@ -31,9 +32,9 @@ For a more detailed explanation about the template, see [Understanding esp-templ
[Wokwi simulator]: https://wokwi.com/
[VS Code Wokwi extension]: https://marketplace.visualstudio.com/items?itemName=wokwi.wokwi-vscode
[web flash]: https://github.com/bjoernQ/esp-web-flash-server
[Understanding esp-template]: https://esp-rs.github.io/book/writing-your-own-application/no-std-applications/understanding-esp-template.html
[Understanding esp-template]: https://esp-rs.github.io/book/writing-your-own-application/generate-project/esp-template.html
[The Rust on ESP Book]: https://esp-rs.github.io/book/

[`log`]: https://docs.rs/log/latest/log/

## License

Expand Down
5 changes: 5 additions & 0 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ type = "bool"
prompt = "Add CI files for GitHub Action?"
default = false

[conditional.'advanced'.placeholders.logging]
type = "bool"
prompt = "Setup logging using the log crate?"
default = false

[conditional.'!devcontainer']
ignore = [
".devcontainer/",
Expand Down
1 change: 1 addition & 0 deletions pre-script.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ if !advanced {
variable::set("alloc", false);
variable::set("devcontainer", false);
variable::set("wokwi", false);
variable::set("logging", false);
}
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ extern crate alloc;
use esp_backtrace as _;
use esp_println::println;
use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc};
{% if logging -%}
use log::info;
{% endif -%}

{%- if alloc %}
#[global_allocator]
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();
Expand Down Expand Up @@ -69,6 +73,15 @@ fn main() -> ! {
wdt1.disable();
{% endif -%}

{% if logging -%}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think using init_logger_from_env() is easier - plus it supports ESP_LOGTARGET.
Difference is it defaults to Off not Info

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True.
This is what i used in my code because i wanted logging in the default mode.
But I think changing it and adding a comment that you have to set ESP_LOGLEVEL to use it should be fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have an idea if there is any way to set the ENV variable somewhere in the cargo config so that cargo run just works and prints logs?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sadly all of that only works for your own crate, not for dependencies (in this case esp_println)
At least i couldn't figure out a way to get log output, but printing ESP_LOGLEVEL from the config in my own code worked.
So it looks like the rustc-env is only applied to your own code and the ENV section only at runtime

Copy link
Member

Choose a reason for hiding this comment

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

No, it works for dependencies too. Smoltcp does its configuration this way. There is a horrible bug in cargo which means it doesn't detect changes with the [env] section: rust-lang/cargo#10358, so make sure whenever you add/change/remove the [env] section do a clean build afterwards.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok
Thanks for the information.

// setup logger
// To change the log_level change the env section in .config/cargo.toml
// or remove it and set ESP_LOGLEVEL manually before running cargo run
// this requires a clean rebuild because of https://github.com/rust-lang/cargo/issues/10358
Comment on lines +77 to +80
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could move this information to the README instead of having it on code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you ever see the readme if you just use cargo generate to generate a project from the template?
I think it is fine because it's just a template and you will look at the code. But we can also add it to the readme additionally

esp_println::logger::init_logger_from_env();
info!("Logger is setup");
{% endif -%}

println!("Hello world!");

loop {}
Expand Down