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) -> SyRes<()>;
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#[allow(async_fn_in_trait)]
91/// An implementation for the syslog "style" message handling (base).
92pub trait AsyncSyslogApi<F: SyslogFormatter, D: AsyncSyslogDestination>: std::fmt::Debug + Send + 'static
93{
94 /// Requests to connect to remote server.
95 async fn connectlog(&mut self) -> SyRes<()>;
96
97 /// Sets the logmask to filter out the syslog calls.
98 ///
99 /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
100 ///
101 /// # Example
102 ///
103 /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
104 ///
105 /// or
106 ///
107 /// ~(LOG_MASK!(Priority::LOG_INFO))
108 /// LOG_UPTO!(Priority::LOG_ERROR)
109 fn set_logmask(&mut self, logmask: i32) -> i32;
110
111 /// Closes connection to the syslog server
112 async fn closelog(&mut self) -> SyRes<()>;
113
114 /// Similar to libc, syslog() sends data to syslog server.
115 ///
116 /// # Arguments
117 ///
118 /// * `pri` - a priority [Priority]
119 ///
120 /// * `fmt` - a formatted [SyslogFormatter] message. In C exists a functions with
121 /// variable argumets amount. In Rust you should create your
122 /// own macros like format!() or use format!()]
123 async fn vsyslog1(&mut self, pri: Priority, fmt: F) -> SyRes<()>;
124
125
126 /// This function can be used to update the facility name, for example
127 /// after fork().
128 ///
129 /// # Arguments
130 ///
131 /// * `ident` - a new identity (up to 48 UTF8 chars)
132 fn change_identity(&mut self, ident: &str);
133
134 /// Re-opens the connection to the syslog server. Can be used to
135 /// rotate logs(handle SIGHUP).
136 ///
137 /// # Returns
138 ///
139 /// A [Result] is retured as [SyRes].
140 ///
141 /// * [Result::Ok] - with empty inner type.
142 ///
143 /// * [Result::Err] - an error code and description
144 async fn reconnect(&mut self) -> SyRes<()>;
145
146 /// Updates the instance's socket. `tap_data` [TapTypeData] should be of
147 /// the same variant (type) as current.
148 async fn update_tap_data(&mut self, tap_data: D) -> SyRes<()>;
149}
150
151