use std::fmt::Debug;
use kyoto::NodeState;
use kyoto::Txid;
use kyoto::Warning;
#[cfg(feature = "trace")]
use tracing::subscriber::SetGlobalDefaultError;
use crate::NodeEventHandler;
#[derive(Default, Debug)]
pub struct PrintLogger {}
impl PrintLogger {
pub fn new() -> Self {
Self {}
}
}
impl NodeEventHandler for PrintLogger {
fn dialog(&self, dialog: String) {
println!("{dialog}");
}
fn warning(&self, warning: Warning) {
println!("{warning}");
}
fn state_changed(&self, state: NodeState) {
println!("State change: {state}");
}
fn tx_sent(&self, txid: Txid) {
println!("Transaction sent: {txid}");
}
fn tx_failed(&self, txid: Txid) {
println!("Transaction failed: {txid}");
}
fn blocks_disconnected(&self, blocks: Vec<u32>) {
for block in blocks {
println!("Block {block} was reorganized");
}
}
fn synced(&self, tip: u32) {
println!("Synced to tip {tip}");
}
fn connections_met(&self) {
println!("Required connections met");
}
}
#[cfg(feature = "trace")]
#[derive(Default, Debug)]
pub struct TraceLogger {}
#[cfg(feature = "trace")]
impl TraceLogger {
pub fn new() -> Result<Self, SetGlobalDefaultError> {
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber)?;
Ok(Self {})
}
}
#[cfg(feature = "trace")]
impl NodeEventHandler for TraceLogger {
fn dialog(&self, dialog: String) {
tracing::info!("{dialog}")
}
fn warning(&self, warning: Warning) {
tracing::warn!("{warning}")
}
fn state_changed(&self, state: NodeState) {
tracing::info!("State change: {state}")
}
fn tx_sent(&self, txid: Txid) {
tracing::info!("Transaction sent: {txid}")
}
fn tx_failed(&self, txid: Txid) {
tracing::info!("Transaction failed: {txid}")
}
fn blocks_disconnected(&self, blocks: Vec<u32>) {
for block in blocks {
tracing::warn!("Block {block} was reorganized");
}
}
fn synced(&self, tip: u32) {
tracing::info!("Synced to height: {tip}")
}
fn connections_met(&self) {
tracing::info!("Required connections met")
}
}
impl NodeEventHandler for () {
fn dialog(&self, _dialog: String) {}
fn warning(&self, _warning: Warning) {}
fn state_changed(&self, _state: NodeState) {}
fn connections_met(&self) {}
fn tx_sent(&self, _txid: Txid) {}
fn tx_failed(&self, _txid: Txid) {}
fn blocks_disconnected(&self, _blocks: Vec<u32>) {}
fn synced(&self, _tip: u32) {}
}