From dc5252cbcb759afcd98037b4018bc5e2f7030578 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Sun, 14 Jan 2024 22:40:44 +0100 Subject: [PATCH] feat: add logging --- Cargo.lock | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/core/mod.rs | 2 +- src/main.rs | 29 +++++++++++++++++++---------- 4 files changed, 173 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1566f90..f1c953e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,15 @@ version = 3 [[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] name = "anstream" version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -119,6 +128,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] +name = "env_logger" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] name = "form_urlencoded" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -149,6 +181,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] +name = "hermit-abi" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] name = "idna" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -159,6 +203,17 @@ dependencies = [ ] [[package]] +name = "is-terminal" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys", +] + +[[package]] name = "itertools" version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -229,12 +284,24 @@ dependencies = [ ] [[package]] +name = "linux-raw-sys" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" + +[[package]] name = "log" version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] +name = "memchr" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" + +[[package]] name = "openssl-probe" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -283,6 +350,48 @@ dependencies = [ ] [[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "rustix" +version = "0.38.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] name = "ryu" version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -324,8 +433,10 @@ name = "smolguess" version = "0.1.0" dependencies = [ "clap", + "env_logger", "git2", "itertools", + "log", "serde", "serde_json", "stopwatch", @@ -354,6 +465,15 @@ dependencies = [ ] [[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] name = "tinyvec" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -413,6 +533,37 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" [[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index 9b490fc..97804ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,10 @@ edition = "2021" [dependencies] clap = { version = "4.4.16", features = ["derive"] } +env_logger = "0.10.1" git2 = "0.18.1" itertools = "0.12.0" +log = "0.4.20" serde = { version = "1.0.195", features = ["derive"] } serde_json = "1.0.111" stopwatch = { git = "https://git.holly.sh/stopwatch.git", version = "0.1.0" } diff --git a/src/core/mod.rs b/src/core/mod.rs index 00558cb..6233969 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -391,7 +391,7 @@ where /// assert_eq!(language_match.language.name, "Rust"); /// assert_eq!(language_match.language.extension, "rs"); /// ``` -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Clone)] pub struct LanguageMatch { pub file: PathBuf, pub language: LanguageDefinition, diff --git a/src/main.rs b/src/main.rs index 992fe4a..1a4f58b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::{ use clap::Parser; use itertools::Itertools; +use log::{debug, error, info}; use smolguess::{ core::{LanguageDefinitions, LanguageMatch}, repository, @@ -29,6 +30,8 @@ struct CliArgs { } fn main() -> io::Result<()> { + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init(); + let args: CliArgs = CliArgs::parse(); let mut stopwatch = stopwatch::Stopwatch::new(); @@ -41,6 +44,7 @@ fn main() -> io::Result<()> { ))); let grouped_matches: HashMap<_, _> = repo_languages + .clone() .into_iter() .group_by(|matched| matched.language.name.clone()) .into_iter() @@ -50,28 +54,29 @@ fn main() -> io::Result<()> { let mut sorted_groups: Vec<_> = grouped_matches.into_iter().collect(); sorted_groups.sort_by(|(_, first), (_, second)| second.len().cmp(&first.len())); - if args.verbose { - println!("{:#?}", sorted_groups); - } - stopwatch.stop(); - // Print out the results let json: String = serde_json::to_string_pretty(&sorted_groups)?; + // Print out the results if args.verbose { + debug!("======== JSON DATA ========"); println!("{}", json); - println!("----------------") + debug!("======== END DATA ========"); + debug!("Files found: {}", repo_languages.len()); } // Write match data to file if args.export.is_some() { - let file = PathBuf::from(args.export.unwrap()); + let file = PathBuf::from(args.export.as_ref().unwrap()); if !file.is_dir() { File::create(file)?.write_all(json.as_bytes())?; } else { - println!("This is a directory. Aborting file write."); + error!( + "The provided path is a directory: '{}'", + args.export.unwrap() + ); } } @@ -79,11 +84,15 @@ fn main() -> io::Result<()> { if !sorted_groups.is_empty() { let default: (String, Vec) = (String::from("Unknown"), vec![]); - println!("{}", sorted_groups.first().unwrap_or_else(|| &default).0); + println!( + "{}", + sorted_groups.first().unwrap_or_else(|| { &default }).0 + ); } + // Print out the execution time if args.verbose { - println!("Operation completed in: {}", stopwatch) + info!("Operation completed in: {}", stopwatch) } Ok(()) -- libgit2 1.7.2