Skip to content

Commit c3ebabd

Browse files
committed
feat: introduce bdk_common crate with log macros
1 parent 8474e73 commit c3ebabd

File tree

13 files changed

+187
-114
lines changed

13 files changed

+187
-114
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"crates/esplora",
99
"crates/bitcoind_rpc",
1010
"crates/testenv",
11+
"crates/common",
1112
"examples/example_cli",
1213
"examples/example_electrum",
1314
"examples/example_esplora",

crates/bitcoind_rpc/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ workspace = true
1919
bitcoin = { version = "0.32.0", default-features = false }
2020
bitcoincore-rpc = { version = "0.19.0" }
2121
bdk_core = { path = "../core", version = "0.6.0", default-features = false }
22-
tracing = { version = "0.1", optional = true }
22+
bdk_common = { path = "../common" }
2323

2424
[dev-dependencies]
2525
bdk_bitcoind_rpc = { path = "." }
@@ -30,7 +30,7 @@ bdk_chain = { path = "../chain" }
3030
default = ["std"]
3131
std = ["bitcoin/std", "bdk_core/std"]
3232
serde = ["bitcoin/serde", "bdk_core/serde"]
33-
tracing-logs = ["tracing"]
33+
log = ["bdk_common/log"]
3434

3535
[[example]]
3636
name = "filter_iter"

crates/bitcoind_rpc/src/lib.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
#[macro_use]
1515
extern crate alloc;
1616

17+
#[allow(unused_imports)]
18+
#[macro_use]
19+
extern crate bdk_common;
20+
1721
use alloc::sync::Arc;
1822
use bdk_core::collections::{HashMap, HashSet};
1923
use bdk_core::{BlockId, CheckPoint};
2024
use bitcoin::{Block, BlockHash, Transaction, Txid};
2125
use bitcoincore_rpc::{bitcoincore_rpc_json, RpcApi};
2226
use core::ops::Deref;
2327

24-
#[cfg(feature = "tracing-logs")]
25-
use tracing::trace;
26-
2728
pub mod bip158;
2829

