scaffold the CLI, logging and turn research into TODOs
This commit is contained in:
commit
6aa7e23872
17 changed files with 888 additions and 0 deletions
54
src/logging.rs
Normal file
54
src/logging.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use tracing::Level;
|
||||
use tracing_subscriber::{Layer as _, layer::SubscriberExt as _, util::SubscriberInitExt as _};
|
||||
|
||||
pub const DEFAULT_LOG_LEVEL: Level = if cfg!(debug_assertions) {
|
||||
Level::DEBUG // debug builds
|
||||
} else {
|
||||
Level::WARN // release builds
|
||||
};
|
||||
|
||||
fn level_to_index(level: Level) -> u8 {
|
||||
match level {
|
||||
Level::TRACE => 0,
|
||||
Level::DEBUG => 1,
|
||||
Level::INFO => 2,
|
||||
Level::WARN => 3,
|
||||
Level::ERROR => 4,
|
||||
}
|
||||
}
|
||||
|
||||
fn index_to_level(index: u8) -> Level {
|
||||
match index {
|
||||
0 => Level::TRACE,
|
||||
1 => Level::DEBUG,
|
||||
2 => Level::INFO,
|
||||
3 => Level::WARN,
|
||||
4.. => Level::ERROR,
|
||||
}
|
||||
}
|
||||
|
||||
fn level_from_verbosity(verbosity: u8) -> Level {
|
||||
index_to_level(level_to_index(DEFAULT_LOG_LEVEL).saturating_sub(verbosity))
|
||||
}
|
||||
|
||||
pub fn init_tracing(verbosity: u8) -> anyhow::Result<()> {
|
||||
let terminal_layer = tracing_subscriber::fmt::layer()
|
||||
.without_time()
|
||||
.compact()
|
||||
.boxed();
|
||||
|
||||
let level = level_from_verbosity(verbosity);
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(terminal_layer)
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::builder()
|
||||
.with_default_directive(level.into())
|
||||
.from_env()?,
|
||||
)
|
||||
.init();
|
||||
|
||||
tracing::info!("logging with level {level}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue