Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize tracing internally #57

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading