logging
This commit is contained in:
parent
fc7e45cd3f
commit
a8e6572a53
|
|
@ -14,3 +14,5 @@ regex = "1.8.4"
|
||||||
serde = { version = "1.0.164", features = ["serde_derive"] }
|
serde = { version = "1.0.164", features = ["serde_derive"] }
|
||||||
serde_json = "1.0.97"
|
serde_json = "1.0.97"
|
||||||
resolve-path = "0.1.0"
|
resolve-path = "0.1.0"
|
||||||
|
simple_logger = "4.3.3"
|
||||||
|
log = "0.4.21"
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ pub struct Args {
|
||||||
/// set config file to use
|
/// set config file to use
|
||||||
#[arg(short, long, value_name = "FILE")]
|
#[arg(short, long, value_name = "FILE")]
|
||||||
pub config: Option<String>,
|
pub config: Option<String>,
|
||||||
|
|
||||||
/// show current config file
|
/// show current config file
|
||||||
#[arg(short = 'C', long)]
|
#[arg(short = 'C', long)]
|
||||||
pub current_config: bool,
|
pub current_config: bool,
|
||||||
|
|
@ -23,4 +22,8 @@ pub struct Args {
|
||||||
/// list closest files to date
|
/// list closest files to date
|
||||||
#[arg(short = 'L', long)]
|
#[arg(short = 'L', long)]
|
||||||
pub list_all: bool,
|
pub list_all: bool,
|
||||||
|
|
||||||
|
/// increase logging level
|
||||||
|
#[arg(short, long, action = clap::ArgAction::Count)]
|
||||||
|
pub verbose: u8,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use chrono::Datelike;
|
||||||
use comrak::nodes::{AstNode, NodeValue};
|
use comrak::nodes::{AstNode, NodeValue};
|
||||||
use comrak::parse_document;
|
use comrak::parse_document;
|
||||||
use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
|
use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
|
||||||
|
use log;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::{read, File};
|
use std::fs::{read, File};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
@ -147,14 +148,12 @@ mod test {
|
||||||
|
|
||||||
let sections = vec!["Sub section".to_string()];
|
let sections = vec!["Sub section".to_string()];
|
||||||
let result = extract_secitons(root, §ions);
|
let result = extract_secitons(root, §ions);
|
||||||
println!("{:#?}", root);
|
|
||||||
assert_eq!(result.keys().count(), 1);
|
assert_eq!(result.keys().count(), 1);
|
||||||
assert!(result.get(sections.first().unwrap()).is_some());
|
assert!(result.get(sections.first().unwrap()).is_some());
|
||||||
assert_eq!(result.get(sections.first().unwrap()).unwrap().level, 3);
|
assert_eq!(result.get(sections.first().unwrap()).unwrap().level, 3);
|
||||||
|
|
||||||
let sections = vec!["Content".to_string()];
|
let sections = vec!["Content".to_string()];
|
||||||
let result = extract_secitons(root, §ions);
|
let result = extract_secitons(root, §ions);
|
||||||
println!("{:#?}", root);
|
|
||||||
assert_eq!(result.keys().count(), 1);
|
assert_eq!(result.keys().count(), 1);
|
||||||
assert!(result.get(sections.first().unwrap()).is_some());
|
assert!(result.get(sections.first().unwrap()).is_some());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
src/main.rs
33
src/main.rs
|
|
@ -1,23 +1,28 @@
|
||||||
mod cli;
|
mod cli;
|
||||||
mod config;
|
mod config;
|
||||||
mod file;
|
mod file;
|
||||||
|
mod logging;
|
||||||
mod todo;
|
mod todo;
|
||||||
|
|
||||||
use crate::cli::Args;
|
|
||||||
use crate::config::Config;
|
|
||||||
use crate::todo::{File as TodoFile, TaskGroup};
|
|
||||||
use chrono::naive::NaiveDate;
|
use chrono::naive::NaiveDate;
|
||||||
use chrono::{Local, TimeDelta};
|
use chrono::{Local, TimeDelta};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use cli::Args;
|
||||||
use comrak::Arena;
|
use comrak::Arena;
|
||||||
|
use config::Config;
|
||||||
|
use log;
|
||||||
|
use logging::get_logging_level;
|
||||||
use resolve_path::PathResolveExt;
|
use resolve_path::PathResolveExt;
|
||||||
|
use simple_logger::init_with_level;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
use todo::{File as TodoFile, TaskGroup};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = Args::parse();
|
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() {
|
let expected_cfg_files = match Config::expected_locations() {
|
||||||
Ok(cfg_files) => cfg_files,
|
Ok(cfg_files) => cfg_files,
|
||||||
|
|
@ -48,7 +53,7 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
if args.current_config {
|
if args.current_config {
|
||||||
println!("{}", &cfg_file);
|
log::debug!("{}", &cfg_file);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,13 +61,14 @@ fn main() {
|
||||||
Ok(cfg) => cfg,
|
Ok(cfg) => cfg,
|
||||||
Err(_e) => panic!("could not load config: {}", cfg_file),
|
Err(_e) => panic!("could not load config: {}", cfg_file),
|
||||||
};
|
};
|
||||||
|
log::debug!("{:#?}", cfg);
|
||||||
|
|
||||||
let data_dir = cfg.notes_dir.resolve().to_path_buf();
|
let data_dir = cfg.notes_dir.resolve().to_path_buf();
|
||||||
|
|
||||||
if !fs::metadata(&data_dir).is_ok() {
|
if !fs::metadata(&data_dir).is_ok() {
|
||||||
match fs::create_dir_all(&data_dir) {
|
match fs::create_dir_all(&data_dir) {
|
||||||
Err(_e) => panic!("Could not create default directory: {:?}", &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 {
|
let current_file = match latest_file {
|
||||||
Some(todo_file) if todo_file.date < today && args.previous == 0 => {
|
Some(todo_file) if todo_file.date < today && args.previous == 0 => {
|
||||||
let sections = &cfg.sections;
|
let sections = &cfg.sections;
|
||||||
|
log::info!("looking for sections: {:?}", sections);
|
||||||
let arena = Arena::new();
|
let arena = Arena::new();
|
||||||
|
|
||||||
let root = {
|
let root = {
|
||||||
|
log::info!(
|
||||||
|
"loading and parsing file: {}",
|
||||||
|
todo_file.file.to_string_lossy()
|
||||||
|
);
|
||||||
let contents = file::load_file(&todo_file);
|
let contents = file::load_file(&todo_file);
|
||||||
let root = file::parse_todo_file(&contents, &arena);
|
let root = file::parse_todo_file(&contents, &arena);
|
||||||
root
|
root
|
||||||
};
|
};
|
||||||
|
log::trace!("file loaded");
|
||||||
let groups = file::extract_secitons(root, sections);
|
let groups = file::extract_secitons(root, sections);
|
||||||
|
log::trace!("sections extracted");
|
||||||
let level = groups.values().map(|group| group.level).min().unwrap_or(2);
|
let level = groups.values().map(|group| group.level).min().unwrap_or(2);
|
||||||
let data = sections
|
let data = sections
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -114,12 +127,14 @@ fn main() {
|
||||||
|
|
||||||
let content = file::generate_file_content(&data, &today);
|
let content = file::generate_file_content(&data, &today);
|
||||||
let file_path = file::get_filepath(&data_dir, &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::write_file(&file_path, &content);
|
||||||
file_path
|
file_path
|
||||||
}
|
}
|
||||||
Some(todo_file) => todo_file.file.clone(),
|
Some(todo_file) => todo_file.file.clone(),
|
||||||
None => {
|
None => {
|
||||||
let sections = &cfg.sections;
|
let sections = &cfg.sections;
|
||||||
|
log::info!("creating new empty file with sections: {:?}", sections);
|
||||||
let data = sections
|
let data = sections
|
||||||
.iter()
|
.iter()
|
||||||
.map(|sec| TaskGroup::empty(sec.clone(), 2))
|
.map(|sec| TaskGroup::empty(sec.clone(), 2))
|
||||||
|
|
@ -127,10 +142,16 @@ fn main() {
|
||||||
let content = file::generate_file_content(&data, &today);
|
let content = file::generate_file_content(&data, &today);
|
||||||
let file_path = file::get_filepath(&data_dir, &today);
|
let file_path = file::get_filepath(&data_dir, &today);
|
||||||
file::write_file(&file_path, &content);
|
file::write_file(&file_path, &content);
|
||||||
|
log::info!("writing to file: {}", file_path.to_string_lossy());
|
||||||
file_path
|
file_path
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
log::info!(
|
||||||
|
"Opening {} in {}",
|
||||||
|
current_file.to_string_lossy(),
|
||||||
|
cfg.editor
|
||||||
|
);
|
||||||
Command::new(&cfg.editor)
|
Command::new(&cfg.editor)
|
||||||
.args([current_file])
|
.args([current_file])
|
||||||
.status()
|
.status()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue