adjust bin interface with log

This commit is contained in:
Christoph J. Scherr 2024-01-16 14:03:55 +01:00
parent a9cf78ee0d
commit 0515e221f9
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
4 changed files with 46 additions and 96 deletions

View File

@ -31,4 +31,4 @@ path = "src/main/mod.rs"
clap = { version = "4.4.4", features = ["derive"] } clap = { version = "4.4.4", features = ["derive"] }
clap-num = "1.0.2" clap-num = "1.0.2"
clap-verbosity-flag = "2.0.1" clap-verbosity-flag = "2.0.1"
libpt = { version = "0.1.7", path = "../..", features = ["ccc", "math", "hedu", "net"] } libpt = {path = "../..", features = ["default", "ccc", "math", "hedu", "net", "log"] }

View File

@ -93,34 +93,10 @@ fn main() {
} }
}; };
if cli.log_meta { if cli.log_meta {
Logger::init_customized( Logger::init(None, Some(ll)).expect("could not initialize Logger");
false,
PathBuf::from("/dev/null"),
true,
false,
true,
true,
ll,
false,
false,
false,
)
.expect("could not initialize Logger");
} else { } else {
// less verbose version // less verbose version
Logger::init_customized( Logger::init_mini(Some(ll)).expect("could not initialize Logger");
false,
PathBuf::from("/dev/null"),
true,
false,
true,
false,
ll,
false,
false,
false,
)
.expect("could not initialize Logger");
} }
let mut expr: String = String::new(); let mut expr: String = String::new();
for part in cli.expression { for part in cli.expression {

View File

@ -18,14 +18,18 @@ use libpt::{hedu::*, log::*};
use clap::Parser; use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity}; use clap_verbosity_flag::{InfoLevel, Verbosity};
use std::{fs::File, io::BufReader, path::PathBuf}; use std::{
fs::File,
io::{BufRead, BufReader, IsTerminal, Read},
path::PathBuf,
};
//// TYPES ///////////////////////////////////////////////////////////////////////////////////////// //// TYPES /////////////////////////////////////////////////////////////////////////////////////////
//// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// //// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
/// short about section displayed in help /// short about section displayed in help
const ABOUT_ROOT: &'static str = r##" const ABOUT_ROOT: &'static str = r##"
Dumps data in fancy formats Dumps data in fancy formats.
"##; "##;
/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT) /// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT)
static LONG_ABOUT_ROOT: &'static str = r##" static LONG_ABOUT_ROOT: &'static str = r##"
@ -49,9 +53,13 @@ static LONG_ABOUT_ROOT: &'static str = r##"
about = ABOUT_ROOT, about = ABOUT_ROOT,
long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT), long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT),
help_template = help_template =
r#"libpt: {version}{about-section}Author: r#"{about-section}
{author-with-newline} {usage-heading} {usage}
{usage-heading} {usage}{all-args}{tab}"# {all-args}{tab}
libpt: {version}
Author: {author-with-newline}
"#
)] )]
pub struct Cli { pub struct Cli {
// clap_verbosity_flag seems to make this a global option implicitly // clap_verbosity_flag seems to make this a global option implicitly
@ -59,7 +67,7 @@ pub struct Cli {
#[command(flatten)] #[command(flatten)]
pub verbose: Verbosity<InfoLevel>, pub verbose: Verbosity<InfoLevel>,
/// show logger meta /// show additional logging meta data
#[arg(short, long, global = true)] #[arg(short, long, global = true)]
pub log_meta: bool, pub log_meta: bool,
@ -67,8 +75,10 @@ pub struct Cli {
#[arg(short, long, global = true)] #[arg(short, long, global = true)]
pub chars: bool, pub chars: bool,
/// a data source, probably a file /// a data source, probably a file.
pub data_source: String, ///
/// If left empty or set as "-", the program will read from stdin.
pub data_source: Option<String>,
} }
//// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// //// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
@ -78,19 +88,31 @@ pub struct Cli {
//// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// //// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
fn main() { fn main() {
let cli = cli_parse(); let cli = cli_parse();
debug!("Trying to open '{}'", cli.data_source); let source: Box<dyn BufRead>;
let file = match File::open(cli.data_source.clone()) { if cli.data_source.is_some() && cli.data_source.clone().is_some_and(|val| val != "-") {
Ok(file) => file, let data_source = cli.data_source.unwrap();
Err(err) => { debug!("Trying to open '{}'", data_source);
error!("Could not open file '{}': {err}", cli.data_source); source = match File::open(&data_source) {
std::process::exit(1); Ok(file) => Box::new(BufReader::new(file)),
Err(err) => {
error!("Could not open file '{}': {err}", data_source);
std::process::exit(1);
}
};
} else {
debug!("Trying to open stdout");
let stdin = std::io::stdin();
if stdin.is_terminal() {
warn!("Refusing to dump from interactive terminal");
std::process::exit(2)
} }
}; source = Box::new(BufReader::new(stdin));
match dump(BufReader::new(file), cli.chars) { }
match dump(BufReader::new(source), cli.chars) {
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {
error!("Could not dump data of file: {err}"); error!("Could not dump data of file: {err}");
std::process::exit(2); std::process::exit(3);
} }
} }
} }
@ -109,34 +131,10 @@ fn cli_parse() -> Cli {
} }
}; };
if cli.log_meta { if cli.log_meta {
Logger::init_customized( Logger::init(None, Some(ll)).expect("could not initialize Logger");
false,
PathBuf::from("/dev/null"),
true,
false,
true,
true,
ll,
false,
false,
false,
)
.expect("could not initialize Logger");
} else { } else {
// less verbose version // less verbose version
Logger::init_customized( Logger::init_mini(Some(ll)).expect("could not initialize Logger");
false,
PathBuf::from("/dev/null"),
true,
false,
true,
false,
ll,
false,
false,
false,
)
.expect("could not initialize Logger");
} }
return cli; return cli;
} }

View File

@ -51,34 +51,10 @@ pub fn main() {
} }
}; };
if cli.log_meta { if cli.log_meta {
Logger::init_customized( Logger::init(None, Some(ll)).expect("could not initialize Logger");
false,
PathBuf::from("/dev/null"),
true,
false,
true,
true,
ll,
false,
false,
false,
)
.expect("could not initialize Logger");
} else { } else {
// less verbose version // less verbose version
Logger::init_customized( Logger::init_mini(Some(ll)).expect("could not initialize Logger");
false,
PathBuf::from("/dev/null"),
true,
false,
true,
false,
ll,
false,
false,
false,
)
.expect("could not initialize Logger");
} }
trace!("started the main function"); trace!("started the main function");