syslog_rs/a_sync/
syslog_async_shared.rs

1/*-
2 * syslog-rs - a syslog client translated from libc to rust
3 * 
4 * Copyright 2025 Aleksandr Morozov
5 * 
6 * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
7 * the European Commission - subsequent versions of the EUPL (the "Licence").
8 * 
9 * You may not use this work except in compliance with the Licence.
10 * 
11 * You may obtain a copy of the Licence at:
12 * 
13 *    https://joinup.ec.europa.eu/software/page/eupl
14 * 
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 * Licence for the specific language governing permissions and limitations
19 * under the Licence.
20 */
21
22use std::sync::Arc;
23
24use tokio::sync::Mutex;
25
26use async_trait::async_trait;
27
28use crate::formatters::SyslogFormatter;
29
30use crate::{common::*, socket::TapTypeData};
31use crate::error::SyRes;
32
33
34use super::syslog_async_internal::AsyncSyslogInternal;
35use super::syslog_trait::AsyncSyslogApi;
36
37
38/// A common instance which describes the syslog state
39#[derive(Debug)]
40pub struct AsyncSyslogShared<F: SyslogFormatter + Sync>
41{   
42    /// A giant lock to synchronize the access to assets of the [SyslogInternal]
43    inner: Arc<Mutex<AsyncSyslogInternal<F>>>,
44}
45
46unsafe impl<F: SyslogFormatter + Sync> Send for AsyncSyslogShared<F> {}
47unsafe impl<F: SyslogFormatter + Sync> Sync for AsyncSyslogShared<F> {}
48
49impl<F: SyslogFormatter + Sync> AsyncSyslogShared<F>
50{
51    pub async 
52    fn openlog(ident: Option<&str>, logstat: LogStat, facility: LogFacility, net_tap: TapTypeData) -> SyRes<AsyncSyslogShared<F>> 
53    {
54        let mut syslog = 
55            AsyncSyslogInternal::<F>::new(ident, logstat, facility, net_tap)?;
56       
57        if logstat.contains(LogStat::LOG_NDELAY) == true
58        {
59            syslog.connectlog().await?;
60        }
61
62        return Ok( 
63            Self
64            {
65                inner: Arc::new(Mutex::new(syslog)),
66            }
67        );
68    }
69}
70
71
72#[async_trait]
73impl<F: SyslogFormatter + Sync> AsyncSyslogApi for AsyncSyslogShared<F>
74{
75    async 
76    fn connectlog(&mut self) -> SyRes<()>
77    {
78        return self.inner.lock().await.connectlog().await;
79    }
80
81    async 
82    fn setlogmask(&self, logmask: i32) -> SyRes<i32>
83    {
84        return Ok(
85            self
86                .inner
87                .lock()
88                .await
89                .set_logmask(logmask)
90        );
91    }
92
93    async 
94    fn closelog(&self) -> SyRes<()>
95    {
96        return self.inner.lock().await.disconnectlog().await;
97    }
98
99    #[inline]
100    async 
101    fn syslog(&self, pri: Priority, fmt: String)
102    {
103        self
104            .vsyslog(pri, fmt.as_str())
105            .await;
106    }
107
108    #[inline]
109    async 
110    fn vsyslog(&self, pri: Priority, fmt: &str)
111    {
112        self
113            .inner
114            .lock()
115            .await
116            .vsyslog1(pri, fmt.as_ref())
117            .await;
118    }
119
120    async 
121    fn change_identity(&self, ident: &str) -> SyRes<()>
122    {
123        self
124            .inner
125            .lock()
126            .await
127            .set_logtag(ident, true);
128
129        return Ok(());
130    }
131
132    async 
133    fn reconnect(&self) -> SyRes<()>
134    {
135        let mut lock = 
136            self
137                .inner
138                .lock()
139                .await;
140
141        
142        lock.disconnectlog().await?;
143
144        lock.connectlog().await?;
145            
146        return Ok(());
147    }
148
149    async 
150    fn update_tap_data(&self, tap_data: TapTypeData) -> SyRes<()>
151    {
152        let mut lock = 
153            self
154                .inner
155                .lock()
156                .await;
157
158        return lock.update_tap_data(tap_data).await;
159    }
160}