Skip to content

Commit d9c3aeb

Browse files
0xNeshiLeoPatOZ
andauthored
Use macro wrappers for feature-gated tracing macros, turning all of them "off" by default (#270)
Co-authored-by: Leo <[email protected]>
1 parent 5f594f4 commit d9c3aeb

File tree

13 files changed

+120
-21
lines changed

13 files changed

+120
-21
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,4 @@ workspace = true
7474

7575
[features]
7676
test-utils = []
77+
tracing = []

src/block_range_scanner.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ use crate::{
7676
use alloy::{
7777
consensus::BlockHeader,
7878
eips::{BlockId, BlockNumberOrTag},
79-
network::{BlockResponse, Network, primitives::HeaderResponse},
79+
network::{BlockResponse, Network},
8080
primitives::BlockNumber,
8181
};
82-
use tracing::{error, info, warn};
8382

8483
mod common;
8584
mod range_iterator;
@@ -295,7 +294,7 @@ impl<N: Network> Service<N> {
295294
cmd = self.command_receiver.recv() => {
296295
if let Some(command) = cmd {
297296
if let Err(e) = self.handle_command(command).await {
298-
error!("Command handling error: {}", e);
297+
error!(error = %e, "Command handling error");
299298
self.error_count += 1;
300299
}
301300
} else {
@@ -545,7 +544,7 @@ impl<N: Network> Service<N> {
545544
let common_ancestor = common_ancestor.header().number();
546545
info!(
547546
block_number = %tip_number,
548-
hash = %tip.header().hash(),
547+
hash = %alloy::network::primitives::HeaderResponse::hash(tip.header()),
549548
common_ancestor = %common_ancestor,
550549
"Reorg detected"
551550
);

src/block_range_scanner/common.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use alloy::{
1313
network::{BlockResponse, Network},
1414
primitives::BlockNumber,
1515
};
16-
use tracing::{error, info, warn};
1716

1817
#[allow(clippy::too_many_arguments)]
1918
pub(crate) async fn stream_live_blocks<N: Network>(

src/block_range_scanner/range_iterator.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloy::primitives::BlockNumber;
22
use std::{marker::PhantomData, ops::RangeInclusive};
3-
use tracing::debug;
43

54
pub struct Forward;
65
pub struct Reverse;

src/block_range_scanner/reorg_handler.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use alloy::{
44
network::{BlockResponse, Ethereum, Network, primitives::HeaderResponse},
55
primitives::BlockHash,
66
};
7-
use tracing::{info, warn};
87

98
use crate::{
109
ScannerError,

src/block_range_scanner/sync_handler.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloy::{eips::BlockId, network::Network, primitives::BlockNumber};
22
use tokio::sync::mpsc;
3-
use tracing::{error, info};
43

54
use crate::{
65
Notification, ScannerError,

src/event_scanner/scanner/common.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use tokio::{
2020
task::JoinSet,
2121
};
2222
use tokio_stream::{Stream, wrappers::ReceiverStream};
23-
use tracing::{debug, error, info, trace, warn};
2423

2524
#[derive(Copy, Clone, Debug)]
2625
pub(crate) enum ConsumerMode {
@@ -155,7 +154,7 @@ fn spawn_log_consumers_in_stream_mode<N: Network>(
155154
break;
156155
}
157156
Err(RecvError::Lagged(skipped)) => {
158-
debug!("Channel lagged, skipped {skipped} messages");
157+
debug!(skipped_messages = skipped, "Channel lagged");
159158
}
160159
}
161160
}
@@ -301,7 +300,7 @@ fn spawn_log_consumers_in_collection_mode<N: Network>(
301300
break;
302301
}
303302
Err(RecvError::Lagged(skipped)) => {
304-
debug!("Channel lagged, skipped {skipped} messages");
303+
debug!(skipped_messages = skipped, "Channel lagged");
305304
}
306305
}
307306
}

src/event_scanner/scanner/sync/from_latest.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use alloy::{eips::BlockNumberOrTag, network::Network};
22

3-
use tracing::{error, info};
4-
53
use crate::{
64
EventScannerBuilder, ScannerError,
75
event_scanner::{

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
pub mod block_range_scanner;
1+
#[macro_use]
2+
mod logging;
23

4+
pub mod block_range_scanner;
35
pub mod robust_provider;
46
#[cfg(any(test, feature = "test-utils"))]
57
pub mod test_utils;

src/logging.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#[cfg(feature = "tracing")]
2+
#[allow(unused_macros)]
3+
macro_rules! error {
4+
($($arg:tt)*) => {
5+
tracing::error!($($arg)*)
6+
};
7+
}
8+
9+
#[cfg(not(feature = "tracing"))]
10+
#[allow(unused_macros)]
11+
macro_rules! error {
12+
($($arg:tt)*) => {
13+
$crate::__trace_consume!($($arg)*)
14+
};
15+
}
16+
17+
#[cfg(feature = "tracing")]
18+
#[allow(unused_macros)]
19+
macro_rules! warn {
20+
($($arg:tt)*) => {
21+
tracing::warn!($($arg)*)
22+
};
23+
}
24+
25+
#[cfg(not(feature = "tracing"))]
26+
#[allow(unused_macros)]
27+
macro_rules! warn {
28+
($($arg:tt)*) => {
29+
$crate::__trace_consume!($($arg)*)
30+
};
31+
}
32+
33+
#[cfg(feature = "tracing")]
34+
#[allow(unused_macros)]
35+
macro_rules! info {
36+
($($arg:tt)*) => {
37+
tracing::info!($($arg)*)
38+
};
39+
}
40+
41+
#[cfg(not(feature = "tracing"))]
42+
#[allow(unused_macros)]
43+
macro_rules! info {
44+
($($arg:tt)*) => {
45+
$crate::__trace_consume!($($arg)*)
46+
};
47+
}
48+
49+
#[cfg(feature = "tracing")]
50+
#[allow(unused_macros)]
51+
macro_rules! debug {
52+
($($arg:tt)*) => {
53+
tracing::debug!($($arg)*)
54+
};
55+
}
56+
57+
#[cfg(not(feature = "tracing"))]
58+
#[allow(unused_macros)]
59+
macro_rules! debug {
60+
($($arg:tt)*) => {
61+
$crate::__trace_consume!($($arg)*)
62+
};
63+
}
64+
65+
#[cfg(feature = "tracing")]
66+
#[allow(unused_macros)]
67+
macro_rules! trace {
68+
($($arg:tt)*) => {
69+
tracing::trace!($($arg)*)
70+
};
71+
}
72+
73+
#[cfg(not(feature = "tracing"))]
74+
#[allow(unused_macros)]
75+
macro_rules! trace {
76+
($($arg:tt)*) => {
77+
$crate::__trace_consume!($($arg)*)
78+
};
79+
}
80+
81+
#[doc(hidden)]
82+
#[macro_export]
83+
#[cfg(not(feature = "tracing"))]
84+
#[allow(unused_macros)]
85+
macro_rules! __trace_consume {
86+
// field = %expr, rest...
87+
($field:ident = % $value:expr, $($rest:tt)*) => {
88+
{ let _ = &$value; $crate::__trace_consume!($($rest)*); }
89+
};
90+
// field = ?expr, rest...
91+
($field:ident = ? $value:expr, $($rest:tt)*) => {
92+
{ let _ = &$value; $crate::__trace_consume!($($rest)*); }
93+
};
94+
// field = expr, rest...
95+
($field:ident = $value:expr, $($rest:tt)*) => {
96+
{ let _ = &$value; $crate::__trace_consume!($($rest)*); }
97+
};
98+
// String literal or other tokens - ignore
99+
($lit:literal $($rest:tt)*) => {
100+
$crate::__trace_consume!($($rest)*)
101+
};
102+
// Base case - empty
103+
() => {};
104+
}

0 commit comments

Comments
 (0)