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

Commit

Permalink
added option to add support for the log crate (#100)
Browse files Browse the repository at this point in the history
* added option to add support for the log crate

* added logging to ci matrix

* switcth to init_logger_from_env and fixed typo in ci

* fix format error
  • Loading branch information
MnlPhlp authored Aug 10, 2023
1 parent c8551b4 commit a2554fb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
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 -%}
// 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
esp_println::logger::init_logger_from_env();
info!("Logger is setup");
{% endif -%}

println!("Hello world!");

loop {}
Expand Down

0 comments on commit a2554fb

Please sign in to comment.