logging exercise: improved instructions, updated version of env_logger

This commit is contained in:
Nathan Stocks
2021-08-18 22:09:59 -06:00
parent baaa6b69fb
commit faac38254b
5 changed files with 15 additions and 13 deletions

View File

@@ -6,7 +6,9 @@ edition = "2018"
[dependencies]
# I'm glad you came to add the `log` dependency! I got it all ready for you, just uncomment:
log = "0.4"
#
# log = "0.4"
# And here's the env_logger dependency that you'll need in main.rs
env_logger = "0.8"
#
# env_logger = "0.9"

View File

@@ -1,9 +1,10 @@
// 1. Bring the macros error, warn, info, debug, and trace into scope from the log package with a
// 1. Bring the macros `error, warn, info, debug, trace` into scope from the log package with a
// `use` statement.
//
// - You should be able to run `cargo build --lib` successfully after each step.
// You should be able to run `cargo build --lib` successfully after this step (and each step in this
// file)
//
// Hint: You may need to update Cargo.toml first
// Hint: You need to update Cargo.toml to add the `log` dependency, first.
#[derive(Debug)]
pub struct Frog {
@@ -17,14 +18,13 @@ impl Frog {
Default::default()
}
pub fn hop(&mut self) {
// 3. Use info!() to log that a Frog hopped, and how much energy is left
self.energy -= 1;
// 3. Use info!() to log that a Frog hopped, and how much energy is left
if self.energy == 0 {
// 4. Use warn!() to warn that the frog will go to sleep since he ran out of energy
self.sleep();
}
}
// info, error
pub fn sleep(&mut self) {
if !self.sleeping {
// 5. Use error!() to log a (non-fatal) error stating that the Frog is already asleep

View File

@@ -2,16 +2,16 @@
use frogger::Frog;
// You did 1-6 in lib.rs already, right?
// You did #1-#6 in lib.rs already, right?
//
// 7. Bring env_logger into scope. You might need to update Cargo.toml, first.
// 7. Update Cargo.toml to add the `env_logger` dependency
fn main() {
// 8. Initialize env_logger using the init() function
// 8. Initialize env_logger using the init() function at the top level of the library
// 9. Run this program with `cargo run` and take a look at the default output.
// - Now run it again with an explicit log level, like `RUST_LOG=info cargo run`
// - ...and `RUST_LOG=trace cargo run`
// - or `RUST_LOG=trace cargo run`
let mut skippy = Frog::new();
skippy.hop();
skippy.hop();