Skip to content

Commit

Permalink
add logging crate with println and tracing features
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppe-g-gelardi committed Dec 7, 2024
1 parent fced9e1 commit ad89c1a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[workspace]
members = [
"cargobase",
"cargobase-async"
"cargobase-async",
"logg",
]

[workspace.package]
Expand All @@ -15,7 +16,7 @@ serde_json = { version = "1.0.132", features = ["raw_value"] }
serde_derive = "1.0.188"
base64 = "0.22.1"
tokio = { version = "1", features = ["full"] }
uuid = {version ="1.11.0", features = ["v4"] }
uuid = { version ="1.11.0", features = ["v4"] }
thiserror = "2.0.3"
tempfile = "3.14.0"
serde-reflection = "0.4.0"
Expand Down
16 changes: 16 additions & 0 deletions logg/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "logg"
version = "0.1.0"
edition = "2021"

[dependencies]
ansi_term = "0.12.1"
tracing = { version = "0.1", optional = true }

[features]
default = [] # no logging
logging = [] # logging with println macro
tracing = ["dep:tracing"] # logging with tracing crate

# [lib]
# path = "src/lib.rs"
64 changes: 64 additions & 0 deletions logg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// #[cfg(feature = "logging")]
// use ansi_term::Colour;

#[macro_export]
macro_rules! info {
($($arg:tt)*) => {{
#[cfg(feature = "logging")]
println!("{} {}", $crate::color::info_prefix(), format!($($arg)*));
#[cfg(feature = "tracing")]
tracing::info!($($arg)*);
}};
}

#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {{
#[cfg(feature = "logging")]
println!("{} {}", $crate::color::warn_prefix(), format!($($arg)*));
#[cfg(feature = "tracing")]
tracing::warn!($($arg)*);
}};
}

#[macro_export]
macro_rules! error {
($($arg:tt)*) => {{
#[cfg(feature = "logging")]
println!("{} {}", $crate::color::error_prefix(), format!($($arg)*));
#[cfg(feature = "tracing")]
tracing::error!($($arg)*);
}};
}

#[macro_export]
macro_rules! debug {
($($arg:tt)*) => {{
#[cfg(feature = "logging")]
println!("{} {}", $crate::color::debug_prefix(), format!($($arg)*));
#[cfg(feature = "tracing")]
tracing::debug!($($arg)*);
}};
}

pub mod color {
#[cfg(feature = "logging")]
pub fn info_prefix() -> String {
ansi_term::Colour::Green.paint("[INFO]").to_string()
}

#[cfg(feature = "logging")]
pub fn warn_prefix() -> String {
ansi_term::Colour::Yellow.paint("[WARN]").to_string()
}

#[cfg(feature = "logging")]
pub fn error_prefix() -> String {
ansi_term::Colour::Red.paint("[ERROR]").to_string()
}

#[cfg(feature = "logging")]
pub fn debug_prefix() -> String {
ansi_term::Colour::Blue.paint("[DEBUG]").to_string()
}
}

0 comments on commit ad89c1a

Please sign in to comment.