From cded60ad49bcd84c86c895014c7307864c7d6005 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 27 Oct 2023 17:11:59 -0500 Subject: [PATCH] Traaace --- src/main.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7855f4d..403ea52 100644 --- a/src/main.rs +++ b/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> { + // 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