syslog_rs/
lib.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//! syslog-rs
15//! 
16//! 
17//! An implementation of the syslog from glibc/libc like it was designed in
18//! in both system libraries. The API is almost compatible with what is in
19//! libc/glibc.
20//! 
21//! ## Supports
22//! 
23//! * GNU/Linux RFC3164 (UTF-8 by default)
24//! * *BSD and OSX RFC5424 (BOM UTF-8 by default)
25//! 
26//! Directories:
27//! * a_sync - all async code
28//! * formatters - syslog message formatter.
29//! * sync - all sync code
30//! * src - all common code for both sync and async.
31//! 
32//! Features: 
33//! * feature = "build_sync"  a base feature which enables all SYNC code. Just enabling the `use_sync`
34//!      would enable only basic SharedSyslog.
35//! * feature = "build_async_tokio" a Tokio based async IO. Includes the tokio and tokio-rustls deps.
36//! * feature = "build_async_smol" for synchronious with async processing (use syslog_rs::sy_async_queue::{Syslog};)
37//! * feature = "build_with_queue" a Smol based async IO. Includes the smol and futures-rustls dep.
38//!     moves the syslog client to separate thread and by default uses 
39//!     crossbeam-channel to receive messages.
40//!     Depending on which async procvider is selected, the channel realization 
41//!     will be switched suring compilation. In this case, an async code can be 
42//!     attached to the sync queue which allows to write to the same syslog
43//!     connection with the same setup.
44//! * feature = "build_ext_net" adds the networking support! (TCP, UDP)
45//! * feature = "build_ext_tls" adds the TLS support over network (TCP).
46//! * feature = "build_ext_file" writing directly to file.
47//! 
48//! --
49//! * feature = "udp_truncate_1024_bytes" - Forces truncation of the (whole) syslog message to 1024
50//! * feature = udp_truncate_1440_bytes - Forces truncation of the (whole) syslog message to 1440
51//! * feature = tcp_truncate_1024_bytes - Forces truncation of the (whole) syslog message to 1024
52//! * feature = tcp_truncate_2048_bytes - Forces truncation of the (whole) syslog message to 2048 
53//! * feature = tcp_truncate_4096_bytes - Forces truncation of the (whole) syslog message to 4096
54//! * feature = tcp_truncate_max_bytes - Forces truncation of the (whole) syslog message to 8192 (max)
55//! * feature = dgram_sysctl_failure_panic - BSD only! By default, if crate fails to obtain a sysctl net.local.dgram.maxdgram, 
56//!     it returns a default value 2048. If this feature is enabled, then it will panic.
57//! 
58//! `build_async_tokio` and `build_async_smol` cannot be used together.
59//! 
60//! `build_with_queue` behaves differently if enabled ine of the async providers.
61//! 
62//! # Usage
63//! 
64//! syslog-rs = {version = "2.0", default-features = false, features = ["build_sync", "build_ext_net", "udp_truncate_1440_bytes"]}
65//! 
66//! By default, the following features are enabled: `build_sync`, `truncate_default`
67//! 
68//! ```ignore
69//! use std::{sync::LazyLock, thread};
70//! use std::time::Duration;
71//! 
72//! use syslog_rs::sy_sync::Syslog;
73//! 
74//! use syslog_rs::{LogFacility, LogStat, Priority, SyslogLocal};
75//! 
76//! pub static SYSLOG: LazyLock<Syslog> = LazyLock::new(|| 
77//!    {
78//!        Syslog::openlog(
79//!            Some("example"), 
80//!            LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
81//!            LogFacility::LOG_DAEMON, SyslogLocal::new()
82//!        )
83//!        .unwrap()
84//!    }
85//! );
86//! 
87//! 
88//! macro_rules! logdebug 
89//! {
90//!     ($($arg:tt)*) => (
91//!         SYSLOG.syslog(Priority::LOG_DEBUG, format!($($arg)*))
92//!     )
93//! }
94//! 
95//! pub fn main()
96//! {
97//!     logdebug!("test message1!");
98//! 
99//!     SYSLOG.change_identity("example2").unwrap();
100//! 
101//!     logdebug!("test message from new ident");
102//! 
103//!     thread::sleep(Duration::from_micros(10));
104//! 
105//!     return;
106//! }
107//! ```
108//! 
109//! ```ignore
110//! use syslog_rs::sy_async::AsyncSyslog;
111//! use tokio::sync::OnceCell;
112//! use tokio::time::{Duration, sleep};
113//! 
114//! use syslog_rs::{LogFacility, LogStat, Priority, SyslogLocal};
115//! 
116//! 
117//! pub static SYSLOG: OnceCell<AsyncSyslog> = OnceCell::const_new();
118//! 
119//! 
120//! 
121//! 
122//! macro_rules! logdebug 
123//! {
124//!     ($($arg:tt)*) => (
125//!         SYSLOG.get().unwrap().syslog(Priority::LOG_DEBUG, format!($($arg)*)).await
126//!     )
127//! }
128//! 
129//! #[tokio::main]
130//! async fn main()
131//! {
132//!     let syslog =
133//!         AsyncSyslog::openlog(
134//!                 Some("example"), 
135//!                 LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
136//!                 LogFacility::LOG_DAEMON,
137//!                 SyslogLocal::new()
138//!             )
139//!             .await
140//!             .unwrap();
141//! 
142//! 
143//!     SYSLOG.get_or_init(|| async { syslog }).await;
144//! 
145//! 
146//!     logdebug!("test message async start!");
147//!     
148//!     SYSLOG.get().unwrap().vsyslog(Priority::LOG_DEBUG, "test 2").await;
149//! 
150//!     sleep(Duration::from_micros(10)).await;
151//! 
152//!     SYSLOG.get().unwrap().change_identity("new_identity").await.unwrap();
153//! 
154//!     logdebug!("test message new identity!");
155//! 
156//!     sleep(Duration::from_micros(10)).await;
157//! 
158//!     logdebug!("test 123!");
159//!     logdebug!("test 123123! end ");
160//!     return;
161//! }
162//! 
163//! ```
164
165// make sure that the smol and tokio are not used together.
166#[cfg(all(feature = "build_async_tokio", feature = "build_async_smol"))]
167compile_error!("use_async_tokio and use_async_smol can not be used at the same time");
168
169
170//-- REEXPORT --
171#[macro_use] 
172extern crate bitflags;
173pub extern crate chrono;
174pub extern crate nix;
175extern crate socket2;
176
177// -- TLS --
178
179#[cfg(all(feature = "build_sync", feature = "build_ext_tls"))]
180extern crate rustls;
181
182#[cfg(all(feature = "build_async_tokio", feature = "build_ext_tls"))]
183extern crate tokio_rustls;
184
185#[cfg(all(feature = "build_async_smol", feature = "build_ext_tls"))]
186extern crate futures_rustls;
187
188// -- ASYNC --
189
190#[cfg(feature = "build_async_tokio")]
191extern crate tokio;
192
193#[cfg(feature = "build_async_smol")]
194extern crate smol;
195
196// -- SYNC --
197#[cfg(feature = "build_with_queue")]
198extern crate crossbeam_channel;
199
200#[cfg(feature = "build_sync")]
201pub mod sync;
202
203#[cfg(feature = "async_enabled")]
204pub mod a_sync;
205
206pub mod formatters;
207
208pub mod syslog_provider;
209pub use syslog_provider::*;
210
211
212//-- NON EXPORT
213mod portable;
214
215pub mod socket;
216
217//-- PUBLIC 
218
219#[macro_use] 
220pub mod common;
221#[macro_use] 
222pub mod error;
223
224pub use socket::TapType;
225
226pub use syslog_provider::*;
227
228#[cfg(feature = "build_sync")]
229pub use sync::syslog as sy_sync;
230#[cfg(feature = "build_sync")]
231pub use sync::syslog::Syslog;
232#[cfg(feature = "build_sync")]
233pub use sync::syslog_sync_shared::SyslogShared;
234
235#[cfg(feature = "async_enabled")]
236pub use a_sync::syslog_async as sy_async;
237#[cfg(feature = "async_enabled")]
238pub use a_sync::syslog_async::AsyncSyslog;
239#[cfg(feature = "async_enabled")]
240pub use a_sync::syslog_async_shared::AsyncSyslogShared;
241#[cfg(all(feature = "async_enabled", feature = "build_with_queue"))]
242pub use a_sync::syslog_async_queue::AsyncSyslogQueue;
243
244#[cfg(feature = "build_with_queue")]
245pub use sync::syslog_sync_queue as sy_sync_queue;
246#[cfg(feature = "build_with_queue")]
247pub use sync::syslog_sync_queue::SyslogQueue;
248
249pub use common::*;