-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add logging crate with println and tracing features
- Loading branch information
1 parent
fced9e1
commit ad89c1a
Showing
3 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |