eh-log: add debug level; respect verbosity levels

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Idc62fd26efc10f0c1e49424cf337e6d16a6a6964
This commit is contained in:
raf 2026-05-12 14:48:12 +03:00
commit 4707a7e49f
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
2 changed files with 44 additions and 6 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "eh-log"
description = "Styled logging for eh"
description = "Tiny, styled logging crate for eh"
version.workspace = true
edition.workspace = true
authors.workspace = true

View file

@ -1,26 +1,64 @@
use std::fmt;
use std::{
fmt,
sync::atomic::{AtomicI8, Ordering},
};
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) {
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) {
eprintln!(" {} {args}", "->".yellow().bold());
if enabled(Level::Warn) {
eprintln!(" {} {args}", "->".yellow().bold());
}
}
pub fn error(args: fmt::Arguments) {
eprintln!(" {} {args}", "!".red().bold());
if enabled(Level::Error) {
eprintln!(" {} {args}", "!".red().bold());
}
}
pub fn hint(args: fmt::Arguments) {
eprintln!(" {} {args}", "~".yellow().dim());
if enabled(Level::Info) {
eprintln!(" {} {args}", "~".yellow().dim());
}
}
#[macro_export]
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_rules! log_warn { ($($t:tt)*) => { $crate::warn(format_args!($($t)*)) } }