Skip to content

Commit

Permalink
feat: Add tracing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Jul 19, 2024
1 parent d78cedd commit 383f22b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ repository = "https://github.com/mcmah309/error_set"

[dependencies]
error_set_impl = { version = "=0.3.2", path = "impl" }
tracing = { version = "0.1", optional = true }

[dev-dependencies]
trybuild = "^1.0.91"
tracing-test = { version = "0.2", features = ["no-env-filter"] }

[workspace]
members = ["impl"]

[features]
default = []
coerce_macro = ["error_set_impl/coerce_macro"]
tracing = ["dep:tracing"]
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#![doc = include_str!("../README.md")]

#[cfg(feature = "tracing")]
mod tracing;
#[cfg(feature = "tracing")]
pub use tracing::*;

pub use error_set_impl::*;

pub trait CoerceResult<T, E1> {
Expand Down
56 changes: 56 additions & 0 deletions src/tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::fmt::Display;

use tracing::{debug, error, info, trace, warn};

pub trait LogErr<T, E> {
fn log_e(self, context: impl Display) -> Result<T, E>;
fn log_w(self, context: impl Display) -> Result<T, E>;
fn log_i(self, context: impl Display) -> Result<T, E>;
fn log_d(self, context: impl Display) -> Result<T, E>;
fn log_t(self, context: impl Display) -> Result<T, E>;
}

impl<T, E> LogErr<T, E> for Result<T, E>
where
E: std::fmt::Debug,
{
#[inline]
fn log_e(self, context: impl Display) -> Result<T, E> {
if let Err(ref err) = self {
error!("{}: {:?}", context, err);
}
self
}

#[inline]
fn log_w(self, context: impl Display) -> Result<T, E> {
if let Err(ref err) = self {
warn!("{}: {:?}", context, err);
}
self
}

#[inline]
fn log_i(self, context: impl Display) -> Result<T, E> {
if let Err(ref err) = self {
info!("{}: {:?}", context, err);
}
self
}

#[inline]
fn log_d(self, context: impl Display) -> Result<T, E> {
if let Err(ref err) = self {
debug!("{}: {:?}", context, err);
}
self
}

#[inline]
fn log_t(self, context: impl Display) -> Result<T, E> {
if let Err(ref err) = self {
trace!("{}: {:?}", context, err);
}
self
}
}
63 changes: 63 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ pub mod readme_example_aggregation {
}

#[cfg(feature = "coerce_macro")]
#[cfg(test)]
pub mod coerce_macro {
use error_set::error_set;

Expand Down Expand Up @@ -332,6 +333,7 @@ pub mod coerce_macro {
}
}

#[cfg(test)]
pub mod documentation {
use error_set::{error_set, CoerceResult};

Expand Down Expand Up @@ -414,3 +416,64 @@ pub mod should_not_compile_tests {
t.compile_fail("tests/trybuild/recursive_dependency.rs");
}
}

#[cfg(feature = "tracing")]
#[cfg(test)]
mod tracing {
use error_set::LogErr;
use tracing_test::traced_test;

#[traced_test]
#[test]
fn test_log_error() {
let result: Result<(), &str> = Err("error");
let _ = result.log_e("An error occurred");

assert!(logs_contain("An error occurred: \"error\""));
}

#[traced_test]
#[test]
fn test_log_warn() {
let result: Result<(), &str> = Err("warning");
let _ = result.log_w("A warning occurred");

assert!(logs_contain("A warning occurred: \"warning\""));
}

#[traced_test]
#[test]
fn test_log_info() {
let result: Result<(), &str> = Err("info");
let _ = result.log_i("An info message");

assert!(logs_contain("An info message: \"info\""));
}

#[traced_test]
#[test]
fn test_log_debug() {
let result: Result<(), &str> = Err("debug");
let _ = result.log_d("A debug message");

assert!(logs_contain("A debug message: \"debug\""));
}

#[traced_test]
#[test]
fn test_log_trace() {
let result: Result<(), &str> = Err("trace");
let _ = result.log_t("A trace message");

assert!(logs_contain("A trace message: \"trace\""));
}

#[traced_test]
#[test]
fn test_log_success() {
let result: Result<(), &str> = Ok(());
let _ = result.log_e("This should not log an error");

assert!(!logs_contain("This should not log an error"));
}
}

0 comments on commit 383f22b

Please sign in to comment.