logging exercise: improved instructions, updated version of env_logger
This commit is contained in:
@@ -6,4 +6,4 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
env_logger = "0.8"
|
env_logger = "0.9"
|
||||||
|
|||||||
@@ -8,4 +8,4 @@ edition = "2018"
|
|||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
puzzles = { path = "../puzzles" }
|
puzzles = { path = "../puzzles" }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
env_logger = "0.8"
|
env_logger = "0.9"
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# I'm glad you came to add the `log` dependency! I got it all ready for you, just uncomment:
|
# 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
|
# And here's the env_logger dependency that you'll need in main.rs
|
||||||
env_logger = "0.8"
|
#
|
||||||
|
# env_logger = "0.9"
|
||||||
|
|||||||
@@ -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.
|
// `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)]
|
#[derive(Debug)]
|
||||||
pub struct Frog {
|
pub struct Frog {
|
||||||
@@ -17,14 +18,13 @@ impl Frog {
|
|||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
pub fn hop(&mut self) {
|
pub fn hop(&mut self) {
|
||||||
// 3. Use info!() to log that a Frog hopped, and how much energy is left
|
|
||||||
self.energy -= 1;
|
self.energy -= 1;
|
||||||
|
// 3. Use info!() to log that a Frog hopped, and how much energy is left
|
||||||
if self.energy == 0 {
|
if self.energy == 0 {
|
||||||
// 4. Use warn!() to warn that the frog will go to sleep since he ran out of energy
|
// 4. Use warn!() to warn that the frog will go to sleep since he ran out of energy
|
||||||
self.sleep();
|
self.sleep();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// info, error
|
|
||||||
pub fn sleep(&mut self) {
|
pub fn sleep(&mut self) {
|
||||||
if !self.sleeping {
|
if !self.sleeping {
|
||||||
// 5. Use error!() to log a (non-fatal) error stating that the Frog is already asleep
|
// 5. Use error!() to log a (non-fatal) error stating that the Frog is already asleep
|
||||||
|
|||||||
@@ -2,16 +2,16 @@
|
|||||||
|
|
||||||
use frogger::Frog;
|
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() {
|
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.
|
// 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`
|
// - 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();
|
let mut skippy = Frog::new();
|
||||||
skippy.hop();
|
skippy.hop();
|
||||||
skippy.hop();
|
skippy.hop();
|
||||||
|
|||||||
Reference in New Issue
Block a user