eh-log: add debug level; respect verbosity levels
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Idc62fd26efc10f0c1e49424cf337e6d16a6a6964
This commit is contained in:
parent
7b3452ef18
commit
4707a7e49f
2 changed files with 44 additions and 6 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "eh-log"
|
name = "eh-log"
|
||||||
description = "Styled logging for eh"
|
description = "Tiny, styled logging crate for eh"
|
||||||
version.workspace = true
|
version.workspace = true
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,64 @@
|
||||||
use std::fmt;
|
use std::{
|
||||||
|
fmt,
|
||||||
|
sync::atomic::{AtomicI8, Ordering},
|
||||||
|
};
|
||||||
|
|
||||||
use yansi::Paint;
|
use yansi::Paint;
|
||||||
|
|
||||||
|
static VERBOSITY: AtomicI8 = AtomicI8::new(0);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
|
pub enum Level {
|
||||||
|
Error = -2,
|
||||||
|
Warn = -1,
|
||||||
|
Info = 0,
|
||||||
|
Debug = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_verbosity(verbosity: i8) {
|
||||||
|
VERBOSITY.store(verbosity, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(level: Level) -> bool {
|
||||||
|
VERBOSITY.load(Ordering::Relaxed) >= level as i8
|
||||||
|
}
|
||||||
|
|
||||||
pub fn info(args: fmt::Arguments) {
|
pub fn info(args: fmt::Arguments) {
|
||||||
eprintln!(" {} {args}", "->".green().bold());
|
if enabled(Level::Info) {
|
||||||
|
eprintln!(" {} {args}", "->".green().bold());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn debug(args: fmt::Arguments) {
|
||||||
|
if enabled(Level::Debug) {
|
||||||
|
eprintln!(" {} {args}", "*".blue().dim());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn warn(args: fmt::Arguments) {
|
pub fn warn(args: fmt::Arguments) {
|
||||||
eprintln!(" {} {args}", "->".yellow().bold());
|
if enabled(Level::Warn) {
|
||||||
|
eprintln!(" {} {args}", "->".yellow().bold());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn error(args: fmt::Arguments) {
|
pub fn error(args: fmt::Arguments) {
|
||||||
eprintln!(" {} {args}", "!".red().bold());
|
if enabled(Level::Error) {
|
||||||
|
eprintln!(" {} {args}", "!".red().bold());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hint(args: fmt::Arguments) {
|
pub fn hint(args: fmt::Arguments) {
|
||||||
eprintln!(" {} {args}", "~".yellow().dim());
|
if enabled(Level::Info) {
|
||||||
|
eprintln!(" {} {args}", "~".yellow().dim());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! log_info { ($($t:tt)*) => { $crate::info(format_args!($($t)*)) } }
|
macro_rules! log_info { ($($t:tt)*) => { $crate::info(format_args!($($t)*)) } }
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! log_debug { ($($t:tt)*) => { $crate::debug(format_args!($($t)*)) } }
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! log_warn { ($($t:tt)*) => { $crate::warn(format_args!($($t)*)) } }
|
macro_rules! log_warn { ($($t:tt)*) => { $crate::warn(format_args!($($t)*)) } }
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue