From a8e6572a5378ea61ac3cb50975390262d74049ab Mon Sep 17 00:00:00 2001 From: Andrei Stoica Date: Tue, 2 Apr 2024 10:58:49 -0400 Subject: [PATCH] logging --- Cargo.toml | 2 ++ src/cli/mod.rs | 5 ++++- src/file/mod.rs | 3 +-- src/logging/mod.rs | 26 ++++++++++++++++++++++++++ src/main.rs | 33 +++++++++++++++++++++++++++------ 5 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/logging/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 20e03b2..e513423 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,5 @@ regex = "1.8.4" serde = { version = "1.0.164", features = ["serde_derive"] } serde_json = "1.0.97" resolve-path = "0.1.0" +simple_logger = "4.3.3" +log = "0.4.21" diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 4108eaf..d3cba42 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -6,7 +6,6 @@ pub struct Args { /// set config file to use #[arg(short, long, value_name = "FILE")] pub config: Option, - /// show current config file #[arg(short = 'C', long)] pub current_config: bool, @@ -23,4 +22,8 @@ pub struct Args { /// list closest files to date #[arg(short = 'L', long)] pub list_all: bool, + + /// increase logging level + #[arg(short, long, action = clap::ArgAction::Count)] + pub verbose: u8, } diff --git a/src/file/mod.rs b/src/file/mod.rs index 0636f97..c7d886e 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -5,6 +5,7 @@ use chrono::Datelike; use comrak::nodes::{AstNode, NodeValue}; use comrak::parse_document; use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions}; +use log; use std::collections::HashMap; use std::fs::{read, File}; use std::io::Write; @@ -147,14 +148,12 @@ mod test { let sections = vec!["Sub section".to_string()]; let result = extract_secitons(root, §ions); - println!("{:#?}", root); assert_eq!(result.keys().count(), 1); assert!(result.get(sections.first().unwrap()).is_some()); assert_eq!(result.get(sections.first().unwrap()).unwrap().level, 3); let sections = vec!["Content".to_string()]; let result = extract_secitons(root, §ions); - println!("{:#?}", root); assert_eq!(result.keys().count(), 1); assert!(result.get(sections.first().unwrap()).is_some()); assert_eq!( diff --git a/src/logging/mod.rs b/src/logging/mod.rs new file mode 100644 index 0000000..bd36722 --- /dev/null +++ b/src/logging/mod.rs @@ -0,0 +1,26 @@ +use log::Level; + +pub fn get_logging_level(verbose_level: u8) -> Level { + match verbose_level { + ..=0 => Level::Error, + 1 => Level::Warn, + 2 => Level::Info, + 3 => Level::Debug, + 4.. => Level::Trace, + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_get_logging_level() { + assert_eq!(get_logging_level(0), Level::Error); + assert_eq!(get_logging_level(1), Level::Warn); + assert_eq!(get_logging_level(2), Level::Info); + assert_eq!(get_logging_level(3), Level::Debug); + assert_eq!(get_logging_level(4), Level::Trace); + assert_eq!(get_logging_level(5), Level::Trace); + } +} diff --git a/src/main.rs b/src/main.rs index c00a010..ab89a7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,28 @@ mod cli; mod config; mod file; +mod logging; mod todo; -use crate::cli::Args; -use crate::config::Config; -use crate::todo::{File as TodoFile, TaskGroup}; use chrono::naive::NaiveDate; use chrono::{Local, TimeDelta}; use clap::Parser; +use cli::Args; use comrak::Arena; +use config::Config; +use log; +use logging::get_logging_level; use resolve_path::PathResolveExt; +use simple_logger::init_with_level; use std::fs; use std::path::Path; use std::process::Command; +use todo::{File as TodoFile, TaskGroup}; fn main() { let args = Args::parse(); - println!("{:?}", args); + let _logger = init_with_level(get_logging_level(args.verbose)).unwrap(); + log::debug!("{:?}", args); let expected_cfg_files = match Config::expected_locations() { Ok(cfg_files) => cfg_files, @@ -48,7 +53,7 @@ fn main() { }; if args.current_config { - println!("{}", &cfg_file); + log::debug!("{}", &cfg_file); return; } @@ -56,13 +61,14 @@ fn main() { Ok(cfg) => cfg, Err(_e) => panic!("could not load config: {}", cfg_file), }; + log::debug!("{:#?}", cfg); let data_dir = cfg.notes_dir.resolve().to_path_buf(); if !fs::metadata(&data_dir).is_ok() { match fs::create_dir_all(&data_dir) { Err(_e) => panic!("Could not create default directory: {:?}", &data_dir), - _ => (), + _ => log::info!("created dir {}", &data_dir.to_string_lossy()), }; } @@ -95,14 +101,21 @@ fn main() { let current_file = match latest_file { Some(todo_file) if todo_file.date < today && args.previous == 0 => { let sections = &cfg.sections; + log::info!("looking for sections: {:?}", sections); let arena = Arena::new(); let root = { + log::info!( + "loading and parsing file: {}", + todo_file.file.to_string_lossy() + ); let contents = file::load_file(&todo_file); let root = file::parse_todo_file(&contents, &arena); root }; + log::trace!("file loaded"); let groups = file::extract_secitons(root, sections); + log::trace!("sections extracted"); let level = groups.values().map(|group| group.level).min().unwrap_or(2); let data = sections .iter() @@ -114,12 +127,14 @@ fn main() { let content = file::generate_file_content(&data, &today); let file_path = file::get_filepath(&data_dir, &today); + log::info!("writing to file: {}", file_path.to_string_lossy()); file::write_file(&file_path, &content); file_path } Some(todo_file) => todo_file.file.clone(), None => { let sections = &cfg.sections; + log::info!("creating new empty file with sections: {:?}", sections); let data = sections .iter() .map(|sec| TaskGroup::empty(sec.clone(), 2)) @@ -127,10 +142,16 @@ fn main() { let content = file::generate_file_content(&data, &today); let file_path = file::get_filepath(&data_dir, &today); file::write_file(&file_path, &content); + log::info!("writing to file: {}", file_path.to_string_lossy()); file_path } }; + log::info!( + "Opening {} in {}", + current_file.to_string_lossy(), + cfg.editor + ); Command::new(&cfg.editor) .args([current_file]) .status()