From 727b84e56b1231bb3fcc369d3e45bc105a8acb10 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 6 Sep 2024 11:45:58 +0200 Subject: [PATCH] feat: add logger --- Cargo.toml | 3 +-- src/format.rs | 6 ++++++ src/main.rs | 10 ++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 02f15e8..95f31b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ categories = ["command-line-utilities", "encoding"] anyhow = "1.0.83" clap = { version = "4.5.4", features = ["derive"] } fast32 = "1.0.2" -libpt = { version = "0.5.1", features = ["bintols"], default-features = false } +libpt = { version = "0.6.0", features = ["bintols", "log", "cli"] } num = "0.4.3" rand = "0.8.5" - diff --git a/src/format.rs b/src/format.rs index 34a457c..39d5a50 100644 --- a/src/format.rs +++ b/src/format.rs @@ -25,6 +25,8 @@ use anyhow::anyhow; use clap::{ArgGroup, Parser}; use libpt::bintols::{join, split}; +use libpt::cli::args::VerbosityLevel; +use libpt::log::debug; /// The number type [numf](crate) uses pub type NumberType = u128; @@ -127,6 +129,9 @@ pub struct FormatOptions { /// /// The numbers may be left empty at first, if numbers are provided from the stdin. numbers: Vec, + + #[command(flatten)] + pub(crate) verbosity: VerbosityLevel, } impl FormatOptions { @@ -242,6 +247,7 @@ impl Default for FormatOptions { numbers: vec![], rand: 0, rand_max: NumberType::MAX, + verbosity: VerbosityLevel::default(), } } } diff --git a/src/main.rs b/src/main.rs index 4eeca51..816d8d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,21 @@ use clap::{CommandFactory, Parser}; mod format; use format::*; +use libpt::log::debug; use numf::format::numf_parser; fn main() -> anyhow::Result<()> { // try to read from stdin first, appending the numbers we read to the FormatOptions let mut options = FormatOptions::parse(); + let _logger = libpt::log::Logger::builder() + .set_level(options.verbosity.level()) + .display_time(false) + .build() + .map_err(|e| { + eprintln!("could not initialize logger: {e}"); + }); + debug!("logger active"); + let mut stdin_nums = Vec::new(); let stdin = std::io::stdin(); // only accept numbers from stdin if the stdin is not an interactive terminal