tracing_helpers/lib.rs
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Helpers for using the tracing crate more effectively.
5//!
6//! In particular, this includes extension traits to make it easier to pass
7//! errors to tracing events. The events can take a `dyn Error`, but getting a
8//! `dyn Error` from an arbitrary error type requires casting.
9//!
10//! Hopefully this crate will be short lived as `tracing` ergonomics continue
11//! to improve.
12
13#![forbid(unsafe_code)]
14
15pub mod formatter;
16
17/// Extension trait to make it easy to trace anyhow errors.
18pub trait AnyhowValueExt {
19 /// Returns the error as a type that can be traced.
20 fn as_error(&self) -> &(dyn 'static + std::error::Error);
21}
22
23impl AnyhowValueExt for anyhow::Error {
24 fn as_error(&self) -> &(dyn 'static + std::error::Error) {
25 &**self
26 }
27}
28
29/// Extension trait to make it easy to trace errors.
30pub trait ErrorValueExt {
31 /// Returns the error as a type that can be traced.
32 fn as_error(&self) -> &(dyn 'static + std::error::Error);
33}
34
35impl<T> ErrorValueExt for T
36where
37 T: 'static + std::error::Error,
38{
39 fn as_error(&self) -> &(dyn 'static + std::error::Error) {
40 self
41 }
42}