From ad89c1ac22264079c91c8c7c8a665b2c0e7b4ef8 Mon Sep 17 00:00:00 2001 From: giuseppe-g-gelardi Date: Sat, 7 Dec 2024 17:39:08 -0500 Subject: [PATCH 1/2] add logging crate with println and tracing features --- Cargo.toml | 5 ++-- logg/Cargo.toml | 16 +++++++++++++ logg/src/lib.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 logg/Cargo.toml create mode 100644 logg/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index d51baa1..fff3a10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,8 @@ [workspace] members = [ "cargobase", - "cargobase-async" + "cargobase-async", + "logg", ] [workspace.package] @@ -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" diff --git a/logg/Cargo.toml b/logg/Cargo.toml new file mode 100644 index 0000000..6306d3c --- /dev/null +++ b/logg/Cargo.toml @@ -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" diff --git a/logg/src/lib.rs b/logg/src/lib.rs new file mode 100644 index 0000000..9255383 --- /dev/null +++ b/logg/src/lib.rs @@ -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() + } +} From 5dec4573cde823957123ef190a35e5522bdbea25 Mon Sep 17 00:00:00 2001 From: giuseppe-g-gelardi Date: Sat, 7 Dec 2024 22:11:28 -0500 Subject: [PATCH 2/2] added logg crate -- will implement later --- cargobase-async/Cargo.toml | 7 ++ cargobase/Cargo.toml | 6 ++ cargobase/src/columns.rs | 2 +- logg/src/lib.rs | 205 ++++++++++++++++++++++++++++++------- 4 files changed, 184 insertions(+), 36 deletions(-) diff --git a/cargobase-async/Cargo.toml b/cargobase-async/Cargo.toml index 07e74d0..e78b8dd 100644 --- a/cargobase-async/Cargo.toml +++ b/cargobase-async/Cargo.toml @@ -3,7 +3,14 @@ name = "cargobase-async" version = "0.1.1" edition = "2021" + +[features] +default = [] # default features / no logging +logging = ["logg/logging"] # logging features with println! +tracing = ["logg/tracing"] # logging features with tracing + [dependencies] +logg = { path = "../logg" } serde = { version = "1.0.215", features = ["derive"] } serde_json = { version = "1.0.132", features = ["raw_value"] } serde_derive = "1.0.188" diff --git a/cargobase/Cargo.toml b/cargobase/Cargo.toml index 34364f4..3b07ba9 100644 --- a/cargobase/Cargo.toml +++ b/cargobase/Cargo.toml @@ -3,7 +3,13 @@ name = "cargobase" version = "0.1.1" edition = "2021" +[features] +default = [] # default features / no logging +logging = ["logg/logging"] # logging features with println! +tracing = ["logg/tracing"] # logging features with tracing + [dependencies] +logg = { path = "../logg" } serde = { version = "1.0.215", features = ["derive"] } serde_json = { version = "1.0.132", features = ["raw_value"] } serde_derive = "1.0.188" diff --git a/cargobase/src/columns.rs b/cargobase/src/columns.rs index 789087c..0fdf94f 100644 --- a/cargobase/src/columns.rs +++ b/cargobase/src/columns.rs @@ -2,7 +2,7 @@ use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use serde_json::Value; use serde_reflection::{ContainerFormat, Named, Tracer, TracerConfig}; -use tracing; +// use tracing; use crate::DatabaseError; diff --git a/logg/src/lib.rs b/logg/src/lib.rs index 9255383..d1f2d69 100644 --- a/logg/src/lib.rs +++ b/logg/src/lib.rs @@ -1,64 +1,199 @@ +// #[cfg(feature = "tracing")] +// use tracing::Level; +// // #[cfg(feature = "logging")] // use ansi_term::Colour; +/// Logs an error message. #[macro_export] -macro_rules! info { +macro_rules! error { ($($arg:tt)*) => {{ - #[cfg(feature = "logging")] - println!("{} {}", $crate::color::info_prefix(), format!($($arg)*)); #[cfg(feature = "tracing")] - tracing::info!($($arg)*); + tracing::event!(Level::ERROR, $($arg)*); + + #[cfg(feature = "logging")] + println!("{}", ansi_term::Colour::Red.paint(format!($($arg)*))); + + #[cfg(not(any(feature = "logging", feature = "tracing")))] + let _ = format!($($arg)*); // No-op }}; } +/// Logs a warning message. #[macro_export] macro_rules! warn { ($($arg:tt)*) => {{ - #[cfg(feature = "logging")] - println!("{} {}", $crate::color::warn_prefix(), format!($($arg)*)); #[cfg(feature = "tracing")] - tracing::warn!($($arg)*); + tracing::event!(Level::WARN, $($arg)*); + + #[cfg(feature = "logging")] + println!("{}", ansi_term::Colour::Yellow.paint(format!($($arg)*))); + + #[cfg(not(any(feature = "logging", feature = "tracing")))] + let _ = format!($($arg)*); // No-op }}; } +/// Logs an info message. #[macro_export] -macro_rules! error { +macro_rules! info { ($($arg:tt)*) => {{ - #[cfg(feature = "logging")] - println!("{} {}", $crate::color::error_prefix(), format!($($arg)*)); #[cfg(feature = "tracing")] - tracing::error!($($arg)*); + tracing::event!(Level::INFO, $($arg)*); + + #[cfg(feature = "logging")] + println!("{}", ansi_term::Colour::Green.paint(format!($($arg)*))); + + #[cfg(not(any(feature = "logging", feature = "tracing")))] + let _ = format!($($arg)*); // No-op }}; } +/// Logs a debug message. #[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() - } + tracing::event!(Level::DEBUG, $($arg)*); - #[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")] + println!("{}", ansi_term::Colour::Blue.paint(format!($($arg)*))); - #[cfg(feature = "logging")] - pub fn debug_prefix() -> String { - ansi_term::Colour::Blue.paint("[DEBUG]").to_string() - } + #[cfg(not(any(feature = "logging", feature = "tracing")))] + let _ = format!($($arg)*); // No-op + }}; } + +// #[cfg(feature = "logging")] +// use ansi_term::Colour; +// +// #[cfg(feature = "tracing")] +// use tracing; +// +// /// Logs an informational message. +// pub fn info(message: &str) { +// #[cfg(feature = "tracing")] +// { +// tracing::info!("{}", message); +// } +// +// #[cfg(feature = "logging")] +// { +// println!("{} {}", Colour::Green.paint("[INFO]"), message); +// } +// } +// +// /// Logs a warning message. +// pub fn warn(message: &str) { +// #[cfg(feature = "tracing")] +// { +// tracing::warn!("{}", message); +// } +// +// #[cfg(feature = "logging")] +// { +// println!("{} {}", Colour::Yellow.paint("[WARN]"), message); +// } +// } +// +// /// Logs an error message. +// pub fn error(message: &str) { +// #[cfg(feature = "tracing")] +// { +// tracing::error!("{}", message); +// } +// +// #[cfg(feature = "logging")] +// { +// println!("{} {}", Colour::Red.paint("[ERROR]"), message); +// } +// } +// +// /// Logs a debug message. +// pub fn debug(message: &str) { +// #[cfg(feature = "tracing")] +// { +// tracing::debug!("{}", message); +// } +// +// #[cfg(feature = "logging")] +// { +// println!("{} {}", Colour::Blue.paint("[DEBUG]"), message); +// } +// } +// +// // #[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! error { +// // ($($arg:tt)*) => { +// // { +// // #[cfg(feature = "tracing")] +// // { +// // tracing::error!($($arg)*); +// // } +// // #[cfg(feature = "logging")] +// // { +// // println!("\x1b[31m[ERROR]\x1b[0m {}", format!($($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()