Skip to main content

aura_composition/adapters/
logging.rs

1//! Logging system handler adapter
2
3use crate::adapters::utils::{deserialize_operation_params, serialize_operation_result};
4use crate::registry::{HandlerContext, HandlerError, RegistrableHandler};
5use async_trait::async_trait;
6use aura_core::effects::registry as effect_registry;
7use aura_core::effects::SystemEffects;
8use aura_core::{EffectType, ExecutionMode};
9use aura_effects::system::logging::LoggingSystemHandler;
10
11/// Adapter for LoggingSystemHandler
12pub struct LoggingSystemHandlerAdapter {
13    handler: LoggingSystemHandler,
14}
15
16impl LoggingSystemHandlerAdapter {
17    pub fn new(handler: LoggingSystemHandler) -> Self {
18        Self { handler }
19    }
20}
21
22#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
23#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
24impl RegistrableHandler for LoggingSystemHandlerAdapter {
25    async fn execute_operation_bytes(
26        &self,
27        effect_type: EffectType,
28        operation: &str,
29        parameters: &[u8],
30        _ctx: &HandlerContext,
31    ) -> Result<Vec<u8>, HandlerError> {
32        if effect_type != EffectType::System {
33            return Err(HandlerError::UnsupportedEffect { effect_type });
34        }
35
36        match operation {
37            "log" => {
38                let (level, component, message): (String, String, String) =
39                    deserialize_operation_params(effect_type, operation, parameters)?;
40                self.handler
41                    .log(&level, &component, &message)
42                    .await
43                    .map_err(|e| HandlerError::ExecutionFailed {
44                        source: Box::new(e),
45                    })?;
46                Ok(Vec::new())
47            }
48            "log_with_context" => {
49                let (level, component, message, context): (
50                    String,
51                    String,
52                    String,
53                    std::collections::HashMap<String, String>,
54                ) = deserialize_operation_params(effect_type, operation, parameters)?;
55                self.handler
56                    .log_with_context(&level, &component, &message, context)
57                    .await
58                    .map_err(|e| HandlerError::ExecutionFailed {
59                        source: Box::new(e),
60                    })?;
61                Ok(Vec::new())
62            }
63            "health_check" => {
64                let result = self.handler.health_check().await.map_err(|e| {
65                    HandlerError::ExecutionFailed {
66                        source: Box::new(e),
67                    }
68                })?;
69                serialize_operation_result(effect_type, operation, &result)
70            }
71            "get_system_info" => {
72                let result = self.handler.get_system_info().await.map_err(|e| {
73                    HandlerError::ExecutionFailed {
74                        source: Box::new(e),
75                    }
76                })?;
77                serialize_operation_result(effect_type, operation, &result)
78            }
79            "set_config" => {
80                let (key, value): (String, String) =
81                    deserialize_operation_params(effect_type, operation, parameters)?;
82                self.handler.set_config(&key, &value).await.map_err(|e| {
83                    HandlerError::ExecutionFailed {
84                        source: Box::new(e),
85                    }
86                })?;
87                Ok(Vec::new())
88            }
89            "get_config" => {
90                let key: String = deserialize_operation_params(effect_type, operation, parameters)?;
91                let value = self.handler.get_config(&key).await.map_err(|e| {
92                    HandlerError::ExecutionFailed {
93                        source: Box::new(e),
94                    }
95                })?;
96                serialize_operation_result(effect_type, operation, &value)
97            }
98            "get_metrics" => {
99                let result = self.handler.get_metrics().await.map_err(|e| {
100                    HandlerError::ExecutionFailed {
101                        source: Box::new(e),
102                    }
103                })?;
104                serialize_operation_result(effect_type, operation, &result)
105            }
106            "restart_component" => {
107                let component: String =
108                    deserialize_operation_params(effect_type, operation, parameters)?;
109                self.handler
110                    .restart_component(&component)
111                    .await
112                    .map_err(|e| HandlerError::ExecutionFailed {
113                        source: Box::new(e),
114                    })?;
115                Ok(Vec::new())
116            }
117            "shutdown" => {
118                self.handler
119                    .shutdown()
120                    .await
121                    .map_err(|e| HandlerError::ExecutionFailed {
122                        source: Box::new(e),
123                    })?;
124                Ok(Vec::new())
125            }
126            _ => Err(HandlerError::UnknownOperation {
127                effect_type,
128                operation: operation.to_string(),
129            }),
130        }
131    }
132
133    fn supported_operations(&self, effect_type: EffectType) -> Vec<String> {
134        effect_registry::operations_for(effect_type)
135            .iter()
136            .map(|op| (*op).to_string())
137            .collect()
138    }
139
140    fn supports_effect(&self, effect_type: EffectType) -> bool {
141        effect_type == EffectType::System
142    }
143
144    fn execution_mode(&self) -> ExecutionMode {
145        ExecutionMode::Production
146    }
147}