2021-12-14 12:40:53 +00:00
|
|
|
use std::fmt::Display;
|
2021-12-20 08:41:03 +00:00
|
|
|
use structopt::clap::arg_enum;
|
2021-12-14 12:40:53 +00:00
|
|
|
|
2022-01-07 07:05:37 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
pub enum LogState {
|
2022-01-13 01:07:01 +00:00
|
|
|
Error,
|
|
|
|
Warning,
|
|
|
|
Debug,
|
|
|
|
Verbose,
|
2022-01-07 07:05:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 08:41:03 +00:00
|
|
|
arg_enum! {
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
2022-01-07 07:05:37 +00:00
|
|
|
pub enum LogLevel {
|
2022-01-13 01:07:01 +00:00
|
|
|
Error,
|
|
|
|
Warning,
|
|
|
|
Debug,
|
|
|
|
Verbose,
|
2021-12-20 08:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 07:05:37 +00:00
|
|
|
pub fn log<T>(msg: T, state: LogState, level: LogLevel)
|
2021-12-20 08:41:03 +00:00
|
|
|
where
|
|
|
|
T: Display,
|
|
|
|
{
|
2022-01-13 01:07:01 +00:00
|
|
|
if state == LogState::Error {
|
2021-12-20 08:41:03 +00:00
|
|
|
log_error(msg);
|
2022-01-13 01:07:01 +00:00
|
|
|
} else if state == LogState::Warning {
|
|
|
|
if level != LogLevel::Error {
|
2021-12-20 08:41:03 +00:00
|
|
|
log_warning(msg);
|
|
|
|
}
|
2022-01-13 01:07:01 +00:00
|
|
|
} else if state == LogState::Debug {
|
|
|
|
if level == LogLevel::Debug || level == LogLevel::Verbose {
|
2021-12-20 08:41:03 +00:00
|
|
|
log_debug(msg);
|
|
|
|
}
|
2022-01-13 01:07:01 +00:00
|
|
|
} else if state == LogState::Verbose {
|
|
|
|
if level == LogLevel::Verbose {
|
2021-12-20 08:41:03 +00:00
|
|
|
log_verbose(msg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_error<T>(msg: T)
|
|
|
|
where
|
|
|
|
T: Display,
|
|
|
|
{
|
2022-01-13 01:07:01 +00:00
|
|
|
println!("Error: {}", msg);
|
2021-12-20 08:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_warning<T>(msg: T)
|
|
|
|
where
|
|
|
|
T: Display,
|
|
|
|
{
|
2022-01-13 01:07:01 +00:00
|
|
|
println!("Warning: {}", msg);
|
2021-12-20 08:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_debug<T>(msg: T)
|
2021-12-14 12:40:53 +00:00
|
|
|
where
|
|
|
|
T: Display,
|
|
|
|
{
|
2022-01-13 01:07:01 +00:00
|
|
|
println!("Debug: {}", msg);
|
2021-12-14 12:40:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 08:41:03 +00:00
|
|
|
pub fn log_verbose<T>(msg: T)
|
2021-12-14 12:40:53 +00:00
|
|
|
where
|
|
|
|
T: Display,
|
|
|
|
{
|
2022-01-13 01:07:01 +00:00
|
|
|
println!("Verbose: {}", msg);
|
2021-12-14 12:40:53 +00:00
|
|
|
}
|