summaryrefslogtreecommitdiff
path: root/rola-utils/functions
diff options
context:
space:
mode:
Diffstat (limited to 'rola-utils/functions')
-rw-r--r--rola-utils/functions/Cargo.toml1
-rw-r--r--rola-utils/functions/src/lib.rs2
-rw-r--r--rola-utils/functions/src/log_macros.rs53
3 files changed, 56 insertions, 0 deletions
diff --git a/rola-utils/functions/Cargo.toml b/rola-utils/functions/Cargo.toml
index 564dc54..6ce5adf 100644
--- a/rola-utils/functions/Cargo.toml
+++ b/rola-utils/functions/Cargo.toml
@@ -7,3 +7,4 @@ license.workspace = true
[dependencies]
tokio.workspace = true
+log.workspace = true
diff --git a/rola-utils/functions/src/lib.rs b/rola-utils/functions/src/lib.rs
index 72d5b9c..92b4f03 100644
--- a/rola-utils/functions/src/lib.rs
+++ b/rola-utils/functions/src/lib.rs
@@ -6,3 +6,5 @@ pub use levenshtein_distance::*;
mod test_sandbox;
pub use test_sandbox::*;
+
+mod log_macros;
diff --git a/rola-utils/functions/src/log_macros.rs b/rola-utils/functions/src/log_macros.rs
new file mode 100644
index 0000000..4ee4e68
--- /dev/null
+++ b/rola-utils/functions/src/log_macros.rs
@@ -0,0 +1,53 @@
+//! Custom logging macros, with the same names as `log` crate's `trace!`, `debug!`, `info!`, `warn!`, `error!`,
+//! but different behavior: automatically prepend `file!()` information to the message.
+//!
+//! # Usage
+//! ```ignore
+//! trace!("value = {}", x);
+//! debug!("something happened");
+//! info!("user {} logged in", username);
+//! warn!("disk space low: {} GB", free);
+//! error!("failed to open file: {}", path);
+//! ```
+//!
+//! Expands to:
+//! ```ignore
+//! ::log::trace!("[{}]: {}", file!(), format!("value = {}", x));
+//! ::log::debug!("[{}]: {}", file!(), format!("something happened"));
+//! ...
+//! ```
+
+#[macro_export]
+macro_rules! trace {
+ ($($arg:tt)*) => {
+ ::log::trace!("[{}]: {}", file!(), format!($($arg)*));
+ };
+}
+
+#[macro_export]
+macro_rules! debug {
+ ($($arg:tt)*) => {
+ ::log::debug!("[{}]: {}", file!(), format!($($arg)*));
+ };
+}
+
+#[macro_export]
+macro_rules! info {
+ ($($arg:tt)*) => {
+ ::log::info!("[{}]: {}", file!(), format!($($arg)*));
+ };
+}
+
+#[macro_export]
+macro_rules! warn {
+ ($($arg:tt)*) => {
+ ::log::warn!("[{}]: {}", file!(), format!($($arg)*));
+ };
+}
+
+#[macro_export]
+macro_rules! error {
+ ($($arg:tt)*) => {
+ ::log::error!("[{}]: {}", file!(), format!($($arg)*));
+ };
+}