feat: Improve logging
This commit is contained in:
parent
ca9d921327
commit
534c04bd11
|
@ -594,6 +594,15 @@ dependencies = [
|
||||||
"cfg-if",
|
"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]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
|
@ -840,6 +849,30 @@ dependencies = [
|
||||||
"proc-macro2",
|
"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]]
|
[[package]]
|
||||||
name = "rustc-demangle"
|
name = "rustc-demangle"
|
||||||
version = "0.1.21"
|
version = "0.1.21"
|
||||||
|
@ -1110,10 +1143,14 @@ version = "0.3.16"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
|
checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"matchers",
|
||||||
"nu-ansi-term",
|
"nu-ansi-term",
|
||||||
|
"once_cell",
|
||||||
|
"regex",
|
||||||
"sharded-slab",
|
"sharded-slab",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"thread_local",
|
"thread_local",
|
||||||
|
"tracing",
|
||||||
"tracing-core",
|
"tracing-core",
|
||||||
"tracing-log",
|
"tracing-log",
|
||||||
]
|
]
|
||||||
|
|
|
@ -16,4 +16,4 @@ serde = { version = "1.0.156", features = ["derive"] }
|
||||||
serde_dhall = { version = "0.12.1", default-features = false }
|
serde_dhall = { version = "0.12.1", default-features = false }
|
||||||
snafu = "0.7.4"
|
snafu = "0.7.4"
|
||||||
tracing = "0.1.37"
|
tracing = "0.1.37"
|
||||||
tracing-subscriber = "0.3.16"
|
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
mod cli;
|
mod cli;
|
||||||
|
|
||||||
use cli::Cli;
|
use cli::Cli;
|
||||||
|
use tracing_subscriber::prelude::*;
|
||||||
|
use tracing_subscriber::{fmt, EnvFilter};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use color_eyre::eyre::Result;
|
use color_eyre::eyre::Result;
|
||||||
|
@ -8,7 +10,14 @@ use color_eyre::eyre::Result;
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Setup color eyre and logging
|
// Setup color eyre and logging
|
||||||
color_eyre::install()?;
|
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();
|
let args = Cli::parse();
|
||||||
args.command.run(args.global_args)
|
args.command.run(args.global_args)
|
||||||
|
|
14
src/site.rs
14
src/site.rs
|
@ -8,7 +8,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use snafu::{ensure, ResultExt, Snafu};
|
use snafu::{ensure, ResultExt, Snafu};
|
||||||
use tracing::{debug, instrument};
|
use tracing::{debug, info, info_span};
|
||||||
|
|
||||||
use self::{config::Config, page::Page, post::Post};
|
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
|
/// 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.
|
/// 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<Path>) -> Result<(), SiteError> {
|
pub fn write(&self, site_dir: impl AsRef<Path>) -> Result<(), SiteError> {
|
||||||
let site_dir = site_dir.as_ref();
|
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
|
// Make sure the site_dir is a directory, if it exists, otherwise create it
|
||||||
if site_dir
|
if site_dir
|
||||||
.try_exists()
|
.try_exists()
|
||||||
.context(ExistanceCheckSnafu { path: site_dir })?
|
.context(ExistanceCheckSnafu { path: site_dir })?
|
||||||
{
|
{
|
||||||
|
debug!("Validating site directory");
|
||||||
ensure!(site_dir.is_dir(), NotADirectorySnafu { path: site_dir });
|
ensure!(site_dir.is_dir(), NotADirectorySnafu { path: site_dir });
|
||||||
} else {
|
} else {
|
||||||
|
debug!("Creating site directory");
|
||||||
create_dir_all(site_dir).context(CreateDirectorySnafu { path: site_dir })?;
|
create_dir_all(site_dir).context(CreateDirectorySnafu { path: site_dir })?;
|
||||||
}
|
}
|
||||||
// Serialize the config
|
// Serialize the config
|
||||||
|
debug!(?self.config, "Writing out the config");
|
||||||
let config_path = site_dir.join("config.dhall");
|
let config_path = site_dir.join("config.dhall");
|
||||||
let serialized_config = serde_dhall::serialize(&self.config)
|
let serialized_config = serde_dhall::serialize(&self.config)
|
||||||
.to_string()
|
.to_string()
|
||||||
|
@ -165,11 +170,13 @@ impl Site {
|
||||||
.try_exists()
|
.try_exists()
|
||||||
.context(ExistanceCheckSnafu { path: &statics_dir })?
|
.context(ExistanceCheckSnafu { path: &statics_dir })?
|
||||||
{
|
{
|
||||||
|
debug!(?statics_dir, "Validating statics directory");
|
||||||
ensure!(
|
ensure!(
|
||||||
statics_dir.is_dir(),
|
statics_dir.is_dir(),
|
||||||
NotADirectorySnafu { path: &statics_dir }
|
NotADirectorySnafu { path: &statics_dir }
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
debug!(?statics_dir, "Creating statics directory");
|
||||||
// Create the directory
|
// Create the directory
|
||||||
create_dir_all(&statics_dir).context(CreateDirectorySnafu { path: &statics_dir })?;
|
create_dir_all(&statics_dir).context(CreateDirectorySnafu { path: &statics_dir })?;
|
||||||
// Touch a .gitkeep
|
// Touch a .gitkeep
|
||||||
|
@ -178,6 +185,7 @@ impl Site {
|
||||||
}
|
}
|
||||||
// Create the gitignore
|
// Create the gitignore
|
||||||
let git_ignore_path = site_dir.join(".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 {
|
let mut git_ignore = File::create(&git_ignore_path).context(WriteConfigSnafu {
|
||||||
path: &git_ignore_path,
|
path: &git_ignore_path,
|
||||||
})?;
|
})?;
|
||||||
|
|
|
@ -8,7 +8,7 @@ use std::{
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use snafu::{ensure, ResultExt};
|
use snafu::{ensure, ResultExt};
|
||||||
use tracing::{debug, instrument};
|
use tracing::{debug, info, info_span};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
ConfigReifySnafu, CreateDirectorySnafu, ExistanceCheckSnafu, NotADirectorySnafu, SiteError,
|
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
|
/// 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.
|
/// the directory, or any other IO errors occur while writing out the config.
|
||||||
#[instrument(skip(site_path, page_dir))]
|
|
||||||
pub fn write(
|
pub fn write(
|
||||||
&self,
|
&self,
|
||||||
site_path: impl AsRef<Path>,
|
site_path: impl AsRef<Path>,
|
||||||
|
@ -74,18 +73,24 @@ impl Page {
|
||||||
) -> Result<(), SiteError> {
|
) -> Result<(), SiteError> {
|
||||||
// build the page_dir
|
// build the page_dir
|
||||||
let page_dir = site_path.as_ref().join("pages").join(page_dir.as_ref());
|
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
|
// Make sure the page path is a dir if it exists, otherwise create it
|
||||||
if page_dir
|
if page_dir
|
||||||
.try_exists()
|
.try_exists()
|
||||||
.context(ExistanceCheckSnafu { path: &page_dir })?
|
.context(ExistanceCheckSnafu { path: &page_dir })?
|
||||||
{
|
{
|
||||||
|
debug!("Validating page directory");
|
||||||
ensure!(page_dir.is_dir(), NotADirectorySnafu { path: &page_dir });
|
ensure!(page_dir.is_dir(), NotADirectorySnafu { path: &page_dir });
|
||||||
} else {
|
} else {
|
||||||
|
debug!("Creating page directory");
|
||||||
create_dir_all(&page_dir).context(CreateDirectorySnafu { path: &page_dir })?;
|
create_dir_all(&page_dir).context(CreateDirectorySnafu { path: &page_dir })?;
|
||||||
}
|
}
|
||||||
// Serialize the config
|
// Serialize the config
|
||||||
let config_path = page_dir.join("page.dhall");
|
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)
|
let serialized_config = serde_dhall::serialize(&self.config)
|
||||||
.to_string()
|
.to_string()
|
||||||
.context(ConfigReifySnafu)?;
|
.context(ConfigReifySnafu)?;
|
||||||
|
@ -96,16 +101,21 @@ impl Page {
|
||||||
.context(WriteConfigSnafu { path: &config_path })?;
|
.context(WriteConfigSnafu { path: &config_path })?;
|
||||||
// Create the page file if it doesn't exist
|
// Create the page file if it doesn't exist
|
||||||
let page_path = page_dir.join(&self.file);
|
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
|
if !page_path
|
||||||
.try_exists()
|
.try_exists()
|
||||||
.context(ExistanceCheckSnafu { path: &page_dir })?
|
.context(ExistanceCheckSnafu { path: &page_dir })?
|
||||||
{
|
{
|
||||||
|
debug!(?page_path, "Creating page file");
|
||||||
let contents = format!("# {}\n\n", self.config.title);
|
let contents = format!("# {}\n\n", self.config.title);
|
||||||
let mut page_file =
|
let mut page_file =
|
||||||
File::create(&page_path).context(WritePageStubSnafu { path: &page_path })?;
|
File::create(&page_path).context(WritePageStubSnafu { path: &page_path })?;
|
||||||
page_file
|
page_file
|
||||||
.write_all(contents.as_bytes())
|
.write_all(contents.as_bytes())
|
||||||
.context(WritePageStubSnafu { path: &page_path })?;
|
.context(WritePageStubSnafu { path: &page_path })?;
|
||||||
|
} else {
|
||||||
|
debug!(?page_path, "Page file already exists");
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ use std::{
|
||||||
use chrono::{Local, NaiveDate};
|
use chrono::{Local, NaiveDate};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use snafu::{ensure, ResultExt};
|
use snafu::{ensure, ResultExt};
|
||||||
use tracing::{debug, instrument};
|
use tracing::{debug, info, span, Level};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
ConfigReifySnafu, CreateDirectorySnafu, ExistanceCheckSnafu, NotADirectorySnafu, SiteError,
|
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
|
/// 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.
|
/// the directory, or any other IO errors occur while writing out the config.
|
||||||
#[instrument(skip(site_path, post_dir))]
|
|
||||||
pub fn write(
|
pub fn write(
|
||||||
&self,
|
&self,
|
||||||
site_path: impl AsRef<Path>,
|
site_path: impl AsRef<Path>,
|
||||||
|
@ -79,18 +78,24 @@ impl Post {
|
||||||
) -> Result<(), SiteError> {
|
) -> Result<(), SiteError> {
|
||||||
// build the post_dir
|
// build the post_dir
|
||||||
let post_dir = site_path.as_ref().join("posts").join(post_dir.as_ref());
|
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
|
// Make sure the post path is a dir if it exists, otherwise create it
|
||||||
if post_dir
|
if post_dir
|
||||||
.try_exists()
|
.try_exists()
|
||||||
.context(ExistanceCheckSnafu { path: &post_dir })?
|
.context(ExistanceCheckSnafu { path: &post_dir })?
|
||||||
{
|
{
|
||||||
|
debug!("Validating post directory");
|
||||||
ensure!(post_dir.is_dir(), NotADirectorySnafu { path: &post_dir });
|
ensure!(post_dir.is_dir(), NotADirectorySnafu { path: &post_dir });
|
||||||
} else {
|
} else {
|
||||||
|
debug!("Creating post directory");
|
||||||
create_dir_all(&post_dir).context(CreateDirectorySnafu { path: &post_dir })?;
|
create_dir_all(&post_dir).context(CreateDirectorySnafu { path: &post_dir })?;
|
||||||
}
|
}
|
||||||
// Serialize the config
|
// Serialize the config
|
||||||
let config_path = post_dir.join("post.dhall");
|
let config_path = post_dir.join("post.dhall");
|
||||||
|
debug!(?config_path, ?self.config, "Writing out config");
|
||||||
let serialized_config = serde_dhall::serialize(&self.config)
|
let serialized_config = serde_dhall::serialize(&self.config)
|
||||||
.to_string()
|
.to_string()
|
||||||
.context(ConfigReifySnafu)?;
|
.context(ConfigReifySnafu)?;
|
||||||
|
@ -101,16 +106,21 @@ impl Post {
|
||||||
.context(WriteConfigSnafu { path: &config_path })?;
|
.context(WriteConfigSnafu { path: &config_path })?;
|
||||||
// Create the post file if it doesn't exist
|
// Create the post file if it doesn't exist
|
||||||
let post_path = post_dir.join(&self.file);
|
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
|
if !post_path
|
||||||
.try_exists()
|
.try_exists()
|
||||||
.context(ExistanceCheckSnafu { path: &post_dir })?
|
.context(ExistanceCheckSnafu { path: &post_dir })?
|
||||||
{
|
{
|
||||||
|
debug!(?post_path, "Creating post file");
|
||||||
let contents = format!("# {}\n\n", self.config.title);
|
let contents = format!("# {}\n\n", self.config.title);
|
||||||
let mut post_file =
|
let mut post_file =
|
||||||
File::create(&post_path).context(WritePostStubSnafu { path: &post_path })?;
|
File::create(&post_path).context(WritePostStubSnafu { path: &post_path })?;
|
||||||
post_file
|
post_file
|
||||||
.write_all(contents.as_bytes())
|
.write_all(contents.as_bytes())
|
||||||
.context(WritePostStubSnafu { path: &post_path })?;
|
.context(WritePostStubSnafu { path: &post_path })?;
|
||||||
|
} else {
|
||||||
|
debug!(?post_path, "Post file already exists");
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue