use crate::error::AnchorChainError;
use crate::node::Node;
use anchor_chain_macros::Stateless;
#[cfg(feature = "tracing")]
use tracing::instrument;
#[derive(Debug, Stateless)]
pub struct Logger<T>
where
T: std::fmt::Debug + Send + Sync,
{
prefix: String,
_marker: std::marker::PhantomData<T>,
}
impl<T> Logger<T>
where
T: std::fmt::Debug + Send + Sync,
{
pub fn new(prefix: &str) -> Self {
Self {
prefix: prefix.to_string(),
_marker: std::marker::PhantomData,
}
}
}
#[async_trait::async_trait]
impl<T> Node for Logger<T>
where
T: std::fmt::Debug + Send + Sync,
{
type Input = T;
type Output = T;
#[cfg_attr(feature = "tracing", instrument)]
async fn process(&self, input: Self::Input) -> Result<Self::Output, AnchorChainError> {
println!("{}: {:?}", self.prefix, input);
Ok(input)
}
}
impl<T> Default for Logger<T>
where
T: std::fmt::Debug + Send + Sync,
{
fn default() -> Self {
Self {
prefix: "Input".to_string(),
_marker: std::marker::PhantomData,
}
}
}