Skip to content
Merged
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ workspace = true

[features]
test-utils = []
tracing = []
7 changes: 3 additions & 4 deletions src/block_range_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ use crate::{
use alloy::{
consensus::BlockHeader,
eips::{BlockId, BlockNumberOrTag},
network::{BlockResponse, Network, primitives::HeaderResponse},
network::{BlockResponse, Network},
primitives::BlockNumber,
};
use tracing::{error, info, warn};

mod common;
mod range_iterator;
Expand Down Expand Up @@ -295,7 +294,7 @@ impl<N: Network> Service<N> {
cmd = self.command_receiver.recv() => {
if let Some(command) = cmd {
if let Err(e) = self.handle_command(command).await {
error!("Command handling error: {}", e);
error!(error = %e, "Command handling error");
self.error_count += 1;
}
} else {
Expand Down Expand Up @@ -545,7 +544,7 @@ impl<N: Network> Service<N> {
let common_ancestor = common_ancestor.header().number();
info!(
block_number = %tip_number,
hash = %tip.header().hash(),
hash = %alloy::network::primitives::HeaderResponse::hash(tip.header()),
common_ancestor = %common_ancestor,
"Reorg detected"
);
Expand Down
1 change: 0 additions & 1 deletion src/block_range_scanner/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use alloy::{
network::{BlockResponse, Network},
primitives::BlockNumber,
};
use tracing::{error, info, warn};

#[allow(clippy::too_many_arguments)]
pub(crate) async fn stream_live_blocks<N: Network>(
Expand Down
1 change: 0 additions & 1 deletion src/block_range_scanner/range_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alloy::primitives::BlockNumber;
use std::{marker::PhantomData, ops::RangeInclusive};
use tracing::debug;

pub struct Forward;
pub struct Reverse;
Expand Down
1 change: 0 additions & 1 deletion src/block_range_scanner/reorg_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use alloy::{
network::{BlockResponse, Ethereum, Network, primitives::HeaderResponse},
primitives::BlockHash,
};
use tracing::{info, warn};

use crate::{
ScannerError,
Expand Down
1 change: 0 additions & 1 deletion src/block_range_scanner/sync_handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alloy::{eips::BlockId, network::Network, primitives::BlockNumber};
use tokio::sync::mpsc;
use tracing::{error, info};

use crate::{
Notification, ScannerError,
Expand Down
5 changes: 2 additions & 3 deletions src/event_scanner/scanner/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use tokio::{
task::JoinSet,
};
use tokio_stream::{Stream, wrappers::ReceiverStream};
use tracing::{debug, error, info, trace, warn};

#[derive(Copy, Clone, Debug)]
pub(crate) enum ConsumerMode {
Expand Down Expand Up @@ -155,7 +154,7 @@ fn spawn_log_consumers_in_stream_mode<N: Network>(
break;
}
Err(RecvError::Lagged(skipped)) => {
debug!("Channel lagged, skipped {skipped} messages");
debug!(skipped_messages = skipped, "Channel lagged");
}
}
}
Expand Down Expand Up @@ -301,7 +300,7 @@ fn spawn_log_consumers_in_collection_mode<N: Network>(
break;
}
Err(RecvError::Lagged(skipped)) => {
debug!("Channel lagged, skipped {skipped} messages");
debug!(skipped_messages = skipped, "Channel lagged");
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/event_scanner/scanner/sync/from_latest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use alloy::{eips::BlockNumberOrTag, network::Network};

use tracing::{error, info};

use crate::{
EventScannerBuilder, ScannerError,
event_scanner::{
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod block_range_scanner;
#[macro_use]
mod logging;

pub mod block_range_scanner;
pub mod robust_provider;
#[cfg(any(test, feature = "test-utils"))]
pub mod test_utils;
Expand Down
104 changes: 104 additions & 0 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#[cfg(feature = "tracing")]
#[allow(unused_macros)]
macro_rules! error {
($($arg:tt)*) => {
tracing::error!($($arg)*)
};
}

#[cfg(not(feature = "tracing"))]
#[allow(unused_macros)]
macro_rules! error {
($($arg:tt)*) => {
$crate::__trace_consume!($($arg)*)
};
}

#[cfg(feature = "tracing")]
#[allow(unused_macros)]
macro_rules! warn {
($($arg:tt)*) => {
tracing::warn!($($arg)*)
};
}

#[cfg(not(feature = "tracing"))]
#[allow(unused_macros)]
macro_rules! warn {
($($arg:tt)*) => {
$crate::__trace_consume!($($arg)*)
};
}

#[cfg(feature = "tracing")]
#[allow(unused_macros)]
macro_rules! info {
($($arg:tt)*) => {
tracing::info!($($arg)*)
};
}

#[cfg(not(feature = "tracing"))]
#[allow(unused_macros)]
macro_rules! info {
($($arg:tt)*) => {
$crate::__trace_consume!($($arg)*)
};
}

#[cfg(feature = "tracing")]
#[allow(unused_macros)]
macro_rules! debug {
($($arg:tt)*) => {
tracing::debug!($($arg)*)
};
}

#[cfg(not(feature = "tracing"))]
#[allow(unused_macros)]
macro_rules! debug {
($($arg:tt)*) => {
$crate::__trace_consume!($($arg)*)
};
}

#[cfg(feature = "tracing")]
#[allow(unused_macros)]
macro_rules! trace {
($($arg:tt)*) => {
tracing::trace!($($arg)*)
};
}

#[cfg(not(feature = "tracing"))]
#[allow(unused_macros)]
macro_rules! trace {
($($arg:tt)*) => {
$crate::__trace_consume!($($arg)*)
};
}

#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "tracing"))]
#[allow(unused_macros)]
macro_rules! __trace_consume {
// field = %expr, rest...
($field:ident = % $value:expr, $($rest:tt)*) => {
{ let _ = &$value; $crate::__trace_consume!($($rest)*); }
};
// field = ?expr, rest...
($field:ident = ? $value:expr, $($rest:tt)*) => {
{ let _ = &$value; $crate::__trace_consume!($($rest)*); }
};
// field = expr, rest...
($field:ident = $value:expr, $($rest:tt)*) => {
{ let _ = &$value; $crate::__trace_consume!($($rest)*); }
};
// String literal or other tokens - ignore
($lit:literal $($rest:tt)*) => {
$crate::__trace_consume!($($rest)*)
};
// Base case - empty
() => {};
}
12 changes: 7 additions & 5 deletions src/robust_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use backon::{ExponentialBuilder, Retryable};
use futures::TryFutureExt;
use thiserror::Error;
use tokio::time::{error as TokioError, timeout};
use tracing::{error, info};

use crate::robust_provider::RobustSubscription;

Expand Down Expand Up @@ -211,7 +210,7 @@ impl<N: Network> RobustProvider<N> {
///
/// See [retry errors](#retry-errors).
pub async fn get_latest_confirmed(&self, confirmations: u64) -> Result<u64, Error> {
info!("get_latest_confirmed called with confirmations={}", confirmations);
info!(configurations = confirmations, "get_latest_confirmed called");
let latest_block = self.get_block_number().await?;
let confirmed_block = latest_block.saturating_sub(confirmations);
Ok(confirmed_block)
Expand Down Expand Up @@ -367,10 +366,13 @@ impl<N: Network> RobustProvider<N> {
let fallback_providers = self.fallback_providers.iter().enumerate().skip(start_index);
for (fallback_idx, provider) in fallback_providers {
if require_pubsub && !Self::supports_pubsub(provider) {
info!("Fallback provider {} doesn't support pubsub, skipping", fallback_idx + 1);
info!(
fallback_index = fallback_idx + 1,
"Fallback provider doesn't support pubsub, skipping"
);
continue;
}
info!("Attempting fallback provider {}/{}", fallback_idx + 1, num_fallbacks);
info!(fallback_index = fallback_idx + 1, "Attempting fallback provider");

match self.try_provider_with_timeout(provider, &operation).await {
Ok(value) => {
Expand Down Expand Up @@ -407,7 +409,7 @@ impl<N: Network> RobustProvider<N> {
(|| operation(provider.clone()))
.retry(retry_strategy)
.notify(|err: &RpcError<TransportErrorKind>, dur: Duration| {
info!(error = %err, "RPC error retrying after {:?}", dur);
info!(error = %err, duration_ms = dur.as_millis(), "RPC error retrying");
})
.sleep(tokio::time::sleep),
)
Expand Down
1 change: 0 additions & 1 deletion src/robust_provider/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use thiserror::Error;
use tokio::{sync::broadcast::error::RecvError, time::timeout};
use tokio_stream::Stream;
use tokio_util::sync::ReusableBoxFuture;
use tracing::{error, info, warn};

use crate::robust_provider::{RobustProvider, provider::CoreError};

Expand Down
1 change: 0 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fmt::Debug;

use tokio::sync::mpsc;
use tracing::{info, warn};

use crate::ScannerError;

Expand Down