Skip to content

Commit

Permalink
Merge pull request #57 from rustaceanrob/trace-09-26
Browse files Browse the repository at this point in the history
Initialize `tracing` internally
  • Loading branch information
rustaceanrob authored Sep 26, 2024
2 parents d5436f2 + fc3e414 commit 03ccae1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
10 changes: 4 additions & 6 deletions examples/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ const PEERS: &[IpAddr] = &[IpAddr::V4(Ipv4Addr::new(23, 137, 57, 100))];

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber).unwrap();

let desc = "tr([7d94197e/86'/1'/0']tpubDCyQVJj8KzjiQsFjmb3KwECVXPvMwvAxxZGCP9XmWSopmjW3bCV3wD7TgxrUhiGSueDS1MU5X1Vb1YjYcp8jitXc5fXfdC1z68hDDEyKRNr/0/*)";
let change_desc = "tr([7d94197e/86'/1'/0']tpubDCyQVJj8KzjiQsFjmb3KwECVXPvMwvAxxZGCP9XmWSopmjW3bCV3wD7TgxrUhiGSueDS1MU5X1Vb1YjYcp8jitXc5fXfdC1z68hDDEyKRNr/1/*)";

Expand Down Expand Up @@ -43,18 +40,19 @@ async fn main() -> anyhow::Result<()> {

tokio::task::spawn(async move { node.run().await });

// Print logs to the console using the `tracing` crate
let logger = TraceLogger::new()?;

tracing::info!(
"Balance before sync: {} sats",
wallet.balance().total().to_sat()
);

// Sync and apply updates. We can do this a continual loop while the "application" is running.
// Often this loop would be on a separate "Task" in a Swift app for instance
let logger = TraceLogger::new();
// Often this would occur on a separate thread than the underlying application user interface.
loop {
if let Some(update) = client.update(&logger).await {
wallet.apply_update(update)?;
// Do something here to add more scripts?
tracing::info!("Tx count: {}", wallet.transactions().count());
tracing::info!("Balance: {}", wallet.balance().total().to_sat());
let last_revealed = wallet.derivation_index(KeychainKind::External).unwrap();
Expand Down
18 changes: 13 additions & 5 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! use bdk_kyoto::logger::{NodeMessageHandler, TraceLogger};
//! use bdk_kyoto::Warning;
//!
//! let logger = TraceLogger::new();
//! let logger = TraceLogger::new().unwrap();
//! logger.dialog("The node is running".into());
//! logger.warning(Warning::PeerTimedOut);
//! ```
Expand All @@ -34,6 +34,8 @@ use std::fmt::Debug;
use kyoto::NodeState;
use kyoto::Txid;
use kyoto::Warning;
#[cfg(feature = "trace")]
use tracing::subscriber::SetGlobalDefaultError;

/// Handle dialog and state changes from a node with some arbitrary behavior.
/// The primary purpose of this trait is not to respond to events by persisting changes,
Expand Down Expand Up @@ -106,16 +108,22 @@ impl NodeMessageHandler for PrintLogger {
}
}

/// Print messages from the node to the console
/// Print messages from the node to the console using [`tracing`].
#[cfg(feature = "trace")]
#[derive(Default, Debug)]
pub struct TraceLogger {}

#[cfg(feature = "trace")]
impl TraceLogger {
/// Build a new trace logger
pub fn new() -> Self {
Self {}
/// Build a new trace logger. This constructor will initialize the [`tracing::subscriber`] globally.
///
/// ## Errors
///
/// If [`TraceLogger::new`] has already been called.
pub fn new() -> Result<Self, SetGlobalDefaultError> {
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber)?;
Ok(Self {})
}
}

Expand Down

0 comments on commit 03ccae1

Please sign in to comment.