syslog_rs/a_sync/syslog_trait.rs
1/*-
2 * syslog-rs - a syslog client translated from libc to rust
3 *
4 * Copyright 2025 Aleksandr Morozov
5 *
6 * The syslog-rs crate can be redistributed and/or modified
7 * under the terms of either of the following licenses:
8 *
9 * 1. the Mozilla Public License Version 2.0 (the “MPL”) OR
10 *
11 * 2. The MIT License (MIT)
12 *
13 * 3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
14 */
15
16
17use crate::
18{
19 error::SyRes,
20 formatters::SyslogFormatter,
21 AsyncSyslogDestination,
22 Priority,
23};
24
25#[cfg(feature = "build_with_queue")]
26use crate::SyslogDestination;
27
28/// A syslog adapter to attach the async to sync queue.
29#[cfg(feature = "build_with_queue")]
30#[allow(async_fn_in_trait)]
31pub trait AsyncSyslogQueueApi<F: SyslogFormatter, D: SyslogDestination>: std::fmt::Debug + Send + 'static
32{
33 /// Requests to connect to remote server.
34 async fn a_connectlog(&mut self) -> SyRes<()>;
35
36 /// Sets the logmask to filter out the syslog calls.
37 ///
38 /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
39 ///
40 /// # Example
41 ///
42 /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
43 ///
44 /// or
45 ///
46 /// ~(LOG_MASK!(Priority::LOG_INFO))
47 /// LOG_UPTO!(Priority::LOG_ERROR)
48 async fn a_setlogmask(&self, logmask: i32) -> SyRes<i32>;
49
50 /// Closes connection to the syslog server
51 async fn a_closelog(&self) -> SyRes<()>;
52
53 /// Similar to libc, syslog() sends data to syslog server.
54 ///
55 /// # Arguments
56 ///
57 /// * `pri` - a priority [Priority]
58 ///
59 /// * `fmt` - a string message. In C exists a functions with
60 /// variable argumets amount. In Rust you should create your
61 /// own macros like format!() or use format!()]
62 async fn a_syslog(&self, pri: Priority, fmt: F);
63
64 /// This function can be used to update the facility name, for example
65 /// after fork().
66 ///
67 /// # Arguments
68 ///
69 /// * `ident` - a new identity (up to 48 UTF8 chars)
70 async fn a_change_identity(&self, ident: &str) -> SyRes<()>;
71
72 /// Re-opens the connection to the syslog server. Can be used to
73 /// rotate logs(handle SIGHUP).
74 ///
75 /// # Returns
76 ///
77 /// A [Result] is retured as [SyRes].
78 ///
79 /// * [Result::Ok] - with empty inner type.
80 ///
81 /// * [Result::Err] - an error code and description
82 async fn a_reconnect(&self) -> SyRes<()>;
83
84 /// Updates the instance's socket. `tap_data` [TapTypeData] should be of
85 /// the same variant (type) as current.
86 async fn a_update_tap_data(&self, tap_data: D) -> SyRes<()>;
87}
88
89
90/// An implementation for the syslog "style" message handling (base).
91pub(crate) trait AsyncSyslogApi<F: SyslogFormatter, D: AsyncSyslogDestination>: std::fmt::Debug + Send + 'static
92{
93 /// Requests to connect to remote server.
94 async fn connectlog(&mut self) -> SyRes<()>;
95
96 /// Sets the logmask to filter out the syslog calls.
97 ///
98 /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
99 ///
100 /// # Example
101 ///
102 /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
103 ///
104 /// or
105 ///
106 /// ~(LOG_MASK!(Priority::LOG_INFO))
107 /// LOG_UPTO!(Priority::LOG_ERROR)
108 fn set_logmask(&mut self, logmask: i32) -> i32;
109
110 /// Closes connection to the syslog server
111 async fn closelog(&mut self) -> SyRes<()>;
112
113 /// Similar to libc, syslog() sends data to syslog server.
114 ///
115 /// # Arguments
116 ///
117 /// * `pri` - a priority [Priority]
118 ///
119 /// * `fmt` - a formatted [SyslogFormatter] message. In C exists a functions with
120 /// variable argumets amount. In Rust you should create your
121 /// own macros like format!() or use format!()]
122 async fn vsyslog1(&mut self, pri: Priority, fmt: F);
123
124
125 /// This function can be used to update the facility name, for example
126 /// after fork().
127 ///
128 /// # Arguments
129 ///
130 /// * `ident` - a new identity (up to 48 UTF8 chars)
131 fn change_identity(&mut self, ident: &str);
132
133 /// Re-opens the connection to the syslog server. Can be used to
134 /// rotate logs(handle SIGHUP).
135 ///
136 /// # Returns
137 ///
138 /// A [Result] is retured as [SyRes].
139 ///
140 /// * [Result::Ok] - with empty inner type.
141 ///
142 /// * [Result::Err] - an error code and description
143 async fn reconnect(&mut self) -> SyRes<()>;
144
145 /// Updates the instance's socket. `tap_data` [TapTypeData] should be of
146 /// the same variant (type) as current.
147 async fn update_tap_data(&mut self, tap_data: D) -> SyRes<()>;
148}
149
150