Traaace
This commit is contained in:
parent
448a11d474
commit
cded60ad49
45
src/main.rs
45
src/main.rs
|
@ -1,16 +1,22 @@
|
|||
#![deny(clippy::all)]
|
||||
#![allow(clippy::blanket_clippy_restriction_lints)]
|
||||
#![forbid(
|
||||
clippy::style,
|
||||
clippy::pedantic,
|
||||
// clippy::style,
|
||||
// clippy::pedantic,
|
||||
clippy::nursery,
|
||||
clippy::restriction,
|
||||
// clippy::restriction,
|
||||
// clippy::cargo
|
||||
)]
|
||||
|
||||
//! my code and notes from the book "Learn Rust in a Month of Lunches"
|
||||
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use tracing::debug;
|
||||
use tracing::info;
|
||||
use tracing::span;
|
||||
use tracing::warn;
|
||||
use tracing::Level;
|
||||
use tracing_subscriber::fmt::init as init_trace_logger;
|
||||
|
||||
/// a doc comment - use `cargo doc --open` to see it?
|
||||
|
@ -30,9 +36,42 @@ fn main() {
|
|||
};
|
||||
|
||||
{
|
||||
// ch 3
|
||||
let me = "meeeeeeeeeeeeeeeeeeeeeeeeeee";
|
||||
info!("Hello, {me: <4.12}, meet the real {me}.");
|
||||
};
|
||||
|
||||
let span = span!(Level::TRACE, "outer_span");
|
||||
let _enter = span.enter();
|
||||
|
||||
let span = span!(Level::TRACE, "shaving_yaks");
|
||||
let _enter = span.enter();
|
||||
|
||||
info!("shaving yaks");
|
||||
|
||||
let _ = shave(3);
|
||||
let result = shave(4);
|
||||
info!("Shave result: {result:?}");
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub fn shave(yak: usize) -> Result<(), Box<dyn Error + 'static>> {
|
||||
// this creates an event at the DEBUG level with two fields:
|
||||
// - `excitement`, with the key "excitement" and the value "yay!"
|
||||
// - `message`, with the key "message" and the value "hello! I'm gonna shave a yak."
|
||||
//
|
||||
// unlike other fields, `message`'s shorthand initialization is just the string itself.
|
||||
debug!(excitement = "yay!", "hello! I'm gonna shave a yak.");
|
||||
if yak == 3 {
|
||||
warn!("could not locate yak!");
|
||||
// note that this is intended to demonstrate `tracing`'s features, not idiomatic
|
||||
// error handling! in a library or application, you should consider returning
|
||||
// a dedicated `YakError`. libraries like snafu or thiserror make this easy.
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "shaving yak failed!").into());
|
||||
} else {
|
||||
debug!("yak shaved successfully");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// if I were to write a webapp, I would like to avoid postgres
|
||||
|
|
Loading…
Reference in a new issue