rsiot/components/cmp_logger/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! Компонент для логгирования сообщений

use 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,

    /// Функция преобразования входящих сообщений в записи.
    ///
    /// Можно реализовать фильтрацию сообщений.
    ///
    /// **Примеры**
    ///
    /// - Логгирование всех сообщений
    ///
    /// ```rust
    /// fn_input: |msg| Ok(Some(msg.serialize()?)),
    /// ```
    ///
    /// - Логгирование всех сообщений с заголовком
    ///
    /// ```rust
    /// fn_input: |msg| {
    ///     let text = msg.serialize()?;
    ///     let text = format!("Header: {text}");
    ///     Ok(Some(text))
    /// },
    /// ```
    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(())
}

/// Компонент cmp_logger
pub type Cmp<TMsg, TService> = Component<Config<TMsg>, TMsg, TService>;