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_async_interface" for asynchronious interface only without any realisations.
38//! * feature = "build_with_queue" a Smol based async IO. Includes the smol and futures-rustls dep.
39//! moves the syslog client to separate thread and by default uses
40//! crossbeam-channel to receive messages.
41//! Depending on which async procvider is selected, the channel realization
42//! will be switched suring compilation. In this case, an async code can be
43//! attached to the sync queue which allows to write to the same syslog
44//! connection with the same setup.
45//! * feature = "build_ext_net" adds the networking support! (TCP, UDP)
46//! * feature = "build_ext_tls" adds the TLS support over network (TCP).
47//! * feature = "build_ext_file" writing directly to file.
48//!
49//! --
50//! * feature = "udp_truncate_1024_bytes" - Forces truncation of the (whole) syslog message to 1024
51//! * feature = udp_truncate_1440_bytes - Forces truncation of the (whole) syslog message to 1440
52//! * feature = tcp_truncate_1024_bytes - Forces truncation of the (whole) syslog message to 1024
53//! * feature = tcp_truncate_2048_bytes - Forces truncation of the (whole) syslog message to 2048
54//! * feature = tcp_truncate_4096_bytes - Forces truncation of the (whole) syslog message to 4096
55//! * feature = tcp_truncate_max_bytes - Forces truncation of the (whole) syslog message to 8192 (max)
56//! * feature = dgram_sysctl_failure_panic - BSD only! By default, if crate fails to obtain a sysctl net.local.dgram.maxdgram,
57//! it returns a default value 2048. If this feature is enabled, then it will panic.
58//!
59//! `build_async_tokio` and `build_async_smol` cannot be used together.
60//!
61//! `build_with_queue` behaves differently if enabled ine of the async providers.
62//!
63//! # Usage
64//!
65//! syslog-rs = {version = "2.0", default-features = false, features = ["build_sync", "build_ext_net", "udp_truncate_1440_bytes"]}
66//!
67//! By default, the following features are enabled: `build_sync`, `truncate_default`
68//!
69//! ```ignore
70//! use std::{sync::LazyLock, thread};
71//! use std::time::Duration;
72//!
73//! use syslog_rs::sy_sync::Syslog;
74//!
75//! use syslog_rs::{LogFacility, LogStat, Priority, SyslogLocal};
76//!
77//! pub static SYSLOG: LazyLock<Syslog> = LazyLock::new(||
78//! {
79//! Syslog::openlog(
80//! Some("example"),
81//! LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
82//! LogFacility::LOG_DAEMON, SyslogLocal::new()
83//! )
84//! .unwrap()
85//! }
86//! );
87//!
88//!
89//! macro_rules! logdebug
90//! {
91//! ($($arg:tt)*) => (
92//! SYSLOG.syslog(Priority::LOG_DEBUG, format!($($arg)*))
93//! )
94//! }
95//!
96//! pub fn main()
97//! {
98//! logdebug!("test message1!");
99//!
100//! SYSLOG.change_identity("example2").unwrap();
101//!
102//! logdebug!("test message from new ident");
103//!
104//! thread::sleep(Duration::from_micros(10));
105//!
106//! return;
107//! }
108//! ```
109//!
110//! ```ignore
111//! use syslog_rs::sy_async::AsyncSyslog;
112//! use tokio::sync::OnceCell;
113//! use tokio::time::{Duration, sleep};
114//!
115//! use syslog_rs::{LogFacility, LogStat, Priority, SyslogLocal};
116//!
117//!
118//! pub static SYSLOG: OnceCell<AsyncSyslog> = OnceCell::const_new();
119//!
120//!
121//!
122//!
123//! macro_rules! logdebug
124//! {
125//! ($($arg:tt)*) => (
126//! SYSLOG.get().unwrap().syslog(Priority::LOG_DEBUG, format!($($arg)*)).await
127//! )
128//! }
129//!
130//! #[tokio::main]
131//! async fn main()
132//! {
133//! let syslog =
134//! AsyncSyslog::openlog(
135//! Some("example"),
136//! LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
137//! LogFacility::LOG_DAEMON,
138//! SyslogLocal::new()
139//! )
140//! .await
141//! .unwrap();
142//!
143//!
144//! SYSLOG.get_or_init(|| async { syslog }).await;
145//!
146//!
147//! logdebug!("test message async start!");
148//!
149//! SYSLOG.get().unwrap().vsyslog(Priority::LOG_DEBUG, "test 2").await;
150//!
151//! sleep(Duration::from_micros(10)).await;
152//!
153//! SYSLOG.get().unwrap().change_identity("new_identity").await.unwrap();
154//!
155//! logdebug!("test message new identity!");
156//!
157//! sleep(Duration::from_micros(10)).await;
158//!
159//! logdebug!("test 123!");
160//! logdebug!("test 123123! end ");
161//! return;
162//! }
163//!
164//! ```
165
166// make sure that the smol and tokio are not used together.
167#[cfg(all(feature = "build_async_tokio", feature = "build_async_smol"))]
168compile_error!("use_async_tokio and use_async_smol can not be used at the same time");
169
170// make sure that the build_async_interface and either async_embedded are not used together.
171#[cfg(all(feature = "build_async_interface", feature = "async_embedded"))]
172compile_error!("build_async_interface and either tokio/smol can not be used at the same time");
173
174//-- REEXPORT --
175#[macro_use]
176extern crate bitflags;
177pub extern crate chrono;
178pub extern crate nix;
179extern crate socket2;
180
181// -- TLS --
182
183#[cfg(all(feature = "build_sync", feature = "build_ext_tls"))]
184extern crate rustls;
185
186#[cfg(all(feature = "build_async_tokio", feature = "build_ext_tls"))]
187extern crate tokio_rustls;
188
189#[cfg(all(feature = "build_async_smol", feature = "build_ext_tls"))]
190extern crate futures_rustls;
191
192// -- ASYNC --
193
194#[cfg(feature = "build_async_tokio")]
195extern crate tokio;
196
197#[cfg(feature = "build_async_smol")]
198extern crate smol;
199
200// -- SYNC --
201#[cfg(feature = "build_with_queue")]
202extern crate crossbeam_channel;
203
204/// A sync syslog realization including shared and queued syslog client.
205#[cfg(feature = "build_sync")]
206pub mod sync;
207
208/// An async interface and/or realisations. The main async syslog struct is located there.
209#[cfg(feature = "async_enabled")]
210pub mod a_sync;
211
212/// A message formatters i.e RFC5424 and other. Also a message formatter interface.
213pub mod formatters;
214
215pub mod syslog_provider;
216
217/// A syslog destnation. i.e local, remote, remote TLS, local file. Needed to initialize the
218/// (both sync and async) instances.
219pub use syslog_provider::*;
220
221/// A common things for the sockets managment.
222pub mod socket;
223
224/// A common code which holds all flags and macroses.
225#[macro_use]
226pub mod common;
227
228/// Unified error interface.
229#[macro_use]
230pub mod error;
231
232
233//-- NON EXPORT
234/// A portable code which is different for different OSes.
235mod portable;
236
237/// Public use of the type of the tap.
238pub use socket::TapType;
239
240/// A single shared (between threads) a sync type of the syslog client.
241#[cfg(feature = "build_sync")]
242pub use sync::syslog_sync_shared::SyslogShared;
243
244#[cfg(feature = "build_sync")]
245pub use sync::syslog_trait::SyslogApi;
246
247/// A streamable sync instance (both).
248#[cfg(feature = "build_sync")]
249pub use sync::StreamableSyslogApi;
250
251/// A `sy_async` is a shortcut to the file `syslog_async.rs` in dir `a_sync`.
252#[cfg(feature = "async_enabled")]
253pub use a_sync::syslog_async as sy_async;
254
255/// A main async syslog instance.
256#[cfg(feature = "async_enabled")]
257pub use sy_async::AsyncSyslog;
258
259/// A shortcut for the internals of the `async_syslog` and IO for user implementation.
260#[cfg(feature = "build_async_interface")]
261pub use a_sync::syslog_async_internal::{AsyncSyslogInternal, AsyncSyslogInternalIO};
262
263/// A shortcut to the trait `AsyncMutex` which unifies the mutex realiztion of different
264/// exec environments.
265#[cfg(feature = "build_async_interface")]
266pub use a_sync::syslog_async_internal::AsyncMutex;
267
268/// Use this trait to access queue interface for async code.
269#[cfg(all(feature = "async_enabled", feature = "build_with_queue"))]
270pub use a_sync::syslog_trait::AsyncSyslogQueueApi;
271
272/// A shortcut to the file `syslog_sync_queue.rs` in dir `sync`.
273#[cfg(feature = "build_with_queue")]
274pub use sync::syslog_sync_queue as sy_sync_queue;
275
276/// A queued sync type of the syslog client. Also async queue.
277#[cfg(feature = "build_with_queue")]
278pub use sync::syslog_sync_queue::SyslogQueue;
279
280/// A shortcut use of all in the `common.rs`.
281pub use common::*;