rsiot/components/cmp_logger/
mod.rsuse async_trait::async_trait;
pub use tracing::Level;
use tracing::{debug, error, info, trace, warn};
use crate::{
executor::{CmpInOut, Component, ComponentError, IComponentProcess},
message::{AuthPermissions, Message, MsgDataBound, ServiceBound},
};
#[derive(Clone, Debug)]
pub struct Config<TMsg> {
pub level: Level,
pub fn_input: fn(Message<TMsg>) -> anyhow::Result<Option<String>>,
}
#[cfg_attr(not(feature = "single-thread"), async_trait)]
#[cfg_attr(feature = "single-thread", async_trait(?Send))]
impl<TMsg, TService> IComponentProcess<Config<TMsg>, TMsg, TService>
for Component<Config<TMsg>, TMsg, TService>
where
TMsg: MsgDataBound,
TService: ServiceBound,
{
async fn process(
&self,
config: Config<TMsg>,
in_out: CmpInOut<TMsg, TService>,
) -> Result<(), ComponentError> {
process(
config,
in_out.clone_with_new_id("cmp_logger", AuthPermissions::FullAccess),
)
.await
}
}
async fn process<TMsg, TService>(
config: Config<TMsg>,
mut in_out: CmpInOut<TMsg, TService>,
) -> Result<(), ComponentError>
where
TMsg: MsgDataBound,
TService: ServiceBound,
{
while let Ok(msg) = in_out.recv_input().await {
let text = (config.fn_input)(msg);
let Ok(text) = text else {
warn!("Logger Error: {:?}", text);
continue;
};
let Some(text) = text else { continue };
match config.level {
Level::TRACE => trace!("{text}"),
Level::DEBUG => debug!("{text}"),
Level::INFO => info!("{text}"),
Level::WARN => warn!("{text}"),
Level::ERROR => error!("{text}"),
}
}
Ok(())
}
pub type Cmp<TMsg, TService> = Component<Config<TMsg>, TMsg, TService>;