Skip to content

Commit

Permalink
Introduce tracing macros to the preimage crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Sep 23, 2023
1 parent bb81005 commit abfb950
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/preimage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ tokio = { version = "1.32.0", features = ["full"] }

# misc
anyhow = "1.0.75"
tracing = "0.1.37"
tracing = { version = "0.1.37", optional = true }

[dev-dependencies]
rand = "0.8.5"

[features]
tracing = []
tracing = ["dep:tracing"]
3 changes: 3 additions & 0 deletions crates/preimage/src/hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ impl Hinter for HintWriter {
hint_bytes[0..4].copy_from_slice((hint.len() as u32).to_be_bytes().as_ref());
hint_bytes[4..].copy_from_slice(hint);

crate::debug!("Sending hint: {:?}", hint_bytes);
self.tx.send(hint_bytes)?;

let n = self.rx.recv()?;
if n.len() != 1 {
crate::error!("Failed to read hint ack, received response: {:?}", n);
anyhow::bail!(
"Failed to read invalid pre-image hint ack, received response: {:?}",
n
Expand Down Expand Up @@ -74,6 +76,7 @@ impl HintReader {
if let Err(e) = router(&payload) {
// Write back on error to unblock the hint writer.
self.tx.send(vec![0])?;
crate::error!("Failed to handle hint: {:?}", e);
anyhow::bail!("Failed to handle hint: {:?}", e);
}

Expand Down
2 changes: 2 additions & 0 deletions crates/preimage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![doc = include_str!("../README.md")]
#![allow(dead_code, unused_variables)]

pub(crate) mod traces;

mod oracle;
pub use oracle::{OracleClient, OracleServer};

Expand Down
49 changes: 49 additions & 0 deletions crates/preimage/src/traces.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// Performs a tracing debug if the `tracing` feature is enabled.
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
tracing::debug!($($arg)*);
};
}
pub use debug;

/// Performs a tracing info if the `tracing` feature is enabled.
#[macro_export]
macro_rules! info {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
tracing::info!($($arg)*);
};
}
pub use info;

/// Performs a tracing error if the `tracing` feature is enabled.
#[macro_export]
macro_rules! error {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
tracing::error!($($arg)*);
};
}
pub use error;

/// Performs a tracing warn if the `tracing` feature is enabled.
#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
tracing::warn!($($arg)*);
};
}
pub use crate::warn;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_debug() {
debug!("test");
}
}

0 comments on commit abfb950

Please sign in to comment.