From 534c04bd1137824c3d025bda94f6818992652a7c Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Fri, 17 Mar 2023 19:18:23 -0400 Subject: [PATCH] feat: Improve logging --- Cargo.lock | 37 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- src/bin/stranger_site_gen.rs | 11 ++++++++++- src/site.rs | 14 +++++++++++--- src/site/page.rs | 16 +++++++++++++--- src/site/post.rs | 16 +++++++++++++--- 6 files changed, 85 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c548ec4..1d5b023 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -594,6 +594,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata", +] + [[package]] name = "memchr" version = "2.5.0" @@ -840,6 +849,30 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +dependencies = [ + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "rustc-demangle" version = "0.1.21" @@ -1110,10 +1143,14 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" dependencies = [ + "matchers", "nu-ansi-term", + "once_cell", + "regex", "sharded-slab", "smallvec", "thread_local", + "tracing", "tracing-core", "tracing-log", ] diff --git a/Cargo.toml b/Cargo.toml index 6aa2bae..88b40fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,4 @@ serde = { version = "1.0.156", features = ["derive"] } serde_dhall = { version = "0.12.1", default-features = false } snafu = "0.7.4" tracing = "0.1.37" -tracing-subscriber = "0.3.16" +tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } diff --git a/src/bin/stranger_site_gen.rs b/src/bin/stranger_site_gen.rs index 4fd9054..4deaffc 100644 --- a/src/bin/stranger_site_gen.rs +++ b/src/bin/stranger_site_gen.rs @@ -1,6 +1,8 @@ mod cli; use cli::Cli; +use tracing_subscriber::prelude::*; +use tracing_subscriber::{fmt, EnvFilter}; use clap::Parser; use color_eyre::eyre::Result; @@ -8,7 +10,14 @@ use color_eyre::eyre::Result; fn main() -> Result<()> { // Setup color eyre and logging color_eyre::install()?; - tracing_subscriber::fmt::init(); + let fmt_layer = fmt::layer().pretty(); + let filter_layer = EnvFilter::try_from_default_env() + .or_else(|_| EnvFilter::try_new("info")) + .unwrap(); + tracing_subscriber::registry() + .with(filter_layer) + .with(fmt_layer) + .init(); let args = Cli::parse(); args.command.run(args.global_args) diff --git a/src/site.rs b/src/site.rs index 3043f0f..f43feed 100644 --- a/src/site.rs +++ b/src/site.rs @@ -8,7 +8,7 @@ use std::{ }; use snafu::{ensure, ResultExt, Snafu}; -use tracing::{debug, instrument}; +use tracing::{debug, info, info_span}; use self::{config::Config, page::Page, post::Post}; @@ -126,20 +126,25 @@ impl Site { /// /// Will return an error if serialization fails, the user does not have write permissions for /// the directory, or any other IO errors occur while writing out the config. - #[instrument(skip(site_dir))] pub fn write(&self, site_dir: impl AsRef) -> Result<(), SiteError> { let site_dir = site_dir.as_ref(); - debug!(?site_dir); + // Setup logging + let span = info_span!("Site::write", ?site_dir); + let _enter = span.enter(); + info!("Initializing/Updating site repository"); // Make sure the site_dir is a directory, if it exists, otherwise create it if site_dir .try_exists() .context(ExistanceCheckSnafu { path: site_dir })? { + debug!("Validating site directory"); ensure!(site_dir.is_dir(), NotADirectorySnafu { path: site_dir }); } else { + debug!("Creating site directory"); create_dir_all(site_dir).context(CreateDirectorySnafu { path: site_dir })?; } // Serialize the config + debug!(?self.config, "Writing out the config"); let config_path = site_dir.join("config.dhall"); let serialized_config = serde_dhall::serialize(&self.config) .to_string() @@ -165,11 +170,13 @@ impl Site { .try_exists() .context(ExistanceCheckSnafu { path: &statics_dir })? { + debug!(?statics_dir, "Validating statics directory"); ensure!( statics_dir.is_dir(), NotADirectorySnafu { path: &statics_dir } ); } else { + debug!(?statics_dir, "Creating statics directory"); // Create the directory create_dir_all(&statics_dir).context(CreateDirectorySnafu { path: &statics_dir })?; // Touch a .gitkeep @@ -178,6 +185,7 @@ impl Site { } // Create the gitignore let git_ignore_path = site_dir.join(".gitignore"); + debug!(?git_ignore_path, "Creating gitignore"); let mut git_ignore = File::create(&git_ignore_path).context(WriteConfigSnafu { path: &git_ignore_path, })?; diff --git a/src/site/page.rs b/src/site/page.rs index f1d0dee..be4f789 100644 --- a/src/site/page.rs +++ b/src/site/page.rs @@ -8,7 +8,7 @@ use std::{ use serde::{Deserialize, Serialize}; use snafu::{ensure, ResultExt}; -use tracing::{debug, instrument}; +use tracing::{debug, info, info_span}; use super::{ ConfigReifySnafu, CreateDirectorySnafu, ExistanceCheckSnafu, NotADirectorySnafu, SiteError, @@ -66,7 +66,6 @@ impl Page { /// /// Will return an error if serialization fails, the user does not have write permissions for /// the directory, or any other IO errors occur while writing out the config. - #[instrument(skip(site_path, page_dir))] pub fn write( &self, site_path: impl AsRef, @@ -74,18 +73,24 @@ impl Page { ) -> Result<(), SiteError> { // build the page_dir let page_dir = site_path.as_ref().join("pages").join(page_dir.as_ref()); - debug!(?page_dir); + // Setup logging + let span = info_span!("Page::write", ?page_dir); + let _enter = span.enter(); + info!("Writing out page"); // Make sure the page path is a dir if it exists, otherwise create it if page_dir .try_exists() .context(ExistanceCheckSnafu { path: &page_dir })? { + debug!("Validating page directory"); ensure!(page_dir.is_dir(), NotADirectorySnafu { path: &page_dir }); } else { + debug!("Creating page directory"); create_dir_all(&page_dir).context(CreateDirectorySnafu { path: &page_dir })?; } // Serialize the config let config_path = page_dir.join("page.dhall"); + debug!(?config_path, ?self.config, "Writing out page config"); let serialized_config = serde_dhall::serialize(&self.config) .to_string() .context(ConfigReifySnafu)?; @@ -96,16 +101,21 @@ impl Page { .context(WriteConfigSnafu { path: &config_path })?; // Create the page file if it doesn't exist let page_path = page_dir.join(&self.file); + // Control flow is clearer this way since the `else` is just a log statement + #[allow(clippy::if_not_else)] if !page_path .try_exists() .context(ExistanceCheckSnafu { path: &page_dir })? { + debug!(?page_path, "Creating page file"); let contents = format!("# {}\n\n", self.config.title); let mut page_file = File::create(&page_path).context(WritePageStubSnafu { path: &page_path })?; page_file .write_all(contents.as_bytes()) .context(WritePageStubSnafu { path: &page_path })?; + } else { + debug!(?page_path, "Page file already exists"); } Ok(()) } diff --git a/src/site/post.rs b/src/site/post.rs index 2da0ee7..d233ac2 100644 --- a/src/site/post.rs +++ b/src/site/post.rs @@ -9,7 +9,7 @@ use std::{ use chrono::{Local, NaiveDate}; use serde::{Deserialize, Serialize}; use snafu::{ensure, ResultExt}; -use tracing::{debug, instrument}; +use tracing::{debug, info, span, Level}; use super::{ ConfigReifySnafu, CreateDirectorySnafu, ExistanceCheckSnafu, NotADirectorySnafu, SiteError, @@ -71,7 +71,6 @@ impl Post { /// /// Will return an error if serialization fails, the user does not have write permissions for /// the directory, or any other IO errors occur while writing out the config. - #[instrument(skip(site_path, post_dir))] pub fn write( &self, site_path: impl AsRef, @@ -79,18 +78,24 @@ impl Post { ) -> Result<(), SiteError> { // build the post_dir let post_dir = site_path.as_ref().join("posts").join(post_dir.as_ref()); - debug!(?post_dir); + // Setup logging + let span = span!(Level::INFO, "Post::write", ?post_dir); + let _enter = span.enter(); + info!("Writing out post"); // Make sure the post path is a dir if it exists, otherwise create it if post_dir .try_exists() .context(ExistanceCheckSnafu { path: &post_dir })? { + debug!("Validating post directory"); ensure!(post_dir.is_dir(), NotADirectorySnafu { path: &post_dir }); } else { + debug!("Creating post directory"); create_dir_all(&post_dir).context(CreateDirectorySnafu { path: &post_dir })?; } // Serialize the config let config_path = post_dir.join("post.dhall"); + debug!(?config_path, ?self.config, "Writing out config"); let serialized_config = serde_dhall::serialize(&self.config) .to_string() .context(ConfigReifySnafu)?; @@ -101,16 +106,21 @@ impl Post { .context(WriteConfigSnafu { path: &config_path })?; // Create the post file if it doesn't exist let post_path = post_dir.join(&self.file); + // Control flow is clearer this way since the `else` is just a log statement + #[allow(clippy::if_not_else)] if !post_path .try_exists() .context(ExistanceCheckSnafu { path: &post_dir })? { + debug!(?post_path, "Creating post file"); let contents = format!("# {}\n\n", self.config.title); let mut post_file = File::create(&post_path).context(WritePostStubSnafu { path: &post_path })?; post_file .write_all(contents.as_bytes()) .context(WritePostStubSnafu { path: &post_path })?; + } else { + debug!(?post_path, "Post file already exists"); } Ok(()) }