2930
pub use bitcoincore_rpc;
@@ -127,8 +128,7 @@ where
127128
pub fn mempool_at(&mut self, sync_time: u64) -> Result<MempoolEvent, bitcoincore_rpc::Error> {
128129
let client = &*self.client;
129130

130-
#[cfg(feature = "tracing-logs")]
131-
trace!(
131+
log_trace!(
132132
start_height = self.start_height,
133133
sync_time = sync_time,
134134
"enter mempool_at"
@@ -174,8 +174,7 @@ where
174174
..Default::default()
175175
};
176176

177-
#[cfg(feature = "tracing-logs")]
178-
trace!(
177+
log_trace!(
179178
rpc_mempool_count = rpc_mempool_txids.len(),
180179
rpc_height = rpc_tip_height,
181180
rpc_block_hash = %rpc_tip_hash,
@@ -218,8 +217,7 @@ where
218217

219218
/// Emit the next block height and block (if any).
220219
pub fn next_block(&mut self) -> Result<Option<BlockEvent<Block>>, bitcoincore_rpc::Error> {
221-
#[cfg(feature = "tracing-logs")]
222-
trace!(
220+
log_trace!(
223221
last_block_height = self.last_block.as_ref().map(|r| r.height),
224222
"enter next_block"
225223
);
@@ -229,8 +227,7 @@ where
229227
for tx in &block.txdata {
230228
self.mempool_snapshot.remove(&tx.compute_txid());
231229
}
232-
#[cfg(feature = "tracing-logs")]
233-
trace!(
230+
log_trace!(
234231
block_height = checkpoint.height(),
235232
tx_count = block.txdata.len(),
236233
"emit block"
@@ -309,8 +306,7 @@ where
309306
C: Deref,
310307
C::Target: RpcApi,
311308
{
312-
#[cfg(feature = "tracing-logs")]
313-
trace!(
309+
log_trace!(
314310
last_block_height = emitter.last_block.as_ref().map(|r| r.height),
315311
start_height = emitter.start_height,
316312
"enter poll_once"
@@ -324,16 +320,14 @@ where
324320
let next_hash = client.get_block_hash(emitter.start_height as _)?;
325321
// make sure last emission is still in best chain
326322
if client.get_block_hash(last_res.height as _)? != last_res.hash {
327-
#[cfg(feature = "tracing-logs")]
328-
trace!("block not in best chain");
323+
log_trace!("block not in best chain");
329324
return Ok(PollResponse::BlockNotInBestChain);
330325
}
331326
next_hash
332327
} else {
333328
match last_res.nextblockhash {
334329
None => {
335-
#[cfg(feature = "tracing-logs")]
336-
trace!("no more blocks");
330+
log_trace!("no more blocks");
337331
return Ok(PollResponse::NoMoreBlocks);
338332
}
339333
Some(next_hash) => next_hash,
@@ -342,13 +336,11 @@ where
342336

343337
let res = client.get_block_info(&next_hash)?;
344338
if res.confirmations < 0 {
345-
#[cfg(feature = "tracing-logs")]
346-
trace!("block not in best chain");
339+
log_trace!("block not in best chain");
347340
return Ok(PollResponse::BlockNotInBestChain);
348341
}
349342

350-
#[cfg(feature = "tracing-logs")]
351-
trace!(
343+
log_trace!(
352344
height = res.height,
353345
hash = %res.hash,
354346
"agreement found"
@@ -372,8 +364,7 @@ where
372364
};
373365

374366
// agreement point found
375-
#[cfg(feature = "tracing-logs")]
376-
trace!(
367+
log_trace!(
377368
"poll(): PollResponse::AgreementFound, height={}, hash={}",
378369
res.height,
379370
res.hash

crates/common/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "bdk_common"
3+
version = "0.1.0"
4+
edition = "2021"
5+
rust-version = "1.63"
6+
homepage = "https://bitcoindevkit.org"
7+
repository = "https://github.com/bitcoindevkit/bdk"
8+
description = "Shared utilities and macros for BDK chain sources."
9+
license = "MIT OR Apache-2.0"
10+
publish = false
11+
12+
[dependencies]
13+
tracing = { version = "0.1", optional = true }
14+
15+
[features]
16+
log = ["tracing"]

crates/common/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[cfg(feature = "log")]
2+
pub use tracing;
3+
4+
#[macro_use]
5+
mod log;

crates/common/src/log.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/// Trace-level logging. Expands to `tracing::trace!()` when the `log` feature is enabled,
2+
/// otherwise into a no-op.
3+
#[cfg(feature = "log")]
4+
#[macro_export]
5+
macro_rules! log_trace {
6+
($($tt:tt)*) => {
7+
$crate::tracing::trace!($($tt)*);
8+
};
9+
}
10+
#[cfg(not(feature = "log"))]
11+
#[macro_export]
12+
macro_rules! log_trace {
13+
($($tt:tt)*) => {};
14+
}
15+
16+
/// Trace-level span. Expands to `tracing::trace_span!()` when the `log` feature is enabled,
17+
/// otherwise to a dummy no-op span whose `.entered()` does nothing.
18+
#[cfg(feature = "log")]
19+
#[macro_export]
20+
macro_rules! log_span {
21+
( $($tts:tt)* ) => {
22+
$crate::tracing::trace_span!($($tts)*)
23+
};
24+
}
25+
#[cfg(not(feature = "log"))]
26+
#[macro_export]
27+
macro_rules! log_span {
28+
( $($tts:tt)* ) => {{
29+
struct NoopSpan;
30+
impl NoopSpan {
31+
#[allow(dead_code)]
32+
pub fn entered(self) -> NoopEntered {
33+
NoopEntered
34+
}
35+
}
36+
struct NoopEntered;
37+
NoopSpan
38+
}};
39+
}
40+
41+
/// Debug-level logging. Expands to `tracing::debug!()` when the `log` feature is enabled,
42+
/// otherwise into a no-op.
43+
#[cfg(feature = "log")]
44+
#[macro_export]
45+
macro_rules! log_debug {
46+
($($tt:tt)*) => {
47+
$crate::tracing::debug!($($tt)*);
48+
};
49+
}
50+
#[cfg(not(feature = "log"))]
51+
#[macro_export]
52+
macro_rules! log_debug {
53+
($($tt:tt)*) => {};
54+
}
55+
56+
/// Info-level logging. Expands to `tracing::info!()` when the `log` feature is enabled,
57+
/// otherwise into a no-op.
58+
#[cfg(feature = "log")]
59+
#[macro_export]
60+
macro_rules! log_info {
61+
($($tt:tt)*) => {
62+
$crate::tracing::info!($($tt)*);
63+
};
64+
}
65+
#[cfg(not(feature = "log"))]
66+
#[macro_export]
67+
macro_rules! log_info {
68+
($($tt:tt)*) => {};
69+
}
70+
71+
/// Warn-level logging. Expands to `tracing::warn!()` when the `log` feature is enabled,
72+
/// otherwise into a no-op.
73+
#[cfg(feature = "log")]
74+
#[macro_export]
75+
macro_rules! log_warn {
76+
($($tt:tt)*) => {
77+
$crate::tracing::warn!($($tt)*);
78+
};
79+
}
80+
#[cfg(not(feature = "log"))]
81+
#[macro_export]
82+
macro_rules! log_warn {
83+
($($tt:tt)*) => {};
84+
}
85+
86+
/// Error-level logging. Expands to `tracing::error!()` when the `log` feature is enabled,
87+
/// otherwise into a no-op.
88+
#[cfg(feature = "log")]
89+
#[macro_export]
90+
macro_rules! log_error {
91+
($($tt:tt)*) => {
92+
$crate::tracing::error!($($tt)*);
93+
};
94+
}
95+
#[cfg(not(feature = "log"))]
96+
#[macro_export]
97+
macro_rules! log_error {
98+
($($tt:tt)*) => {};
99+
}

crates/electrum/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ workspace = true
1414

1515
[dependencies]
1616
bdk_core = { path = "../core", version = "0.6.0" }
17+
bdk_common = { path = "../common" }
1718
electrum-client = { version = "0.24.0", features = [ "proxy" ], default-features = false }
18-
tracing = { version = "0.1", optional = true }
1919

2020
[dev-dependencies]
2121
bdk_testenv = { path = "../testenv" }
@@ -27,7 +27,7 @@ default = ["use-rustls"]
2727
use-rustls = ["electrum-client/use-rustls"]
2828
use-rustls-ring = ["electrum-client/use-rustls-ring"]
2929
use-openssl = ["electrum-client/use-openssl"]
30-
tracing-logs = ["tracing"]
30+
log = ["bdk_common/log"]
3131

3232
[[test]]
3333
name = "test_electrum"

0 commit comments

Comments
 (0)