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